C#控制鼠标

using System; using System.Threading; using System.Runtime.InteropServices; using System.Windows.Forms; namespace MouseControl { class MouseControl { ///<summary>/// 鼠标控制参数 ///</summary>constint MOUSEEVENTF_LEFTDOWN =0x2; constint MOUSEEVENTF_LEFTUP =0x4; constint MOUSEEVENTF_MIDDLEDOWN =0x20; constint MOUSEEVENTF_MIDDLEUP =0x40; constint MOUSEEVENTF_MOVE =0x1; constint MOUSEEVENTF_ABSOLUTE =0x8000; constint MOUSEEVENTF_RIGHTDOWN =0x8; constint MOUSEEVENTF_RIGHTUP =0x10; ///<summary>/// 鼠标的位置 ///</summary>publicstruct PONITAPI { publicint x, y; } [DllImport("user32.dll")] publicstaticexternint GetCursorPos(ref PONITAPI p); [DllImport("user32.dll")] publicstaticexternint SetCursorPos(int x, int y); [DllImport("user32.dll")] publicstaticexternint mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); [STAThread] staticvoid Main() { PONITAPI p =new PONITAPI(); GetCursorPos(ref p); Console.WriteLine("鼠标现在的位置X:{0}, Y:{1}", p.x, p.y); Console.WriteLine("Sleep 1 sec..."); Thread.Sleep(1000); p.x = (new Random()).Next(Screen.PrimaryScreen.Bounds.Width); p.y = (new Random()).Next(Screen.PrimaryScreen.Bounds.Height); Console.WriteLine("把鼠标移动到X:{0}, Y:{1}", p.x, p.y); SetCursorPos(p.x, p.y); GetCursorPos(ref p); Console.WriteLine("鼠标现在的位置X:{0}, Y:{1}", p.x, p.y); Console.WriteLine("Sleep 1 sec..."); 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("Sleep 1 sec..."); 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(); } } }

转载于:http://115599727.blog.sohu.com/155687389.html

posted @ 2013-07-09 17:47  chenaran  阅读(200)  评论(0)    收藏  举报