- usingSystem;
- usingSystem.Threading;
- usingSystem.Runtime.InteropServices;
- usingSystem.Windows.Forms;
- namespaceMouseControl
- {
- classMouseControl
- {
- ///<summary>
- ///鼠标控制参数
- ///</summary>
- constintMOUSEEVENTF_LEFTDOWN=0x2;
- constintMOUSEEVENTF_LEFTUP=0x4;
- constintMOUSEEVENTF_MIDDLEDOWN=0x20;
- constintMOUSEEVENTF_MIDDLEUP=0x40;
- constintMOUSEEVENTF_MOVE=0x1;
- constintMOUSEEVENTF_ABSOLUTE=0x8000;
- constintMOUSEEVENTF_RIGHTDOWN=0x8;
- constintMOUSEEVENTF_RIGHTUP=0x10;
- ///<summary>
- ///鼠标的位置
- ///</summary>
- publicstructPONITAPI
- {
- publicintx,y;
- }
- [DllImport("user32.dll")]
- publicstaticexternintGetCursorPos(refPONITAPIp);
- [DllImport("user32.dll")]
- publicstaticexternintSetCursorPos(intx,inty);
- [DllImport("user32.dll")]
- publicstaticexternintmouse_event(intdwFlags,intdx,intdy,intcButtons,intdwExtraInfo);
- [STAThread]
- staticvoidMain()
- {
- PONITAPIp=newPONITAPI();
- GetCursorPos(refp);
- Console.WriteLine("鼠标现在的位置X:{0},Y:{1}",p.x,p.y);
- Console.WriteLine("Sleep1sec...");
- Thread.Sleep(1000);
- p.x=(newRandom()).Next(Screen.PrimaryScreen.Bounds.Width);
- p.y=(newRandom()).Next(Screen.PrimaryScreen.Bounds.Height);
- Console.WriteLine("把鼠标移动到X:{0},Y:{1}",p.x,p.y);
- SetCursorPos(p.x,p.y);
- GetCursorPos(refp);
- Console.WriteLine("鼠标现在的位置X:{0},Y:{1}",p.x,p.y);
- Console.WriteLine("Sleep1sec...");
- Thread.Sleep(1000);
- Console.WriteLine("在X:{0},Y:{1}按下鼠标左键",p.x,p.y);
- mouse_event(MOUSEEVENTF_LEFTDOWN,p.x,p.y,0,0);
- Console.WriteLine("Sleep1sec...");
- Thread.Sleep(1000);
- Console.WriteLine("在X:{0},Y:{1}释放鼠标左键",p.x,p.y);
- mouse_event(MOUSEEVENTF_LEFTUP,p.x,p.y,0,0);
- Console.WriteLine("程序结束,按任意键退出....");
- Console.ReadKey();
- }
- }
- }
c# 实现 鼠标控制
最新推荐文章于 2023-08-13 18:50:02 发布