![](/img/trans.png)
[英]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.