关于控件运行期与设计期重复create的问题.
各位,小弟初学Delphi,刚刚学到写控件这个方面,但是遇到一个棘手的问题(对于我来说),请大家帮忙解答一下.不胜感激!
正文:
我写了一个控件,打算用来仿WIN8的Metro风格. 如图
但是这只是设计期
运行期会出现这个问题.
如图.
我单步运行检查的时候发现是设计期create一次,运行的时候又create了一次. 我查阅了资料,看到可以判断是不是在设计期.
我用了这个方法,但是好像不适用,因为所有东西都要在设计期进行调整. 所以,现在想请各位帮帮忙了. 天资愚钝实在想不出解决办法了. 下面是源码.
- Delphi(Pascal) code
unit MetroTitle;interfaceuses System.UITypes, System.Classes, FMX.ListBox, FMX.Layouts, FMX.Types, FMX.Controls, FMX.Objects, FMX.Effects,System.Types; type TMetroTitle = class(TImage) private FRectangle : TRectangle; FPanel : TPanel; FTiTleText : TText; FText : TText; FReflection : TReflectionEffect; procedure SetPanelColor(Color : TAlphaColor); function GetPanelColor : TAlphaColor; procedure SetText(Text : string); function GetText : string; procedure SetTitleText(Text : string); function GetTitleText : string; procedure SetReflectionDepth(Value : Single); procedure SetReflectionEnabled(Value : Boolean); public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published property TiTle_Text : string read GetTitleText write SetTitleText; property Text : string Read GetText Write SetText; property Color : TAlphaColor read GetPanelColor write SetPanelColor; property Reflection : Boolean write SetReflectionEnabled; property ReflecDepth : Single Write SetReflectionDepth; end;procedure Register;implementation{ TMetroTitle }procedure Register;begin RegisterComponents('Metro', [TMetroTitle);end;constructor TMetroTitle.Create(AOwner: TComponent);var My : TMetroTitle;begin inherited Create(AOwner); Self.Width := 217; Self.Height := 193; FPanel := TPanel.Create(Self); FPanel.Parent := Self; FPanel.Width := Self.Width; FPanel.Height := 56; FPanel.Align := TAlignLayout.alBottom; FPanel.Enabled := False; //FPanel.Position.Y := Self.Height - FPanel.Height; //FPanel.Position.X := 0; FRectangle := TRectangle.Create(Self); FRectangle.Parent := FPanel; FRectangle.Fill.Color := claBlack; FRectangle.Align := TAlignLayout.alClient; FTiTleText := TText.Create(Self); FTiTleText.Parent := FPanel; FTiTleText.Font.Size := 28; FTiTleText.Width := 80; FTiTleText.Height := 25; FTiTleText.Position.X := 0; FTiTleText.Fill.Color := claWhite; FText := TText.Create(Self); FText.Parent := FPanel; FText.Font.Size := 15; FText.Width := FPanel.Width; FText.Height := FPanel.Height - FTiTleText.Height; FText.Fill.Color := claWhite; FText.Position.y := FPanel.Height - FTiTleText.Height ; FReflection := TReflectionEffect.Create(Self); FReflection.Parent := FPanel; FReflection.Length := 0.5; FReflection.Enabled := True; FReflection.Opacity := 0.3; FReflection.Offset := 0; SetAcceptsControls(False);end;destructor TMetroTitle.Destroy;begin //FPanel.Free; // FRectangle.Free; //FTiTleText.Free; //FText.Free; // FReflection.Free; inherited;end;function TMetroTitle.GetPanelColor: TAlphaColor;begin Result := FRectangle.Fill.Color;end;function TMetroTitle.GetText: string;begin Result := FText.Text;end;function TMetroTitle.GetTitleText: string;begin Result := FTiTleText.Text;end;procedure TMetroTitle.SetPanelColor(Color: TAlphaColor);begin FRectangle.Fill.Color := Color;end;procedure TMetroTitle.SetReflectionDepth(Value: Single);begin FReflection.Length := Value;end;procedure TMetroTitle.SetReflectionEnabled(Value: Boolean);begin FReflection.Enabled := Value;end;procedure TMetroTitle.SetText(Text: string);begin FText.Text := Text;end;procedure TMetroTitle.SetTitleText(Text: string);begin FTiTleText.Text := Text;end;end.]
[解决办法]