Delphi 自定义消息处理程序

发布于 2024-07-19 08:15:11 字数 735 浏览 5 评论 0原文

当用户双击 dbgrid 时,我会显示一个非模式表单。

当他们关闭该表单时,我想刷新网格。

为了实现这一点,我尝试了以下操作:

1 - 定义自定义消息常量:

const
  WM_REFRESH_MSG = WM_USER + 1;  //defined in a globally available unit

2 - 在非模态表单的 OnClose 事件中,我有以下内容:

procedure TMyNonModalForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  PostMessage(Self.Handle,WM_REFRESH_MSG,0,0);
end;

3 - 在保存 dbGrid 的表单的私有声明中,我有这样的:

procedure OnRefreshRequest(var Msg: TMessage); message WM_REFRESH_MSG;

...

procedure TMyFormWithADBGrid.OnRefreshRequest(var Msg: TMessage);
begin
  RefreshGrid;
end;

完成这些操作后,PostMessage 可以正常触发,但 OnRefreshRequest 过程永远不会运行。 我究竟做错了什么?

When a user double-clicks a dbgrid, I show a non-modal form.

When they close that form, I want to refresh the grid.

To accomplish that, I have tried the following:

1 - Define a custom message constant:

const
  WM_REFRESH_MSG = WM_USER + 1;  //defined in a globally available unit

2 - In the OnClose event of my non-modal form, I have this:

procedure TMyNonModalForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  PostMessage(Self.Handle,WM_REFRESH_MSG,0,0);
end;

3 - In the private declarations of the form that holds the dbGrid, I have this:

procedure OnRefreshRequest(var Msg: TMessage); message WM_REFRESH_MSG;

...

procedure TMyFormWithADBGrid.OnRefreshRequest(var Msg: TMessage);
begin
  RefreshGrid;
end;

After doing these things, the PostMessage fires fine, but the OnRefreshRequest procedure never runs. What am I doing wrong?

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

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

发布评论

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

评论(5

冰之心 2024-07-26 08:15:11

请注意,WM_USER 不是您自己的消息所基于的正确常量,除非您正在编写直接从 TWinControl 派生的自定义控件。 请改用 WM_APP。

另外,使用 UM_ 而不是 WM_ 来表示用户消息被认为是一种很好的风格,因为该前缀应该为 Windows 头文件保留。

Note that WM_USER is not the correct constant to base your own messages on, unless you are writing a custom control directly descending from TWinControl. Use WM_APP instead.

Also, it's considered good style to use UM_ for User Message instead of WM_ because that prefix is supposed to be reserved for the Windows header files.

清风挽心 2024-07-26 08:15:11

除了其他答案中的消息名称之外,您还在 Self 消失时向 Self.Handle 发布消息。 您可能打算发布到不同的句柄(启动无模式句柄的窗口)。 当您创建无模式窗口时,让您的无模式窗口访问该句柄,然后将消息发布到那里。

Aside from the message name in the other answer, you are posting a message to Self.Handle while Self is going away. You probably meant to post to a different handle (the window that launched the modeless one). Give your modeless window access to that handle when you create it, and post the message there instead.

江城子 2024-07-26 08:15:11

发布消息需要发送到另一个窗口句柄,而不是您列出的 self.handle。 一种方法是在非模态表单上创建一个新属性,并在显示非模态表单之前为其分配另一个表单的句柄。

除此之外,并正确实现 WM_REFRESH_MSG (CheGueVerra 正确)它应该可以正常工作。

The post message needs to be sent to the other window handle, not the self.handle that you have listed. One way to do this would be to create a new property on your non-modal form and assign it the handle of the other form just before you show your non-modal one.

Other than that, and implementing the WM_REFRESH_MSG properly (CheGueVerra has it correct) it should work fine.

拥有 2024-07-26 08:15:11

您可以尝试更改声明的结尾以匹配您尝试发送的消息。

procedure OnRefreshRequest(var Msg: TMessage); message WM_CEA_REFRESH;

应该是这个

procedure OnRefreshRequest(var Msg: TMessage); message WM_REFRESH_MSG;

You might try and change the end of the declaration to match the message you are trying to send.

procedure OnRefreshRequest(var Msg: TMessage); message WM_CEA_REFRESH;

Should be this

procedure OnRefreshRequest(var Msg: TMessage); message WM_REFRESH_MSG;
情话难免假 2024-07-26 08:15:11

我上传了一个“凯文会做什么?”的示例。 访问 Embarcadero 的新闻组论坛 embarcadero.public.attachments。

基本上,它是非模式表单关闭时主表单(或您想要的任何表单/对象)订阅的自定义事件。 在主要(或其他)形式中...

var
  NonModalForm :TfmNonModalForm;
begin
  NonModalForm := TfmNonModalForm.Create(nil); 
  NonModalForm.Execute(NonModalFormClosingListener);

在执行方法中

procedure TfmNonModalForm.Execute(YourListenerMethod: THeyIClosedEvent);
begin
   FHeyIClosedEvent := YourListenerMethod;
   Show();
end;

如果您无法访问论坛并需要附加代码,请发表评论,我将发布缺少的部分。

祝你好运

I've uploaded an example of "What would Kevin do?" to Embarcadero's newsgroup forum embarcadero.public.attachments.

Basically it's a custom event that the main form (or whatever form/object you want) subscribes to when the non-modal form closes. In the main (or whatever) form...

var
  NonModalForm :TfmNonModalForm;
begin
  NonModalForm := TfmNonModalForm.Create(nil); 
  NonModalForm.Execute(NonModalFormClosingListener);

In the Execute method

procedure TfmNonModalForm.Execute(YourListenerMethod: THeyIClosedEvent);
begin
   FHeyIClosedEvent := YourListenerMethod;
   Show();
end;

If you can't get to the forum and need the additional code, leave a comment and I'll post the missing pieces.

Good luck

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