我向外部程序发送消息:
SendMessage(Handle, WM_CHAR, Ord('A'), 0);
但我不知道如何在另一个程序中收到此消息?(使用 WndProc 或类似的东西)有人可以为我解释一下吗?
谢谢
我向外部程序发送消息:
SendMessage(Handle, WM_CHAR, Ord('A'), 0);
但我不知道如何在另一个程序中收到此消息?(使用 WndProc 或类似的东西)有人可以为我解释一下吗?
谢谢
Read How to send and handle a custom Windows message
Untested Code:
const
WM_REFRESHSOMETHING = WM_USER + 6;
On the Form which should catch the Message you need something like:
...
procedure WMRefreshsomething(var ppbMsg : TMessage); message WM_REFRESHSOMETHING;
...
procedure YourForm.WMRefreshsomething(var ppbMsg : TMessage);
begin
//Actions...
end;
And then you can send a message like:
SendMessageToAll(Handle, WM_REFRESHSOMETHING, 0, 0);
To all Forms which have the message WM_REFRESHSOMETHING;
See this compilable Example
我在一系列程序中使用以下代码;一个程序启动程序,当每个程序完成时,它们会向启动器发送一条消息,表明它们已完成。该程序的关键是使用 HWND_BROADCAST 参数:消息被发送到计算机上所有正在运行的程序,但当然它只由具有正确消息处理程序的人处理。在必须发送消息的程序中,写
SendMessage (HWND_BROADCAST, RegisterWindowMessage ('message'), 0, 0);
显然,您可以用某个字符串替换“消息”,该字符串在两个程序中都是常量(发送的和接收的)。
我不推荐使用 SendMessage 的 lparam 参数来将数据传递给接收程序;这应该是一个指针,当然程序1中指向数据的指针将指向程序2中的垃圾。可以将longint转换为指针并发送它,然后必须在接收端取消引用
在接收程序中,
type
TWMMYMessage = record
Msg: Cardinal; // ( first is the message ID )
Handle: HWND; // ( this is the wParam, Handle of sender)
Info: Longint; // ( this is lParam, pointer to our data)
Result: Longint;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
wm_Launcher:= RegisterWindowMessage ('message')
// wm_launcher is a private member of TForm
.
.
.
end;
procedure TForm1.DefaultHandler(var Message);
var
ee: TWMMYMessage;
begin
with TMessage (Message) do
begin
if (Msg = wm_Launcher) then
begin
ee.Msg:= Msg;
ee.Handle:= wParam;
ee.Info:= lParam;
// Checking if this message is not from us
if ee.Handle <> Handle then WmMyMessage(ee);
end
else inherited DefaultHandler(Message);
end;
end;
procedure TForm1.WmMyMessage(var Msg: TWMMYMessage);
begin
startbtnclick (nil) // here we actually do something when the message is received
end;