windows PostMessage 用于跨应用程序消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1414187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PostMessage for cross application messages
提问by Ryan Kearney
I'm trying to send a keystroke to another application. I can successfully find the window handle since using SendMessage worked exactly as intended.
我正在尝试向另一个应用程序发送击键。我可以成功找到窗口句柄,因为使用 SendMessage 完全按预期工作。
However, when I switched the SendMessage over to PostMessage, the application no longer received the messages.
但是,当我将 SendMessage 切换到 PostMessage 时,应用程序不再接收消息。
I did, however, find a workaround by using HWND_BROADCAST as the window handle, and it works fine, but isn't the ideal way to go about it.
但是,我确实通过使用 HWND_BROADCAST 作为窗口句柄找到了一种解决方法,它工作正常,但不是理想的方法。
What I'm asking is, I have a valid hWnd, how can I send it messages using PostMessage and not SendMessage?
我要问的是,我有一个有效的 hWnd,如何使用 PostMessage 而不是 SendMessage 向它发送消息?
EditThis is what I'm trying to do.
编辑这就是我想要做的。
HWND Target = FindWindow(0, "Window Title Goes Here");
LPARAM lParam = (1 | (57<<16)); // OEM Code and Repeat for WM_KEYDOWN
WPARAM wParam = VK_SPACE;
PostMessage(HWND_BROADCAST, WM_KEYDOWN, wParam, lParam); // Works
PostMessage(Target, WM_KEYDOWN, wParam, lParam); // Doesn't Work
SendMessage(Target, WM_KEYDOWN, wParam, lParam); // Works, but I need Post
回答by Kirill V. Lyadvinsky
The PostMessage
function does not work when the message numbers between 0 and WM_USER
-1. Use RegisterWindowMessagefunction to register your own messages.
PostMessage
当消息编号介于 0 和WM_USER
-1之间时,该功能不起作用。使用RegisterWindowMessage函数注册您自己的消息。
回答by shf301
Sent messages and posted messages take completely different routeres. Target is recieving your posted message, it's just either filtering or dispatching it to another window. It gets to do what ever it wants with it. When you send the messages, it goes directly to the window procedure without filtering, so is most likely that cause of that issue.
发送消息和发布消息采用完全不同的路由器。Target 正在接收您发布的消息,它只是将其过滤或分派到另一个窗口。它可以随心所欲地做任何事情。当您发送消息时,它直接进入窗口过程而不进行过滤,因此很可能是该问题的原因。
I don't know why HWND_BROADCAST is working; my best guess is that a window other than Target is processing the message. Or maybe its even being sent to a different window than Target. (You do realize that HWND_BROADCAST sends the messages to every top level window)
我不知道为什么 HWND_BROADCAST 工作;我最好的猜测是 Target 以外的窗口正在处理消息。或者它甚至可能被发送到与 Target 不同的窗口。(您确实意识到 HWND_BROADCAST 将消息发送到每个顶级窗口)
There is a Win32 API function designed to send input, SendInput(), that places the messages on the input queue just like a user keypress. However this doesn't let you specify a window, it sends its input to the active window. To use it you would have to activate and switch focus to Target, which means the user would see that window move to the top (just like you Alt-Tabbed to it). Along that same route VBScript has a SendKeys()function that does the same thing, but is easier to use.
有一个 Win32 API 函数旨在发送输入,SendInput(),它将消息放在输入队列中,就像用户按键一样。但是,这不会让您指定窗口,而是将其输入发送到活动窗口。要使用它,您必须激活并将焦点切换到目标,这意味着用户会看到该窗口移动到顶部(就像您按 Alt-Tabbed 键一样)。沿着相同的路线,VBScript 有一个SendKeys()函数,它可以做同样的事情,但更容易使用。
As a final alternative you could use SendMessageCallback()which will give you the behavior of an asynchronous SendMessage which is what I assume you want. (And is different than PostMessage. Posted messages go into the posted message queue, sent messages are delivered directly)
作为最后的选择,您可以使用SendMessageCallback(),它会给您提供异步 SendMessage 的行为,这正是我假设您想要的。(和PostMessage不同,发布的消息进入发布的消息队列,发送的消息直接投递)
回答by SebSeb
*For the lparam go here http://msdn.microsoft.com/en-us/library/ms646280%28v=vs.85%29.aspx, change the 32 bits (31...3 2 1 0) of lParam. Once you have the binary sentence you want for your paramaters (cRepeat, Scancode etc), convert it to hexadecimal.
*对于 lparam 去这里http://msdn.microsoft.com/en-us/library/ms646280%28v=vs.85%29.aspx,更改 lParam 的 32 位 (31...3 2 1 0) . 一旦您为参数(cRepeat、Scancode 等)获得了所需的二进制句子,请将其转换为十六进制。
try this :
尝试这个 :
void SendString(HWND h, char *text)
{
int len = strlen(text);
for(int i = 0; i < len; i++)
PostMessage(h, WM_CHAR, text[i], 0);
}
HWND Target = FindWindow(0, "Window Title Goes Here");
LPARAM lParam = //The hexadecimal value matching with the parameters you want* example 0x29A1.
WPARAM wParam = VK_SPACE;
PostMessage(HWND_BROADCAST, WM_KEYDOWN, wParam, lParam);
PostMessage(Target, WM_KEYDOWN, wParam, lParam);
SendString(Target, (char*)"themessageyouwant\n");