我正在尝试通过单击另一个窗口将鼠标输入发送到一个窗口。根据使用 PostMessage 模拟按键仅适用于某些应用程序?,我正在尝试找到将事件发送到的正确句柄。这是我到目前为止所拥有的:
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;