Firemonkey 组件保存设计时大小
Firemonkey component save design time size
关于 问题,我已经能够创建一个 FMX 组件,它是一个带有黑色边框的白色矩形,可以用鼠标光标在内部绘制(就像使用 MS Paint 一样)。
当我编译 运行 程序时,我得到这个:
为什么?
如果我设置 Align
属性 例如它有效(客户端将此与客户端对齐)。如果我将组件对齐 Center
它位于中心但尺寸较小(如图所示)。
它看起来不像 "save" 我在对象检查器上设置的宽度和高度。我的组件有这个相关代码:
type
Test = class(TControl)
strict private
FLineFill: TStrokeBrush;
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Align;
property Anchors;
property ClipChildren;
property ClipParent;
property Cursor;
property Enabled;
property Locked;
property Height;
property HitTest;
property Opacity;
property Margins;
property Position;
property Visible;
property Width;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseEnter;
property OnMouseLeave;
property OnPainting;
property OnPaint;
property OnResize;
property OnResized;
end;
实现如下:
constructor Test.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLineFill := TStrokeBrush.Create(TBrushKind.Solid, $FF222222);
//more...
end;
destructor Test.Destroy;
begin
FLineFill.Free;
inherited;
end;
procedure Test.Paint;
var
begin
Canvas.Stroke.Assign(FLineFill);
Canvas.ClearRect(ClipRect, TAlphaColorRec.White);
Canvas.Stroke.Color := TAlphaColorRec.Black;
//more code...
end;
我查看了 TPlotGrid
的源代码,这是一个简单的组件,它基本上完成了我正在做的事情。我是否必须在 Paint 事件中设置更多内容?
您需要添加 Size
属性 因为它负责组件的尺寸。它 returns TControlSize
正是您所需要的。来自文档:
A TControlSize object is used for managing the size of the component.
This can be specified through the Size, Width, Height, and
PlatformDefault attributes.
只需在发布部分添加这段代码:
property Size;
如果您查看其实现,您会发现
property Width: Single read GetWidth write SetWidth stored StoreWidthHeight;
property Height: Single read GetHeight write SetHeight stored StoreWidthHeight;
关于
当我编译 运行 程序时,我得到这个:
为什么?
如果我设置 Align
属性 例如它有效(客户端将此与客户端对齐)。如果我将组件对齐 Center
它位于中心但尺寸较小(如图所示)。
它看起来不像 "save" 我在对象检查器上设置的宽度和高度。我的组件有这个相关代码:
type
Test = class(TControl)
strict private
FLineFill: TStrokeBrush;
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Align;
property Anchors;
property ClipChildren;
property ClipParent;
property Cursor;
property Enabled;
property Locked;
property Height;
property HitTest;
property Opacity;
property Margins;
property Position;
property Visible;
property Width;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseEnter;
property OnMouseLeave;
property OnPainting;
property OnPaint;
property OnResize;
property OnResized;
end;
实现如下:
constructor Test.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLineFill := TStrokeBrush.Create(TBrushKind.Solid, $FF222222);
//more...
end;
destructor Test.Destroy;
begin
FLineFill.Free;
inherited;
end;
procedure Test.Paint;
var
begin
Canvas.Stroke.Assign(FLineFill);
Canvas.ClearRect(ClipRect, TAlphaColorRec.White);
Canvas.Stroke.Color := TAlphaColorRec.Black;
//more code...
end;
我查看了 TPlotGrid
的源代码,这是一个简单的组件,它基本上完成了我正在做的事情。我是否必须在 Paint 事件中设置更多内容?
您需要添加 Size
属性 因为它负责组件的尺寸。它 returns TControlSize
正是您所需要的。来自文档:
A TControlSize object is used for managing the size of the component. This can be specified through the Size, Width, Height, and PlatformDefault attributes.
只需在发布部分添加这段代码:
property Size;
如果您查看其实现,您会发现
property Width: Single read GetWidth write SetWidth stored StoreWidthHeight;
property Height: Single read GetHeight write SetHeight stored StoreWidthHeight;