This article is original by the author, please indicate the source for reprinting
In general, the mouse_event function can be used to implement mouse operations on the Windows platform
. We use the updated SendInput function here.
bool MouseEventpos(InputData data)
{
INPUT MouseData = {
0 };
MouseData.type = INPUT_MOUSE;
MOUSEINPUT &MouseInput = MouseData.mi;
MouseInput.time = NULL;
MouseInput.dwExtraInfo = NULL;
if (true)//绝对值或相对值
{
MouseInput.dwFlags |= MOUSEEVENTF_ABSOLUTE;
MouseInput.dx = ((x * 1.0f) / (m_width * 1.0f)) * 65535;
MouseInput.dy = ((y * 1.0f) / (m_height * 1.0f)) * 65535;
}
else
{
//TODO:相对值
}
//下面是根据函数传入参数进行判断
if (data.input_data() == MOUSE_WHEEL)
{
MouseInput.dwFlags |= MOUSEEVENTF_WHEEL;
MouseInput.mouseData = data.value();
}
else
{
switch (data.inputstate())
{
case DEFAULT:
{
MouseInput.dwFlags |= MOUSEEVENTF_MOVE;
break;
}
case STATE_DOWN:
{
if ((int)BUTTON_LEFT == data.input_data())
{
MouseInput.dwFlags |= MOUSEEVENTF_LEFTDOWN;
}
else if ((int)BUTTON_MIDDLE == data.input_data())
{
MouseInput.dwFlags |= MOUSEEVENTF_MIDDLEDOWN;
}
else if ((int)BUTTON_RIGHT == data.input_data())
{
MouseInput.dwFlags |= MOUSEEVENTF_RIGHTDOWN;
}
break;
}
case STATE_UP:
{
if ((int)BUTTON_LEFT == data.input_data())
{
MouseInput.dwFlags |= MOUSEEVENTF_LEFTUP;
}
else if ((int)BUTTON_MIDDLE == data.input_data())
{
MouseInput.dwFlags |= MOUSEEVENTF_MIDDLEUP;
}
else if ((int)BUTTON_RIGHT == data.input_data())
{
MouseInput.dwFlags |= MOUSEEVENTF_RIGHTUP;
}
break;
}
default:
break;
}
}
//可以一次性发多个,SendInput第一个参数是发送的结构体个数,第二个是数组指针,详见MSDN
SendInput(1, &MouseData, sizeof(MouseData));
return true;
}