gpt4 book ai didi

delphi - 创建一个接受 .PNG 图像作为 Glyph 的按钮

转载 作者:行者123 更新时间:2023-12-03 15:21:53 25 4
gpt4 key购买 nike

我试图了解 SpeedButton 如何Glyph属性工作,我发现该字段声明为:

FGlyph: TObject;

property如:

property Glyph: TBitmap read GetGlyph write SetGlyph stored HasCustomGlyph;

当我尝试创建自己的 SpeedButton 时,即使我逐行阅读代码,我也无法理解该代码。接受 .PNG图像也代替 .bmp仅图像。

我第一次考虑将该属性声明为 TPicture而不是TBitmap .

有没有办法用 Glyph : TPicture 创建 MySpeedButton ?

我尝试的方法如下:

TMyButton = class(TSpeedButton)
private
//
FGlyph: TPicture;
procedure SetGlyph(const Value: TPicture);
protected
//
public
//
published
//
Property Glyph : TPicture read FGlyph write SetGlyph;
end;

程序:

procedure TMyButton.SetGlyph(const Value: TPicture);
begin
FGlyph := Value;
end;

最佳答案

您的SetGlyph()需要调用FGlyph.Assign(Value)而不是FGlyph := Value。请务必在构造函数中创建 FGlyph 并在析构函数中销毁它。然后,当 Graphic 不为空时,您可以在重写的 Paint() 中调用绘制图形。

type
TMyButton = class(TGraphicControl)
private
FGlyph: TPicture;
procedure GlyphChanged(Sender: TObject);
procedure SetGlyph(const Value: TPicture);
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Glyph : TPicture read FGlyph write SetGlyph;
end;

constructor TMyButton.Create(AOwner: TComponent);
begin
inherited;
FGlyph := TPicture.Create;
FGlyph.OnChange := GlyphChanged;
end;

destructor TMyButton.Destroy;
begin
FGlyph.Free;
inherited;
end;

procedure TMyButton.GlyphChanged(Sender: TObject);
begin
Invalidate;
end;

procedure TMyButton.SetGlyph(const Value: TPicture);
begin
FGlyph.Assign(Value):
end;

procedure TMyButton.Paint;
begin
...
if (FGlyph.Graphic <> nil) and (not FGlyph.Graphic.Empty) then
Canvas.Draw(..., FGlyph.Graphic);
...
end;

关于delphi - 创建一个接受 .PNG 图像作为 Glyph 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46758140/

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