Recent

Author Topic: DrawText for multiline button caption failure  (Read 14897 times)

anna

  • Sr. Member
  • ****
  • Posts: 426
DrawText for multiline button caption failure
« on: April 04, 2010, 10:46:32 am »
On my form i have only BitBtn4. For Form OnCreate event I've written:
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var S: AnsiString;
    R: TRect;
begin
with BitBtn4 do
  begin
    S:=Caption;
    Glyph.Canvas.Font := Self.Font;
    Glyph.Width := Width - 6;
    Glyph.Height := Height - 6;
    R := Bounds(0, 0, Glyph.Width, 0);
    Caption := '';
    DrawText(Glyph.Canvas.Handle,PChar(S),Length(S),R,
    DT_CENTER or DT_WORDBREAK or DT_CALCRECT);
    OffsetRect(R,(Glyph.Width - R.Right) div 2,
    (Glyph.Height - R.Bottom) div 2);
    DrawText(Glyph.Canvas.Handle,PChar(S),Length(S),R,
    DT_CENTER or DT_WORDBREAK);
  end;
end;
But I saw a black rectangle. If I add     Glyph.Mask(0);
I see text in white background, but characters have poor look.
Code: [Select]
..............
    Glyph.Mask(0);
  end;
end;
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.
« Last Edit: April 04, 2010, 11:02:45 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: DrawText for multiline button caption failure
« Reply #1 on: April 04, 2010, 11:39:13 am »
I need a same look as TButton. How to do it?
If there is simpler way to do multiline caption in button - write me.

You could take a TButton and Insert the break position yourself.
Like:  Button1.Caption:='Hello'+LineEnding+'World';

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: DrawText for multiline button caption failure
« Reply #2 on: April 04, 2010, 11:56:51 am »
I need a same look as TButton. How to do it?
If there is simpler way to do multiline caption in button - write me.

You could take a TButton and Insert the break position yourself.
Like:  Button1.Caption:='Hello'+LineEnding+'World';
It does't work. All text is in one line regardless of length of words.
WinXP SP3 Pro Russian 32-bit (5.1.2600)

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: DrawText for multiline button caption failure
« Reply #3 on: April 04, 2010, 12:20:46 pm »
I think you should draw a bitmap with the desired text and use a SpeedButton to show it.

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: DrawText for multiline button caption failure
« Reply #4 on: April 04, 2010, 12:27:34 pm »
I think you should draw a bitmap with the desired text and use a SpeedButton to show it.
What's the difference? Just now I test my project with SpeedButton instead of BitBtn - see black rectangle. Problems remain same.
WinXP SP3 Pro Russian 32-bit (5.1.2600)

José Mejuto

  • Full Member
  • ***
  • Posts: 136
Re: DrawText for multiline button caption failure
« Reply #5 on: April 04, 2010, 12:36:23 pm »
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.

Hello,

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.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: DrawText for multiline button caption failure
« Reply #6 on: April 04, 2010, 12:41:10 pm »
Quote
see black rectangle. Problems remain same.

If you set Transparent property to SpeedButton...

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: DrawText for multiline button caption failure
« Reply #7 on: April 04, 2010, 12:41:50 pm »
It does't work. All text is in one line regardless of length of words.

Yes, Sorry it works for GTK2.

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

Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var S: AnsiString;
    R: TRect;
    stl:TTextStyle;
begin
    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 := '';
    BitBtn1.Glyph.Transparent:=false;
    BitBtn1.Glyph.Canvas.Brush.Color:=clBtnFace;
    BitBtn1.Glyph.Canvas.FillRect(R);
    stl.Alignment:=taCenter;
    stl.Wordbreak:=true;
    stl.Layout:=tlCenter;
    stl.SingleLine:=false;
    R := Bounds(1, 1, BitBtn1.Glyph.Width-1, BitBtn1.Glyph.Height-1);
    BitBtn1.Glyph.Canvas.TextRect(R,0,0,S,stl);
end;

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9867
  • Debugger - SynEdit - and more
    • wiki
Re: DrawText for multiline button caption failure
« Reply #8 on: April 04, 2010, 01:37:17 pm »
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);
var
  s: String;
  r: TRect;
begin
  s:= 'test with a very long long long long long text';
  r:= SpeedButton1.ClientRect;

  // First clear the whole area
  SpeedButton1.Canvas.Brush.Color := clred;
  SpeedButton1.canvas.FillRect(r);

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



Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9867
  • Debugger - SynEdit - and more
    • wiki
Re: DrawText for multiline button caption failure
« Reply #9 on: April 04, 2010, 01:59:50 pm »
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;
    Glyph.Canvas.FillRect(r);

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: DrawText for multiline button caption failure
« Reply #10 on: April 06, 2010, 06:56:14 pm »
It does't work. All text is in one line regardless of length of words.

Yes, Sorry it works for GTK2.

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

Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var S: AnsiString;
    R: TRect;
    stl:TTextStyle;
begin
    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 := '';
    BitBtn1.Glyph.Transparent:=false;
    BitBtn1.Glyph.Canvas.Brush.Color:=clBtnFace;
    BitBtn1.Glyph.Canvas.FillRect(R);
    stl.Alignment:=taCenter;
    stl.Wordbreak:=true;
    stl.Layout:=tlCenter;
    stl.SingleLine:=false;
    R := Bounds(1, 1, BitBtn1.Glyph.Width-1, BitBtn1.Glyph.Height-1);
    BitBtn1.Glyph.Canvas.TextRect(R,0,0,S,stl);
end;

Thank you for listing. How to define size of text to be written by TextRect ?
Next operators don't work:
BitBtn1.Glyph.Canvas.Font.Size:=6;
BitBtn1.Glyph.Canvas.Font.Height:=6;

I monitored TextHeight(S) method before TextRect and was surprised that it's 13 for unknown reasons.
« Last Edit: April 06, 2010, 08:28:31 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

 

TinyPortal © 2005-2018