带有类对象的 onResize 事件

发布于 2024-09-25 17:47:07 字数 810 浏览 0 评论 0原文

这是我的代码:

type TNav = class(TPanel)
private
  procedure CMMouseEnter(var AMsg: TMessage); message CM_MOUSEENTER;
  procedure CMMouseLeave(var AMsg: TMessage); message CM_MOUSELEAVE;
public
end;

type TForm1 = class(TForm)
  ...
  procedure FormCreate(Sender: TObject);
private
public
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TNav.Create(Form1) do begin
    Parent := Self;
    Name := 'nav';
    Top := 0;
    Left := 0;
    Height := 27;
    Width := Form1.Width - 8;
    Visible := true;
    Caption := '';
  end;      
end;

procedure TNav.CMMouseEnter(var AMsg: TMessage);
begin
  Self.Top := 0;
end;

procedure TNav.CMMouseLeave(var AMsg: TMessage);
begin
  Self.Top := -23;
end;

有没有办法为我的 TNav 添加 onResize 事件,甚至从 Form1 发送宽度/高度值?

先感谢您!

Here's is my code:

type TNav = class(TPanel)
private
  procedure CMMouseEnter(var AMsg: TMessage); message CM_MOUSEENTER;
  procedure CMMouseLeave(var AMsg: TMessage); message CM_MOUSELEAVE;
public
end;

type TForm1 = class(TForm)
  ...
  procedure FormCreate(Sender: TObject);
private
public
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TNav.Create(Form1) do begin
    Parent := Self;
    Name := 'nav';
    Top := 0;
    Left := 0;
    Height := 27;
    Width := Form1.Width - 8;
    Visible := true;
    Caption := '';
  end;      
end;

procedure TNav.CMMouseEnter(var AMsg: TMessage);
begin
  Self.Top := 0;
end;

procedure TNav.CMMouseLeave(var AMsg: TMessage);
begin
  Self.Top := -23;
end;

Is there a way to add an onResize event for my TNav, or even to send the width/height values from the Form1?

Thank you in advance!

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

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

发布评论

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

评论(1

心不设防 2024-10-02 17:47:07

您是否想要向 TNav 的每个实例添加“OnResize”处理程序(内部),或者您只想让 TNav 显示 OnResize event 以便您可以在应用程序中设置它?在第一种情况下,只需执行

type
  TNav = class(TPanel)
  private
    procedure CMMouseEnter(var AMsg: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var AMsg: TMessage); message CM_MOUSELEAVE;
  protected
    procedure Resize; override;
  public
  end;

procedure TNav.Resize;
begin
  inherited;
  // Do something
end;

在后一种情况下,只需添加

published
  property OnResize;

要访问父表单的属性(如果有),请在 TNav 类中执行(例如)

GetParentForm(Self).Width

顺便说一下,您是知道 TPanelAnchors 属性吗?将 TPanel 添加到表单,并在属性编辑器中设置 Anchors := [akLeft,akTop,akRight]。这是你可以使用的东西吗?

Do you want to add a "OnResize" handler to every instance of the TNav (internally), or do you just want the TNav to display a OnResize event so that you can set it in the application? In the first case, just do

type
  TNav = class(TPanel)
  private
    procedure CMMouseEnter(var AMsg: TMessage); message CM_MOUSEENTER;
    procedure CMMouseLeave(var AMsg: TMessage); message CM_MOUSELEAVE;
  protected
    procedure Resize; override;
  public
  end;

and

procedure TNav.Resize;
begin
  inherited;
  // Do something
end;

In the latter case, just add

published
  property OnResize;

To access the properties of the parent form (if any), in the TNav class do (for example)

GetParentForm(Self).Width

By the way, are you aware of the Anchors property of TPanel? Add a TPanel to a form, and set Anchors := [akLeft,akTop,akRight] in the Property Editor. Is this something you can use?

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