[英]How I can send a Ctrl + P to window if i khow a handle of this window?
[英]How i can simulate a double mouse click on window ( i khow handle) on x, y coordinate, using SendInput?
我如何使用SendInput在x,y坐标上模拟双击鼠标(我知道这个窗口的句柄)?
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
这将模拟在某些坐标处的双击
SetCursorPos(X,Y);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP,0,0,0,0);
您可以指定您的目标(最佳)或方法,但如果您尝试同时指定两者,那么答案往往是“那不能像那样工作”。
SendInput
不能像这样工作,它模拟屏幕上的鼠标活动,它将被传递到该位置可见的任何窗口(或鼠标捕获),而不是您选择的窗口。
要双击特定窗口,请尝试PostMessage(hwnd, WM_LBUTTONDBLCLK, 0, MAKEDWORD(x, y))
。
由于我的“声誉”还不够高(我),我想对#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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.