简体   繁体   English

我如何使用SendInput在x,y坐标上模拟双击鼠标(i khow handle)?

[英]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));
}

You can use GetWindowRect() to get the window position from its handle and pass relative x and y to DoubleClick function: 您可以使用GetWindowRect()从其句柄获取窗口位置,并将相对xy传递给DoubleClick函数:

RECT rect;
GetWindowRect(hwnd, &rect);

HWND phwnd = GetForegroundWindow();

SetForegroundWindow(hwnd);

DoubleClick(rect.left + x, rect.top + y);

SetForegroundWindow(phwnd); // To activate previous window

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);

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))

There's a piece of code in this website . 这个网站上有一段代码。 Try using the function LeftClick() twice in succession. 尝试连续两次使用LeftClick()函数。 That does the trick according to this guy . 根据这个人的说法, 就是诀窍。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM