Delphi自定义消息处理程序

Delphi自定义消息处理程序

本文介绍了Delphi自定义消息处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户双击dbgrid时,将显示非模式形式.

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-定义自定义消息常量:

1 - Define a custom message constant:

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

2-在我的非模式形式的OnClose事件中,我有以下内容:

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-在保存dbGrid的表单的私有声明中,我有以下内容:

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;

完成这些操作后,PostMessage正常运行,但是OnRefreshRequest过程从不运行.我在做什么错了?

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

推荐答案

除了其他答案中的消息名称外,您还在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.

这篇关于Delphi自定义消息处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:09