如何在 TcustomContol 中使用 Picture.LoadFromFile()

发布于 2024-09-24 17:53:49 字数 223 浏览 4 评论 0原文

我创建了一个从 TcustomControl 派生的 TSkinPanel,

它有一个 FGraphic:TPicture。

FGraphic 绘制在 TSkinPanel 的画布上,如果从 TObject Inspector 加载图像,则可以正常工作。

但我不会在运行时加载图像“Form1.SkinPanel1.Picture.LoadFromFile('skin.bmp');

I created a TSkinPanel derive from TcustomControl

it has a FGraphic: TPicture.

the FGraphic is drawn on the canvas of the TSkinPanel and works fine if you load and image from the TObject Inspector.

but i doesnt won'k work on loading image on runtime "Form1.SkinPanel1.Picture.LoadFromFile('skin.bmp');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

地狱即天堂 2024-10-01 17:53:49

您必须使用 TPicture.OnChange 事件,例如:

type
  TSkinPanel = class(TCustomControl)
  private
    FPicture: TPicture;
    procedure PictureChanged(Sender: TObject);
    procedure SetPicture(Value: TPicture);
  protected
    procedure Paint; override;
  public
    constructor Create(Owner: TComponent); override;
    destructor Destroy; override;
  published
    property Picture: TPicture read FPicture write SetPicture;
  end;

  constructor TSkinPanel.Create(Owner: TComponent);
  begin
    inherited;
    FPicture := TPicture.Create;
    FPicture.OnChange := PictureChanged;
  end;

  destructor TSkinPanel.Destroy;
  begin
    FPicture.Free;
    inherited;
  end;

  procedure TSkinPanel.PictureChanged(Sender: TObject);
  begin
    Invalidate;
  end;

  procedure TSkinPanel.SetPicture(Value: TPicture);
  begin
    FPicture.Assign(Value);
  end;

  procedure TSkinPanel.Paint;
  begin
    if (FPicture.Graphic <> nil) and (not FPicture.Graphic.Empty) then
    begin
      // use FPicture as needed...
    end;
  end;

You have to use the TPicture.OnChange event, eg:

type
  TSkinPanel = class(TCustomControl)
  private
    FPicture: TPicture;
    procedure PictureChanged(Sender: TObject);
    procedure SetPicture(Value: TPicture);
  protected
    procedure Paint; override;
  public
    constructor Create(Owner: TComponent); override;
    destructor Destroy; override;
  published
    property Picture: TPicture read FPicture write SetPicture;
  end;

  constructor TSkinPanel.Create(Owner: TComponent);
  begin
    inherited;
    FPicture := TPicture.Create;
    FPicture.OnChange := PictureChanged;
  end;

  destructor TSkinPanel.Destroy;
  begin
    FPicture.Free;
    inherited;
  end;

  procedure TSkinPanel.PictureChanged(Sender: TObject);
  begin
    Invalidate;
  end;

  procedure TSkinPanel.SetPicture(Value: TPicture);
  begin
    FPicture.Assign(Value);
  end;

  procedure TSkinPanel.Paint;
  begin
    if (FPicture.Graphic <> nil) and (not FPicture.Graphic.Empty) then
    begin
      // use FPicture as needed...
    end;
  end;
乞讨 2024-10-01 17:53:49

如果调用 Picture.LoadFromFile 时没有出现错误,那么它很可能有效,但您的控件根本没有对更改做出反应。首先要做的是处理 Picture.OnChange 事件处理程序并执行某些操作:如果您自己进行绘画,则只需调用 Invalidate()(如果您使用的是 Picture)要设置一些其他控件来依次进行绘制,请在 OnChange 中执行适当的 Assign()

If you get no error when you call Picture.LoadFromFile then chances are it worked just but your control is simply not reacting to the change. The first thing to do is to handle the Picture.OnChange event handler and do something: if you do the painting yourself simply call Invalidate(), if you're using Picture to set up some other control that in turn does the painting, do the appropriate Assign() fom OnChange.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文