Windows鼠标键盘模拟函数封装
#include <WinSock2.h>
#pragma comment(lib, "ws2_32")
#include <CommCtrl.h>
typedef UINT (WINAPI*PFN_SendInput)(
_In_ UINT cInputs, // number of input in the array
_In_reads_(cInputs) LPINPUT pInputs, // array of inputs
_In_ int cbSize); // sizeof(INPUT)
PFN_SendInput fnSendInput = (PFN_SendInput)GetProcAddress(GetModuleHandle(TEXT("USER32")), ("SendInput"));
void DummyClick(int dx, int dy, DWORD dwDown = MOUSEEVENTF_LEFTDOWN, DWORD dwUp = MOUSEEVENTF_LEFTUP)
{
INPUT Input = { 0 };
int CXFS = ;
int CYFS = ;
Input.type = INPUT_MOUSE; //鼠标消息
Input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE; //鼠标移动事件 + 绝对位置
//input.mi.dx = (LONG)(double)((double)ptScr.x * (65535.0 / (double)((double)GetSystemMetrics(SM_CXSCREEN) - 1.0)));
//input.mi.dy = (LONG)(double)((double)ptScr.y * (65535.0 / (double)((double)GetSystemMetrics(SM_CYSCREEN) - 1.0)));
Input.mi.dx = (LONG)(double)((double)dx * (65536.0 / (double)GetSystemMetrics(SM_CXSCREEN)));
Input.mi.dy = (LONG)(double)((double)dy * (65536.0 / (double)GetSystemMetrics(SM_CYSCREEN)));
fnSendInput(1, &Input, sizeof(INPUT));
// left down
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = dwDown;
fnSendInput(1, &Input, sizeof(INPUT));
Sleep(16);
// left up
ZeroMemory(&Input, sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = dwUp;
fnSendInput(1, &Input, sizeof(INPUT));
}
void MouseLeftClick(int dx, int dy)
{
DummyClick(dx, dy, MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP);
}
void MouseRightClick(int dx, int dy)
{
DummyClick(dx, dy, MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_RIGHTUP);
}