如何使用SendInput在x,y坐标上模拟双击鼠标(我知道此窗口的句柄)?
答案 0 :(得分:14)
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));
}
您可以使用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
答案 1 :(得分:4)
这将模拟某个坐标处的双击
SetCursorPos(X,Y);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,0,0,0,0);
答案 2 :(得分:2)
您可以指定您的目标(最佳)或方法,但如果您尝试同时指定两者,那么答案往往是“那不会那样”。
SendInput
不能像那样工作,它模拟屏幕上的鼠标活动,它将被传递到该位置可见的任何窗口(或鼠标捕获),而不是您选择的窗口。 / p>
要双击特定窗口,请尝试PostMessage(hwnd, WM_LBUTTONDBLCLK, 0, MAKEDWORD(x, y))
。
答案 3 :(得分:1)
this website中有一段代码。尝试连续两次使用函数LeftClick()
。根据{{3}},这就是诀窍。
答案 4 :(得分:0)
因为我的声誉'还不够高(还),我想对#fardjad的解决方案发表评论:它很有用,但有人可能会在" main"例程:
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);