Setting text background color when using canvas.TextRect lazarus / 31-LCL ---------------------------------------------------------------------- scribly · 2019-04-08 21:06 I've got a customcontrol on which I draw using a canvas. The screen is currently white, and the canvas brush.color is set to red and brush.style is set to bsSolid , but the background of the text stays white. Do I have to call fillRect first before calling textRext  ?Or is there another option ? ---------------------------------------------------------------------- 440bx · 2019-04-08 21:38 Disclaimer: I know nothing about the LCL but something like thisCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SetBkMode(Canvas.Handle,TRANSPARENT);might solve the problem. ---------------------------------------------------------------------- lucamar · 2019-04-08 21:49 Use the full form of TextRect and make   Style.Opaque := True; or simply set:   Canvas.TextStyle.Opaque := True; that is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Style: TTextStyle;[... more code ...]  Style.Opaque := True;  { Set other Style members as you see fit ... }  Canvas.TextRext(ARect,  X, Y, 'My Text', Style); { ALTERNATIVELY, just set the TextStyle of the Canvas:  Canvas.TextStyle.Opaque := True;  Canvas.TextRext(ARect,  X, Y, 'My Text');} ---------------------------------------------------------------------- scribly · 2019-04-09 05:39 Thanks Though one big issue is that when using a textrect that's higher than the text itself, the color under it will change as well (also the background after the text). Even if it's multiple lines.  I just need the textpart to have the background color, not the rect that's being written in (Multiple textrect calls with different colors in the same rect) So I'm ending up specifying a rect with just the textheight and width of the string i'm going to write, within the provided rect region, bringing me back to similarly calling fillcanvas on the textpart ---------------------------------------------------------------------- wp · 2019-04-09 08:29 The following user-defined TCustomControl displays the text background color correctly out of the box. Please show more of you code. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type  TMyControl = class(TCustomControl)  private    FTextBackground: TColor;    procedure SetTextBackground(AValue: TColor);  protected    procedure Paint; override;  published    constructor Create(AOwner: TComponent); override;    property Caption;    property Color;    property TextBackground: TColor read FTextBackground write SetTextBackground;  end;   { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin  with TMyControl.Create(self) do begin    Left := 10;    Top := 10;    Width := 200;    Height := 100;    Caption := 'Hallo world';    Color := clYellow;    TextBackground := clRed;    Parent := Self;  end;end; constructor TMyControl.Create(AOwner: TComponent);begin  inherited;  FTextBackground := Color;end; procedure TMyControl.Paint;begin  with Canvas do begin    Brush.Color := Self.Color;    Brush.Style := bsSolid;    FillRect(0, 0, ClientWidth, ClientHeight);     Brush.Color := FTextBackground;    Font.Assign(Self.Font);    TextOut(0, 0, Caption);  end;end; procedure TMyControl.SetTextBackground(AValue: TColor);begin  if FTextBackground = AValue then exit;  FTextBackground := AValue;  Invalidate;end; end. ---------------------------------------------------------------------- scribly · 2019-04-09 15:19 Replace your Paint with this code and you'll see what I mean Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyControl.Paint;var style: TTextStyle;begin  with Canvas do begin    Brush.Color := Self.Color;    Brush.Style := bsSolid;    FillRect(0, 0, ClientWidth, ClientHeight);     Brush.Color := FTextBackground;    Font.Assign(Self.Font);     //I only want to the text to be within the topleft corner    style:=canvas.TextStyle;     style.Opaque:=true;   TextRect(rect(0,0,ClientWidth div 2, ClientHeight div 2),0,20, 'This is a long piece of text that will not fit in the small space provided by the rect',  Style);    TextRect(rect(0,0,ClientWidth div 2, ClientHeight div 2),0,0, 'This is another long piece of text that will not fit in the small space provided by the rect',  Style);   end;end;   I like the autocut off that TextRect does, but apparently it also fills the whole region with the color if opaque Anyhow, just calculating the height and width of the text first and then draw a rectangle for just those strings will work as well ---------------------------------------------------------------------- lucamar · 2019-04-09 19:55 Quote from: scribly on April 09, 2019, 05:19:20 pmI like the autocut off that TextRect does, but apparently it also fills the whole region with the color if opaque Take into account that TextRect() basically just draws a filled rectangle as specified by ARect and renders the text into it as specified by the rest of parameters. The behaviour you're seeing is exactly as it should be. Of course, you can use TextExtent() to define ARect or, maybe better, change to TextOut(), as wp did. ---------------------------------------------------------------------- wp · 2019-04-09 20:13 If you want wordwrap with colored text background you can use TextRect with adapted TextStyle, too. Turn clipping off and set the height of the rectangle to zero: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyControl.Paint;var  style: TTextStyle;  R: TRect;begin  with Canvas do begin    Brush.Color := Self.Color;    Brush.Style := bsSolid;    FillRect(0, 0, ClientWidth, ClientHeight);     style := TextStyle;    style.Opaque := true;    style.WordBreak := true;    style.SingleLine := false;    style.Clipping := false;     R := Rect(0, 0, ClientWidth, 0);     Font.Assign(Self.Font);    Brush.Color := FTextBackground;    TextRect(R, 0, 0, Caption, style);  end;end; procedure TForm1.FormCreate(Sender: TObject);begin  with TMyControl.Create(self) do begin    Left := 10;    Top := 10;    Width := 200;    Height := 100;    Caption := 'This is a long piece of text that will not fit in the small space provided by the rect';    Color := clYellow;    TextBackground := clRed;    Parent := Self;  end;end;