Forum > General

DrawText for multiline button caption failure

<< < (2/3) > >>

José Mejuto:

--- Quote from: anna on April 04, 2010, 10:46:32 am ---On my form i have only BitBtn4. For Form OnCreate event I've written:
I need transparent background, well-look black multiline text and no black rectangle. I need a same look as TButton. How to do it?
If there is simpler way to do multiline caption in button - write me.

--- End quote ---


All this hand made special draws will cause problems in different widgetsets, it could more or less work with windows, but will fail with almost any other widgetset.

If you are interested only in win32 subclass the handle and intercept the WM_ERASEBACKGROUND and WM_PAINT messages for the button and perform you custom paint.

Or you can write a descendant from TGraphicControl and write your custom TButton, but them the platform looking is lost as you must paint everything.


--- Quote ---see black rectangle. Problems remain same.
--- End quote ---

If you set Transparent property to SpeedButton...


--- Quote from: anna on April 04, 2010, 11:56:51 am ---It does't work. All text is in one line regardless of length of words.

--- End quote ---

Yes, Sorry it works for GTK2.

You could try this for windows but I don't know if it works on other WS:

--- Code: ---procedure TForm1.FormCreate(Sender: TObject);
var S: AnsiString;
    R: TRect;
    S:='Hello World Hello World Hello World Hello World';
    BitBtn1.Glyph.Canvas.Font := BitBtn1.Font;
    BitBtn1.Glyph.Width := BitBtn1.Width - 6;
    BitBtn1.Glyph.Height := BitBtn1.Height - 6;
    R := Bounds(0, 0, BitBtn1.Glyph.Width, BitBtn1.Glyph.Height);
    BitBtn1.Caption := '';
    R := Bounds(1, 1, BitBtn1.Glyph.Width-1, BitBtn1.Glyph.Height-1);

--- End code ---

If you can use a TSpeedButton, it has an onpaint event.

IF you paint outside OnPaint, then your work will be destroyed, as soon as anything forces a repaint, that can be a mouse-over, but definitely happens, if any other window moved over your window...
In Speedbuuton OnPaint:

procedure TForm1.SpeedButton1Paint(Sender: TObject);
  s: String;
  r: TRect;
  s:= 'test with a very long long long long long text';
  r:= SpeedButton1.ClientRect;

  // First clear the whole area
  SpeedButton1.Canvas.Brush.Color := clred;

  // now draw the text
  r:= SpeedButton1.ClientRect;
  SetBkColor(SpeedButton1.Canvas.Handle ,clRed);
    DrawTextA(SpeedButton1.Canvas.Handle ,PChar(S),Length(S),@R,

You can also use your code with the glyph. It doesn't need OnPaint, since you are painting the Glyph, which will be copied in the painthandler of the bitbtn

    R := Bounds(0, 0, Glyph.Width, Glyph.Height);
    Glyph.Canvas.Brush.Color := clRed;


[0] Message Index

[#] Next page

[*] Previous page

Go to full version