C++ 发送鼠标点击消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12363215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send mouse Click message
提问by zoli
How we can make mouse click event in a certain position without moving mouse(I mean that make computer think a position is clicked with mouse) with C++
我们如何在不移动鼠标的情况下在某个位置产生鼠标点击事件(我的意思是让计算机认为一个位置是用鼠标点击的)用 C++
回答by sandyiscool
The SendInput
function in windows API will get you started. Have a look at the following link for the definition of the method,its input parameters and return values :
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
SendInput
Windows API 中的函数将帮助您入门。有关该方法的定义、其输入参数和返回值,请查看以下链接:http:
//msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85)。 aspx
You can follow the links in the page to know more about the structures and data-types used in the function.
您可以按照页面中的链接了解有关函数中使用的结构和数据类型的更多信息。
Update :
You can start with something like this
更新:你可以从这样的事情开始
#include<Windows.h>
int main()
{
INPUT input;
input.type=INPUT_MOUSE;
input.mi.dx=0;
input.mi.dy=0;
input.mi.dwFlags=(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE|MOUSEEVENTF_RIGHTDOWN|MOUSEEVENTF_RIGHTUP);
input.mi.mouseData=0;
input.mi.dwExtraInfo=NULL;
input.mi.time=0;
SendInput(1,&input,sizeof(INPUT));
return 0;
}
This will automatically move your mouse to the top left corner of the screen and make a right-click. Now, if you mean to make a click somewhere on the screen without moving your mouse, I think that is not possible using SendInput(). You do not need to worry about moving the mouse as your program will do it by itself. That is what the 'MOUSEEVENTF_MOVE' flag tells the program to do. If you do not use the flag, then the click will take place at the current position of your mouse.
这将自动将您的鼠标移动到屏幕的左上角并进行右键单击。现在,如果您想在不移动鼠标的情况下单击屏幕上的某处,我认为使用 SendInput() 是不可能的。您无需担心移动鼠标,因为您的程序会自行完成。这就是“MOUSEEVENTF_MOVE”标志告诉程序要做的事情。如果您不使用该标志,则单击将发生在您鼠标的当前位置。
回答by SingerOfTheFall
You can use SendInput()
functionto emulate mouse clicks and keyboard strokes:
您可以使用SendInput()
函数来模拟鼠标点击和键盘敲击:
The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream. These events are not interspersed with other keyboard or mouse input events inserted either by the user (with the keyboard or mouse) or by calls to keybd_event, mouse_event, or other calls to SendInput.
SendInput 函数将 INPUT 结构中的事件串行插入键盘或鼠标输入流中。这些事件不会与由用户(使用键盘或鼠标)或通过调用 keybd_event、mouse_event 或其他调用 SendInput 插入的其他键盘或鼠标输入事件穿插在一起。
You can also use SendMessage()
or PostMessage()
to send the button-press message, e.g.
您还可以使用SendMessage()
或PostMessage()
发送按钮消息,例如
SendMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(mousePosX, mousePosY));
, but it is less reliable. Note that in this way you may also need to send WM_LBUTTONUP
later, depending on the way you handle events in your application.
,但可靠性较低。请注意,通过这种方式,您可能还需要WM_LBUTTONUP
稍后发送,具体取决于您在应用程序中处理事件的方式。
Also check thisquestion, the accepted answer is pretty detailed.
还要检查这个问题,接受的答案非常详细。