ButtonのCaptionで改行を使って文字を複数段で表示したい
|
Windows95では #13#10 挿入することにより思った通り表示できるのですが
WindowsNTの場合は改行されません。
procedure TForm1.Button1Click(Sender: TObject);
begin
SetWindowLong(Button1.Handle, GWL_STYLE, GetWindowLong(Button1.Handle, GWL_STYLE) or BS_MULTILINE);
Button1.Caption := 'ABC' + #13#10 + 'DEF';
end;
なお、コンポ−ネント化する場合はCreateParamsをオーバーライドして下さい。
この方法は、TButtonControl系(TRadioButton,TCheckBox)でほとんど使えますが、オーナードロー系(TBitBtn,TSpeedButtonなど)のボタンではできません。
また、以下のプロパティエディッタをインストールすれば、オブジェクトインスペクタで,改行コードを入力を'\n'ですることが出来るようになります。
// 定義部
type
// 複数行の入力を\nで受け入れるプロパティエディッタ
TMultCapProperty = Class(TCaptionProperty)
Public
Function GetValue: string; Override;
Procedure SetValue(const Value: string); Override;
End;
procedure Register;
// 実装部
// 置き換える関数
procedure ReplaceStr(var Source : string; Search, Replace : string);
function XPos(Source, Search : string):integer;
begin
if StrPos(PChar(Source), PCHar(Search)) = nil then
result := 0
else
result := StrPos(PChar(Source), PCHar(Search)) - PChar(Source) + 1;
end;
var
p, L1, L2 : Integer;
begin
L2 := Length(Search);
p := XPos(Source, Search);
while p <> 0 do
begin
L1 := Length(Source);
if p = 1 then
Source := Replace + Copy(Source, L2 + 1, L1)
else
Source := Copy(Source, 1, p - 1) + Replace +
Copy(Source, p + L2, L1);
p := XPos(Source, Search);
end;
end;
function TMultCapProperty.GetValue: string;
begin
Result := GetStrValue;
// 以下3つのパターンがある
ReplaceStr(Result, #13 + #10, '\n');
ReplaceStr(Result, #10, '\n');
ReplaceStr(Result, #13, '\n');
end;
procedure TMultCapProperty.SetValue(const Value: string);
var
Caption : string;
begin
Caption := Value;
ReplaceStr(Caption, '\n', #13);
SetStrValue(Caption);
end;
// プロパティエディタとして登録する
procedure Register;
begin
// TLabelのCaptionプロパティエディタの登録--'\n'で改行入力
RegisterPropertyEditor(TypeInfo(TCaption), TLabel; , 'Caption', TMultCapProperty);
// TButtonのCaptionプロパティエディタの登録--'\n'で改行入力
// ただし上記BS_MULTILINEが設定されていないとダメ
RegisterPropertyEditor(TypeInfo(TCaption), TButton; , 'Caption', TMultCapProperty);
end;
|
|