gpt4 book ai didi

c++ - 如何用C++ move 鼠标

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:38 24 4
gpt4 key购买 nike

我想用 C++ 脚本 move 鼠标光标。我在 Parallels 中的 Windows 7 中使用 Visual C++ 2010 Express,并创建了一个控制台应用程序。

我知道 SetCursorPos 方法,但它不起作用(它什么都不做)。

我设法用 SendInput 模拟了点击,但它实际上并没有 move 鼠标。

这是我的代码:

#include <Windows.h>
#include <Tlhelp32.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <time.h>

void mouseLeftClick(const int x, const int y);

// window
HWND hWindow;

int main()
{
// find window
hWindow = FindWindow(NULL, "Calculadora");

if (NULL == hWindow) {
OutputDebugStringA("Couldn't find application.");
}else{

if (!SetForegroundWindow(hWindow)) {
OutputDebugStringA("Couldn't set application to foreground.");
}else{
// click on 1
mouseLeftClick(20 265));
Sleep(500);
// click on 2
mouseLeftClick(60, 265);
Sleep(500);
}
}
return 0;
}

void mouseLeftClick(const int x, const int y)
{
// get the window position
RECT rect;
GetWindowRect(hWindow, &rect);

// calculate scale factor
const double XSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CXSCREEN) - 1);
const double YSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CYSCREEN) - 1);

// get current position
POINT cursorPos;
GetCursorPos(&cursorPos);
double cx = cursorPos.x * XSCALEFACTOR;
double cy = cursorPos.y * YSCALEFACTOR;

// calculate target position relative to application
double nx = (x + rect.left) * XSCALEFACTOR;
double ny = (y + rect.top) * YSCALEFACTOR;

INPUT Input={0};
Input.type = INPUT_MOUSE;

Input.mi.dx = (LONG)nx;
Input.mi.dy = (LONG)ny;

// set move cursor directly and left click
Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;

SendInput(1,&Input,sizeof(INPUT));
}

最佳答案

这发生在 Parallels 中,因为 SmartMouse 处于开启或自动状态。为了让 Parallels VM 中的程序使用 SetCursorPos 控制鼠标,您需要先隐藏光标。您可以在执行任何鼠标 move 之前使用 ShowCursor(0); 执行此操作,例如设置光标位置。现在,当 SmartMouse 设置为“自动”或“关闭”时,您将能够控制鼠标。

关于c++ - 如何用C++ move 鼠标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11276233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com