gpt4 book ai didi

delphi - Firemonkey自定义组件

转载 作者:行者123 更新时间:2023-12-02 09:06:02 28 4
gpt4 key购买 nike

我正在尝试为 Delphi XE8 上的 FireMonkey 创建跨平台组件...

但是我遇到了一些问题。尽管在对象检查器中设置了属性“宽度”和“高度”进行编译,但显然大小设置被忽略。当我重新打开项目时,我的组件也很小。 (我注意到宽度和高度设置没有保存到 DFM 文件中)。

注意:FireMonkey 的所有其他 native 组件都可以正常工作,只有我的自定义组件不能正常工作。

有什么问题吗?

unit FMX.Card;

interface

uses
System.SysUtils, System.Classes, FMX.Types, FMX.Controls, FMX.Graphics, System.Types;

type

TCardNum = (Ace, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King);

TCardSuit = (Clubs, Diamonds, Hearts, Spades);

TCard = class(TControl)
private
{ Private declarations }
FCardBack: TBitmap;
FCardDown: Boolean;
FCardSet: TBitmap;
FCardNum: TCardNum;
FCardSuit: TCardSuit;
procedure SetCardDown(AValue: Boolean);
procedure SetCardNum(AValue: TCardNum);
procedure SetCardSuit(AValue: TCardSuit);
protected
{ Protected declarations }
procedure Paint; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Position;
property RotationAngle;
property Width;
property Height;
property CardDown: Boolean read FCardDown write FCardDown;
property CardNum: TCardNum read FCardNum write FCardNum;
property CardSuit: TCardSuit read FCardSuit write FCardSuit;
end;

implementation

{$R 'CardResource.res'}

const
DEFAULT_CARD_WIDTH = 71;
DEFAULT_CARD_HEIGHT = 96;

{ TCard }

constructor TCard.Create(AOwner: TComponent);
var
LRStream: TResourceStream;
begin
inherited;
LRStream := TResourceStream.Create(HInstance, 'CardBack', RT_RCDATA);
try
FCardBack := TBitmap.CreateFromStream(LRStream);
finally
LRStream.Free;
end;
LRStream := TResourceStream.Create(HInstance, 'CardSet', RT_RCDATA);
try
FCardSet := TBitmap.CreateFromStream(LRStream);
finally
LRStream.Free;
end;
end;

destructor TCard.Destroy;
begin
FCardBack.Free;
FCardSet.Free;
inherited;
end;

procedure TCard.Paint;
var
LLeft: Single;
LTop: Single;
begin
inherited;
Canvas.BeginScene;
try
if FCardDown then
Canvas.DrawBitmap(FCardBack, TRectF.Create(0, 0, FCardBack.Width, FCardBack.Height), TRectF.Create(0, 0, Width, Height), 1.0)
else
begin
LLeft := Ord(FCardNum) * DEFAULT_CARD_WIDTH;
LTop := Ord(FCardSuit) * DEFAULT_CARD_HEIGHT;
Canvas.DrawBitmap(FCardSet, TRectF.Create(LLeft, LTop, LLeft + DEFAULT_CARD_WIDTH, LTop + DEFAULT_CARD_HEIGHT), TRectF.Create(0, 0, Width, Height), 1.0);
end;
finally
Canvas.EndScene;
end;
end;

最佳答案

解决办法是添加“属性大小;”也已发布。

published
{ Published declarations }
property Size;
property Width;
property Height;

如果您只添加“高度”和“宽度”,则仅在设计时起作用。

感谢所有试图帮助我的人。

关于delphi - Firemonkey自定义组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31054666/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com