我正在尝试在 Delphi XE8 上为 FireMonkey 创建一个跨平台组件...
但我面临一些问题。虽然对象检查器中的属性“宽度”和“高度”显然要编译,但大小设置被忽略了。当我重新打开项目时,我的组件也很小。(我注意到宽度和高度设置没有保存到 DFM 文件中)。
注意:FireMonkey 的所有其他本机组件都可以正常工作,只是我的自定义没有。
有什么问题?
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;