Windows with Delphi Windows API (94) Windows Filesystem (41) Windows Forms (69) Windows Graphics (38)
Exchange Links About this site Links to us 
|
Getting a Line Break on a Button's caption
This article has not been rated yet. After reading, feel free to leave comments and rate it.
Question:
How do I get linebreak on a button's caption?
Answer 1:
You could use a SpeedButton or a BitButton instead of TButton. The disadvantage is that they are not native Windows components, thus overhead, different inheritance, other (window) messages e.g. the TSpeedButton cannot receive the focus.
Better solution -> Answer 2:
Answer 2:
Make a TButton descendent that has the BS_MULTILINE button style. Below is such a class which allows you to use the pipe character ('|') as a substitute for #13 in the designer when setting the button's caption.  | |  | | unit MLButton;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMultilineButton = class(Tbutton)
private
FMultiline: Boolean;
function GetCaption : String;
procedure SetCaption(const Value: String);
procedure SetMultiline(const Value: Boolean);
public
procedure CreateParams(var params: TCreateParams); override;
constructor Create(aOwner: TComponent); override;
published
property Multiline : Boolean read FMultiline write SetMultiline default True;
property Caption : String read GetCaption write SetCaption;
end;
procedure register;
implementation
procedure register;
begin
RegisterComponents('Samples', [TMultilineButton]);
end;
constructor TMultilineButton.Create(aOwner: TComponent);
begin
inherited;
FMultiline := True;
end;
procedure TMultilineButton.CreateParams(var params: TCreateParams);
begin
inherited;
if FMultiline then
begin
params.Style := params.Style or BS_MULTILINE
end;
end;
function TMultilineButton.GetCaption : String;
begin
Result := Stringreplace(inherited Caption, #13, '|', [rfReplaceAll]);
end;
procedure TMultilineButton.SetCaption(const Value: String);
begin
if Value<>Caption then
begin
inherited Caption := Stringreplace(Value, '|', #13, [rfReplaceAll]);
Invalidate;
end ;
end;
procedure TMultilineButton.SetMultiline(const Value: Boolean);
begin
if FMultiline<>Value then
begin
FMultiline := Value;
RecreateWnd;
end ;
end;
end. | |  | |  | You don't like the formatting? Check out SourceCoder then!
Comments:
|