我PostMessage
用来将消息从另一个单元发送到主表单,如下所示:
procedure notify(var Message: TMessage); message 1;
在该过程中,根据以下信息显示信息WParam
:
procedure TForm1.notify(var Message: TMessage);
begin
Case (Message.WParam) of
1: memo1.Lines.Add('task started');
2: memo1.Lines.Add('in progress');
end;
end;
在另一个单元中,我发送如下消息:
PostMessage(Handle, 1, 2, variable_info);
首先,什么是消息ID?我用1代替了它,因为它的类型是基数,我应该怎么用呢?而且我的消息永远也不会收到,因为Message.WParam
它永远不会等于1或2。我的代码有什么问题?
我这样编辑代码:unit1
const
WM_MY_MESSAGE = WM_USER + 0;
在代码中,我添加了以下内容:
PostMessage(Handle,WM_MY_MESSAGE, 1,value_sent);
TFormUnit:
private
procedure notify(var Message :TMessage); message WM_MY_MESSAGE;
procedure TFormMain.notify(var Message: TMessage);
begin
Case (Message.WParam)of // which is 1
1:
//------------------------------------------
begin
memo1.Lines.Add('task started');
通常当PostMessage(Handle,WM_MY_MESSAGE,1,value_sent); 执行后,我应该启动消息任务,但是它是相同的错误,什么也没发生!
所述消息ID必须是唯一的。要在应用程序内发送消息,请使用常量WM_USER
(在Messages
单位中声明)作为第一个有效的起始编号。
低于WM_USER
($ 0400)的值保留用于系统定义的消息。从文档:
应用程序不能将这些值用于私人消息。
在表单的界面部分或与应用程序中的所有其他消息一起在一个单元中声明此内容。
const
WM_MY_MESSAGE = WM_USER + 0;
编辑:
除了问题之外,要在您的PostMessage调用中获取表单窗口句柄,
PostMessage(YourForm.Handle,WM_MY_MESSAGE,1,value_sent);
要么
PostMessage(FindWindow(nil,'YourFormName'),WM_MY_MESSAGE,1,value_sent);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句