多行可以选择显示方式的TButton
{* 作者:网事如风 *}
{* 作用:多行可以选择显示方式的TButton *}
unit MoreLineButton;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
//顶部,中部,下部
TTextLayout = (MoreLineTop, MoreLineCenter, MoreLineBottom);
//居左,居中,居右 / 其实系统已经定义^-^
TAlignment = (taRightJustify, taCenter, TaLeftJustify);
TMoreLineButton = class(TButton)
private
FLayout : TTextLayout;
//对齐方式:
FAlignment : TAlignment;
//多行显示:
FLines : TStringList;
procedure CMTextChanged(var Message: TMessage); Message CM_TEXTCHANGED;
Procedure SetLayout(Value: TTextLayout);
Procedure SetAlignment(Value: TAlignment);
procedure SetLines(Value: TStringList);
protected
Procedure CreateParams(Var Params: TCreateParams); Override;
public
Constructor Create(AOwner: TComponent); OverRide;
Destructor Destroy; OverRide;
published
Property Layout : TTextLayout Read FLayout Write SetLayout;
Property Alignment : TAlignment Read FAlignment Write SetAlignment;
property Lines : TStringList Read FLines Write SetLines;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMoreLineButton]);
end;
{ TMoreLineButton }
procedure TMoreLineButton.CMTextChanged(var Message: TMessage);
begin
FLines.Clear;
FLines.CommaText := Caption;
Invalidate;
end;
constructor TMoreLineButton.Create(AOwner: TComponent);
begin
Inherited Create(AOwner);
FAlignment := TaCenter;
FLayout := MoreLineCenter;
FLines := TStringList.Create;
end;
procedure TMoreLineButton.CreateParams(var Params: TCreateParams);
var
Alignment_Int, Layout_Int : integer;
begin
Inherited CreateParams(Params);
Alignment_Int := BS_RIGHT;
Layout_Int := BS_TOP;
Case FAlignment of
TaRightJustify : Alignment_Int := BS_RIGHT;
TaCenter : Alignment_Int := BS_CENTER;
TaLeftJustify : Alignment_Int := BS_LEFT;
end;
Case FLayout of
MoreLineTop : Layout_Int := BS_TOP;
MoreLineBottom : Layout_Int := BS_BOTTOM;
MoreLineCenter : Layout_Int := BS_VCENTER;
end;
Params.Style := Params.Style + BS_MULTILINE + Layout_Int + Alignment_Int;
end;
Destructor TMoreLineButton.Destroy;
begin
FreeAndNil(Flines);
Inherited Destroy;
end;
procedure TMoreLineButton.SetAlignment(Value: TAlignment);
begin
if FAlignment <> Value then
begin
FAlignment := Value;
ReCreateWnd;
end;
end;
procedure TMoreLineButton.SetLayout(Value: TTextLayout);
begin
if FLayout <> Value then
begin
FLayout := Value;
ReCreateWnd;
end;
end;
procedure TMoreLineButton.SetLines(Value: TStringList);
var
I : integer;
begin
if Value = Nil then Exit;
if Value.Count <= 0 then Exit;
FLines.Assign(Value);
Caption := Value.Strings[0];
for I := 1 to Value.Count-1 do
Caption := Caption + #13#10 + Value.Strings[I];
end;
end.