绝对不要使用该Paint
方法重新定位控件。在最坏的情况下,这会再次触发该Paint
方法,一次又一次......因为,好吧:由于更换控件,面板需要重新绘制。Paint
,以及所有等价物,仅用于绘制自己。
关于何时实现您的代码:这应该在ShowRefresh
属性的设置器中完成。
关于如何实现您的ShowRefresh
属性:当然,您可以像现在一样移动控件。您还可以考虑使用Margins
(Delphi XE) 并对齐按钮和标签。然后属性设置器将变得相当简单:
type
TDateRangePicker = class(TCustomPanel)
private
FChooseButton: TButton;
FClearButton: TButton;
FRefreshButton: TButton;
FLabel1: TLabel;
FLabel2: TLabel;
function GetLabel1Caption: String;
function GetRefreshButtonVisible: Boolean;
procedure SetLabel1Caption(const Value: String);
procedure SetRefreshButtonVisible(Value: Boolean);
public
constructor Create(AOwner: TComponent); override;
published
property RefreshButtonVisible: Boolean read GetRefreshButtonVisible
write SetRefreshButtonVisible default True;
property Label1Caption: String read GetLabel1Caption
write SetLabel1Caption;
end;
...
constructor TDateRangePicker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FChooseButton := TButton.Create(Self);
FChooseButton.Caption := 'Choose';
FChooseButton.Align := alRight;
FChooseButton.AlignWithMargins := True;
FChooseButton.Margins.Left := 10;
FChooseButton.Parent := Self;
FClearButton := TButton.Create(Self);
FClearButton.Caption := 'Clear';
FClearButton.Align := alRight;
FClearButton.AlignWithMargins := True;
FClearButton.Margins.Left := 10;
FClearButton.Parent := Self;
FRefreshButton := TButton.Create(Self);
FRefreshButton.Caption := 'Refresh';
FRefreshButton.Align := alRight;
FRefreshButton.AlignWithMargins := True;
FRefreshButton.Margins.Left := 10;
FRefreshButton.Parent := Self;
FLabel1 := TLabel.Create(Self);
FLabel1.Caption := 'Foo caption: ';
FLabel1.Align := alLeft;
FLabel1.Layout := tlCenter;
FLabel1.Parent := Self;
FLabel2 := TLabel.Create(Self);
FLabel2.Caption := 'From 03/08/2012 to 06/06/2012';
FLabel2.Align := alLeft;
FLabel2.Layout := tlCenter;
FLabel2.Parent := Self;
end;
function TDateRangePicker.GetLabel1Caption: String;
begin
Result := FLabel1.Caption;
end;
function TDateRangePicker.GetRefreshButtonVisible: Boolean;
begin
Result := FRefreshButton.Visible;
end;
procedure TDateRangePicker.SetLabel1Caption(const Value: String);
begin
FLabel1.Caption := Value;
end;
procedure TDateRangePicker.SetRefreshButtonVisible(Value: Boolean);
begin
FRefreshButton.Visible := Value;
FRefreshButton.Left := Width;
end;
和测试程序:
procedure TMainForm.TestButtonClick(Sender: TObject);
begin
DateRangePicker1.Label1Caption := 'Test: ';
DateRangePicker1.RefreshButtonVisible := not DateRangePicker1.RefreshButtonVisible;
end;