【发布时间】:2012-12-31 09:57:18
【问题描述】:
我试图通过单击另一个窗口将鼠标输入发送到一个窗口。根据Simulating key press with PostMessage only works in some applications?,我正在尝试找到正确的句柄来发送事件。这是我目前所拥有的:
case WM_LBUTTONDOWN:
GetCursorPos( reinterpret_cast<POINT *>( &mousePosition ) );
hWnd = WindowFromPoint(mousePosition);
threadID = GetWindowThreadProcessId(otherWindow, &procID);
GetGUIThreadInfo(threadID, ¤tWindowGuiThreadInfo);
otherWindow = currentWindowGuiThreadInfo.hwndFocus;
ScreenToClient(hWnd, &mousePosition);
ClientToScreen(otherWindow, &mousePosition);
ScreenToClient(otherWindow, &mousePosition);
dw = MAKELPARAM(mousePosition.x, mousePosition.y);
PostMessage(otherWindow, WM_LBUTTONDOWN, MK_LBUTTON, dw);
break;
在找到这个线程之前,我正在向 WindowFromPoint 返回的句柄发送 PostMessage 并使用 Spy++ 我可以看到消息通过(但这种方法仅适用于某些窗口)。但是现在GetGuiThreadInfo返回错误码87(参数不正确)。我将 currentWindowGuiThreadInfo.cbSize 设置为 sizeof(GUITHREADINFO)。我哪里错了?请帮忙。我在 Windows 7 64 位和 Visual Studio 2010 上使用 Visual C++、win32。
非常感谢!
编辑
很抱歉没有清楚地回答。这是代码的更完整版本:
POINT mousePosition;
DWORD dw, procID, threadID;
HWND hWnd;
GUITHREADINFO currentWindowGuiThreadInfo;
currentWindowGuiThreadInfo.cbSize = sizeof(GUITHREADINFO);
INPUT Input={0};
HWND hw;
int e;
int xPos, yPos;
MSLLHOOKSTRUCT *mouseParameters = (MSLLHOOKSTRUCT*)lParam;
int a = 2;
if (nCode == HC_ACTION) {
switch(wParam) {
case WM_LBUTTONDOWN:
GetCursorPos( reinterpret_cast<POINT *>( &mousePosition ) );
hWnd = WindowFromPoint(mousePosition);
threadID = GetWindowThreadProcessId(otherWindow, &procID); //otherWindow exists and I can see the proper threadID
GetGUIThreadInfo(threadID, ¤tWindowGuiThreadInfo); //currentWindowGuiThreadInfo returns null for all the handles. The cbSize is 48. But no error is returned. The return value is 1
otherWindow= currentWindowGuiThreadInfo.hwndFocus;
ScreenToClient(hWnd, &mousePosition);
ClientToScreen(otherWindow, &mousePosition);
ScreenToClient(otherWindow, &mousePosition);
dw = MAKELPARAM(mousePosition.x, mousePosition.y);
PostMessage(otherWindow, WM_LBUTTONDOWN, MK_LBUTTON, dw);
break;
【问题讨论】:
-
显示
threadID的定义和currentWindowGuiThreadInfo的定义和设置。
标签: winapi visual-c++