Author: Tomas Rutkauskas
How do I make a button have a two line caption? I think there is a character or
sequence there to embed a linefeed in the caption property.
Answer:
It is not as simple as adding a #13#10 sequence as a line break in the caption. You
also need to add the BS_MULTILINE style. The following sample component will accept
a pipe character ("|") in the caption as proxy for a linebreak. This allows you to
specify the break in the designer, which does not accept Return as part of the
caption string.
1 unit MLButton;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 StdCtrls;
8
9 type
10 TMultilineButton = class(TButton)
11 private
12 FMultiline: Boolean;
13 function GetCaption: string;
14 procedure SetCaption(const Value: string);
15 procedure SetMultiline(const Value: Boolean);
16 public
17 procedure CreateParams(var params: TCreateParams); override;
18 constructor Create(aOwner: TComponent); override;
19 published
20 property Multiline: Boolean read FMultiline write SetMultiline default True;
21 property Caption: string read GetCaption write SetCaption;
22 end;
23
24 procedure register;
25
26 implementation
27
28 procedure register;
29 begin
30 RegisterComponents('PBGoodies', [TMultilineButton]);
31 end;
32
33 constructor TMultilineButton.Create(aOwner: TComponent);
34 begin
35 inherited;
36 FMultiline := True;
37 end;
38
39 procedure TMultilineButton.CreateParams(var params: TCreateParams);
40 begin
41 inherited;
42 if FMultiline then
43 params.Style := params.Style or BS_MULTILINE;
44 end;
45
46 function TMultilineButton.GetCaption: string;
47 begin
48 Result := Stringreplace(inherited Caption, #13, '|', [rfReplaceAll]);
49 end;
50
51 procedure TMultilineButton.SetCaption(const Value: string);
52 begin
53 if value <> Caption then
54 begin
55 inherited Caption := Stringreplace(value, '|', #13, [rfReplaceAll]);
56 Invalidate;
57 end;
58 end;
59
60 procedure TMultilineButton.SetMultiline(const Value: Boolean);
61 begin
62 if FMultiline <> Value then
63 begin
64 FMultiline := Value;
65 RecreateWnd;
66 end;
67 end;
68
69 end.
|