windows 我如何使用 SendInput 在 x、y 坐标上模拟双击窗口(我知道句柄)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5789843/
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
How i can simulate a double mouse click on window ( i khow handle) on x, y coordinate, using SendInput?
提问by G-71
How i can simulate a double mouse click on window ( i know handle of this window) on x, y coordinate, using SendInput?
我如何使用 SendInput 在 x、y 坐标上模拟双击窗口(我知道这个窗口的句柄)?
回答by fardjad
void DoubleClick(int x, int y)
{
const double XSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CXSCREEN) - 1);
const double YSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CYSCREEN) - 1);
POINT cursorPos;
GetCursorPos(&cursorPos);
double cx = cursorPos.x * XSCALEFACTOR;
double cy = cursorPos.y * YSCALEFACTOR;
double nx = x * XSCALEFACTOR;
double ny = y * YSCALEFACTOR;
INPUT Input={0};
Input.type = INPUT_MOUSE;
Input.mi.dx = (LONG)nx;
Input.mi.dy = (LONG)ny;
Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
SendInput(1,&Input,sizeof(INPUT));
SendInput(1,&Input,sizeof(INPUT));
Input.mi.dx = (LONG)cx;
Input.mi.dy = (LONG)cy;
Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
SendInput(1,&Input,sizeof(INPUT));
}
You can use GetWindowRect() to get the window position from its handle and pass relative x
and y
to DoubleClick function:
您可以使用 GetWindowRect() 从其句柄获取窗口位置并将相对x
和传递y
给 DoubleClick 函数:
RECT rect;
GetWindowRect(hwnd, &rect);
HWND phwnd = GetForegroundWindow();
SetForegroundWindow(hwnd);
DoubleClick(rect.left + x, rect.top + y);
SetForegroundWindow(phwnd); // To activate previous window
回答by McCormick32
This will simulate a double click at certain coordinates
这将模拟在某些坐标处双击
SetCursorPos(X,Y);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,0,0,0,0);
回答by Ben Voigt
You can specify your goal (best) or the method, but if you try to specify both, more often than not the answer is going to be "that doesn't work like that".
您可以指定您的目标(最佳)或方法,但如果您尝试指定两者,则答案通常是“那行不通”。
SendInput
doesn't work like that, it simulates mouse activity on the screen, which will be delivered to whatever window is visible at that location (or has mouse capture), not the window of your choice.
SendInput
不像那样工作,它模拟屏幕上的鼠标活动,它将被传送到该位置可见的任何窗口(或有鼠标捕获),而不是您选择的窗口。
To deliver a double-click to a specific window, try PostMessage(hwnd, WM_LBUTTONDBLCLK, 0, MAKEDWORD(x, y))
.
要双击特定窗口,请尝试PostMessage(hwnd, WM_LBUTTONDBLCLK, 0, MAKEDWORD(x, y))
。
回答by Vite Falcon
There's a piece of code in this website. Try using the function LeftClick()
twice in succession. That does the trick according to this guy.
回答by Badt_Paul
Since my 'reputation' is not high enough (yet), I'd like to comment on #fardjad's solution: it works great, but one might add following to the "main" routine:
由于我的“声誉”还不够高(还),我想评论 #fardjad 的解决方案:它很好用,但可能会在“主要”例程中添加以下内容:
SetForegroundWindow(hwnd);
SetCursorPos(rect.left + x, rect.top + y);
// which shows your current mouseposition...
// during my testing, I used a _getch() so that I actually could verify it
Sleep(nWinSleep);
// delay the mouseclick, as window might not get to foreground quick enough;
took me awhile to figure this one out...
DoubleClick(rect.left + x, rect.top + y);