开发者

Newline character in caption of button

开发者 https://www.devze.com 2023-03-24 21:19 出处:网络
I am building an application in which I want to display a button on a form. I want to display the Caption of the button on two lines. I have tried using the following code in the form\'s OnCreate even

I am building an application in which I want to display a button on a form. I want to display the Caption of the button on two lines. I have tried using the following code in the form's OnCreate event but it is not showing the new line.

Button.Caption := 'Hello' + #13开发者_开发百科#10 + 'world';

Any other method to add a new line?


For very old Delphi versions which do not have the WordWrap property:

Use following code prior to setting the caption:

SetWindowLong(Button1.Handle, GWL_STYLE, 
  GetWindowLong(Button1.Handle, GWL_STYLE) or BS_MULTILINE);

But the tricky part is that this code needs execution on a number of occasions. When the button is recreated, then your multiline setting is lost. Kind of similar to this dilemma.

Luckily the VCL provides a solution, but you have to subclass the TButton type, e.g. as follows:

type
  TButton = class(StdCtrls.TButton)
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  end;

  TForm1 = class(TForm)

...

procedure TButton.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.Style := Params.Style or BS_MULTILINE;
end;


Set WordWrap to True. That's all.


Others have told you what you can do in code: set Wordwrap and use SLineBreak.

But I guess you'd like to edit the multiple lines in the designer. This is not possible in the plain IDE. There are a few 3rd party tools which allow it, but you can also simply use a '|' to separate the lines, and then, in code use something like

Button1.Caption := 
  StringReplace(Button1.Caption, '|', SLineBreak, [rfReplaceAll]);

(This is from memory, as I have no Delphi here, so please use the proper syntax).


In the System.pas (which automatically gets used) the following is defined:

const sLineBreak = {$IFDEF LINUX} AnsiChar(#10) {$ENDIF} 
                   {$IFDEF MSWINDOWS} AnsiString(#13#10) {$ENDIF};

So if you want to make your Button wrap, make sure AutoSize is set to true, and then use the following code:

button.Caption := 'Line one'+sLineBreak+'Line two';


In case you want to see to see the change in IDE, at design time, you can do the following:

  1. Right click in the control and select "View as Text".
  2. Locate the control and at the spot you want the line break, add to the value of Caption, the characters '#13#10' (CRLF).
  3. Right click and select "View as Form".
  4. Check the property WordWrap of the button control.


In Delphi 2007 you can use this:

SpeedButton1.Caption := 'first line' + #13 + 'second line';


For older versions of Delphi the Tspeedbutton only responsive for manually created strings with CRLF. Not regular TButton. This is if you do not want to hack TButton class as suggested in the best answer above.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号