c++鼠标点击模拟代码

c++ mouse click simulation code

本文关键字:模拟 代码 鼠标 c++      更新时间:2023-10-16

我很难理解下面的代码。我无法对此做出很好的解释。我在有问题的代码段旁边留下了评论。

  void LeftClick ( )
        {  
          INPUT input = {0};
           // left down 
          input.type      = INPUT_MOUSE;
          input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;
          ::SendInput(1,&input,sizeof(INPUT));
          // left up
          ::ZeroMemory(&input,sizeof(INPUT)); // why zeroMemory? removing this code changes nothing that i can tell
          input.type      = INPUT_MOUSE; // why reset this variable? is it not already set?
          input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;
          ::SendInput(1,&input,sizeof(INPUT));
        }

我在得到这个代码http://forums.codeguru.com/showthread.php?377394-Windows-SDK-User-Interface-How-can-I-Simulate-mouse-events-in-an-application

ZeroMemory函数清除名为input的结构中的所有数据,这就是代码必须重置input.type变量的原因。

文件:http://msdn.microsoft.com/en-us/library/windows/desktop/aa366920(v=vs.85).aspx

实际上,我看了一些我写的旧代码,我根本没有使用ZeroMemory宏。这真的没有必要,因为设置的两个值都会再次重置。