C# 鼠标操作类

  1 ContractedBlock.gif ExpandedBlockStart.gif #region
  2
  3using System;
  4using System.Runtime.InteropServices;
  5
  6#endregion

  7
  8 namespace  Windows.Forms.Base
  9 ExpandedBlockStart.gifContractedBlock.gif {
 10    public class Mouse
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 12ContractedSubBlock.gifExpandedSubBlockStart.gif        MouseEventFlag enum#region MouseEventFlag enum
 13
 14        [Flags]
 15        public enum MouseEventFlag : uint
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 17            Move = 0x0001,
 18            LeftDown = 0x0002,
 19            LeftUp = 0x0004,
 20            RightDown = 0x0008,
 21            RightUp = 0x0010,
 22            MiddleDown = 0x0020,
 23            MiddleUp = 0x0040,
 24            XDown = 0x0080,
 25            XUp = 0x0100,
 26            Wheel = 0x0800,
 27            VirtualDesk = 0x4000,
 28            Absolute = 0x8000
 29        }

 30
 31        #endregion

 32
 33        internal const byte SM_CMOUSEBUTTONS = 43;
 34        internal const byte SM_MOUSEPRESENT = 19;
 35        internal const byte SM_MOUSEWHEELPRESENT = 75;
 36
 37        public static int FullScreenPosition_X
 38ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 39            get
 40ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 41                POINTAPI _POINTAPI = new POINTAPI();
 42                GetCursorPos(ref _POINTAPI);
 43                return _POINTAPI.x;
 44            }

 45        }

 46
 47        public static int FullScreenPosition_Y
 48ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 49            get
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 51                POINTAPI _POINTAPI = new POINTAPI();
 52                GetCursorPos(ref _POINTAPI);
 53                return _POINTAPI.y;
 54            }

 55        }

 56
 57        public static string Type
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 59            get
 60ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 61                if (GetSystemMetrics(SM_MOUSEPRESENT) == 0)
 62ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 63                    return "本计算机尚未安装鼠标";
 64                }

 65                if (GetSystemMetrics(SM_MOUSEWHEELPRESENT) != 0)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 67                    return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键滚轮鼠标";
 68                }

 69                return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键鼠标";
 70            }

 71        }

 72
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 74        ///鼠标左右键功能互换
 75        /// </summary>
 76        /// <param name="bSwap"></param>
 77        /// <returns></returns>

 78        [DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
 79        public static extern int SwapMouseButton(int bSwap);
 80
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 82        /// 鼠标的移动区域限制
 83        /// 0为释放限制
 84        /// </summary>
 85        /// <param name="lpRect"></param>
 86        /// <returns></returns>

 87        [DllImport("user32", EntryPoint = "ClipCursor")]
 88        public static extern int ClipCursor(ref RECT lpRect);
 89
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 91        /// 获取鼠标坐标
 92        /// </summary>
 93        /// <param name="lpPoint"></param>
 94        /// <returns></returns>

 95        [DllImport("user32.dll", EntryPoint = "GetCursorPos")]
 96        public static extern int GetCursorPos(ref POINTAPI lpPoint);
 97
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 99        /// 显示和隐藏鼠标指针.  
100        /// 1为显示0为隐藏 
101        /// </summary>
102        /// <param name="bShow"></param>
103        /// <returns></returns>

104        [DllImport("user32.dll", EntryPoint = "ShowCursor")]
105        public static extern bool ShowCursor(bool bShow);
106
107ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
108        /// 将非模态窗口显示为模态窗口
109        /// </summary>
110        /// <param name="hwnd"></param>
111        /// <param name="fEnable"></param>
112        /// <returns></returns>

113        [DllImport("user32.dll", EntryPoint = "EnableWindow")]
114        public static extern int EnableWindow(int hwnd, int fEnable);
115
116ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
117        /// 获得窗口的大小
118        /// </summary>
119        /// <param name="hwnd"></param>
120        /// <param name="lpRect"></param>
121        /// <returns></returns>

122        [DllImport("user32.dll", EntryPoint = "GetWindowRect")]
123        public static extern int GetWindowRect(int hwnd, ref RECT lpRect);
124
125ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
126        /// 设置鼠标坐标
127        /// </summary>
128        /// <param name="x"></param>
129        /// <param name="y"></param>
130        /// <returns></returns>

131        [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
132        public static extern int SetCursorPos(int x, int y);
133
134ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
135        /// 返回Win桌面中各种显示单元的宽度和高度、是否安装了鼠标、是否调换了鼠标左右键的定义等
136        /// </summary>
137        /// <param name="nIndex"></param>
138        /// <returns></returns>

139        [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
140        public static extern int GetSystemMetrics(int nIndex);
141
142ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
143        /// 参数wCount,表示鼠标双击时间,为毫秒级,系统默认时间为500
144        /// </summary>
145        /// <param name="wCount"></param>
146        /// <returns></returns>

147        [DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
148        public static extern int SetDoubleClickTime(int wCount);
149
150ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
151        /// 该函数无参数;它的返回值为毫秒,为双击鼠标双击有效的时间间隔。
152        /// </summary>
153        /// <returns></returns>

154        [DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
155        public static extern int GetDoubleClickTime();
156
157ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
158        /// 只是休眠而已
159        /// </summary>
160        /// <param name="dwMilliseconds"></param>

161        [DllImport("kernel32.DLL", EntryPoint = "Sleep")]
162        public static extern void Sleep(int dwMilliseconds);
163
164ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
165        /// 模拟鼠标操作
166        /// 调用方法
167        /// mouse_event(MOUSEEVENTF_LEFTDOWN, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
168        /// mouse_event(MOUSEEVENTF_LEFTUP, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
169        ///    其中X,Y分别是你要点击的点的横坐标和纵坐标 
170        /// </summary>
171        /// <param name="dwFlags"></param>
172        /// <param name="dx"></param>
173        /// <param name="dy"></param>
174        /// <param name="dwData"></param>
175        /// <param name="dwExtraInfo"></param>

176        [DllImport("user32.dll")]
177        public static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
178
179        // 隐藏 显示 鼠标
180        public static void Hide()
181ExpandedSubBlockStart.gifContractedSubBlock.gif        {
182            ShowCursor(false);
183        }

184
185        public static void Show()
186ExpandedSubBlockStart.gifContractedSubBlock.gif        {
187            ShowCursor(true);
188        }

189
190        // 将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了
191        public static void Lock(System.Windows.Forms.Form ObjectForm)
192ExpandedSubBlockStart.gifContractedSubBlock.gif        {
193            RECT _FormRect = new RECT();
194            GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);
195            ClipCursor(ref _FormRect);
196        }

197
198        public static void UnLock()
199ExpandedSubBlockStart.gifContractedSubBlock.gif        {
200            RECT _ScreenRect = new RECT();
201            _ScreenRect.top = 0;
202            _ScreenRect.left = 0;
203            _ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
204            _ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;
205            ClipCursor(ref _ScreenRect);
206        }

207
208        // 鼠标失效,不过失效的好像不只是鼠标,小心哦
209        public static void Disable(System.Windows.Forms.Form ObjectForm)
210ExpandedSubBlockStart.gifContractedSubBlock.gif        {
211            EnableWindow(ObjectForm.Handle.ToInt32(), 0);
212        }

213
214        public static void Enable(System.Windows.Forms.Form ObjectForm)
215ExpandedSubBlockStart.gifContractedSubBlock.gif        {
216            EnableWindow(ObjectForm.Handle.ToInt32(), 1);
217        }

218
219        // 鼠标自己移动
220        public static void Move(int From_Handle_ToInt32, int To_Handle_ToInt32)
221ExpandedSubBlockStart.gifContractedSubBlock.gif        {
222            RECT rectFrom = new RECT();
223            RECT rectTo = new RECT();
224            int i;
225            GetWindowRect(From_Handle_ToInt32, ref rectFrom);
226            GetWindowRect(To_Handle_ToInt32, ref rectTo);
227            if ((rectFrom.left + rectFrom.right)/2 - (rectTo.left + rectTo.right)/2 > 0)
228ExpandedSubBlockStart.gifContractedSubBlock.gif            {
229                for (i = (rectFrom.left + rectFrom.right)/2; i >= (rectTo.left + rectTo.right)/2; i--)
230ExpandedSubBlockStart.gifContractedSubBlock.gif                {
231                    SetCursorPos(i, (rectFrom.top + rectFrom.bottom)/2);
232                    Sleep(1);
233                }

234            }

235            else
236ExpandedSubBlockStart.gifContractedSubBlock.gif            {
237                for (i = (rectFrom.left + rectFrom.right)/2; i <= (rectTo.left + rectTo.right)/2; i++)
238ExpandedSubBlockStart.gifContractedSubBlock.gif                {
239                    SetCursorPos(i, (rectFrom.top + rectFrom.bottom)/2);
240                    Sleep(1);
241                }

242            }

243            if ((rectFrom.top + rectFrom.bottom)/2 - (rectTo.top + rectTo.bottom)/2 > 0)
244ExpandedSubBlockStart.gifContractedSubBlock.gif            {
245                for (i = (rectFrom.top + rectFrom.bottom)/2; i >= (rectTo.top + rectTo.bottom)/2; i--)
246ExpandedSubBlockStart.gifContractedSubBlock.gif                {
247                    SetCursorPos((rectTo.left + rectTo.right)/2, i);
248                    Sleep(1);
249                }

250            }

251            else
252ExpandedSubBlockStart.gifContractedSubBlock.gif            {
253                for (i = (rectFrom.top + rectFrom.bottom)/2; i <= (rectTo.top + rectTo.bottom)/2; i++)
254ExpandedSubBlockStart.gifContractedSubBlock.gif                {
255                    SetCursorPos((rectTo.left + rectTo.right)/2, i);
256                    Sleep(1);
257                }

258            }

259        }

260
261        // 得到你的鼠标类型
262
263        // 设置鼠标双击时间
264        public static void DoubleClickTime_Set(int MouseDoubleClickTime)
265ExpandedSubBlockStart.gifContractedSubBlock.gif        {
266            SetDoubleClickTime(MouseDoubleClickTime);
267        }

268
269        public static string DoubleClickTime_Get()
270ExpandedSubBlockStart.gifContractedSubBlock.gif        {
271            return GetDoubleClickTime().ToString();
272        }

273
274        // 设置鼠标默认主键
275        public static void DefaultRightButton()
276ExpandedSubBlockStart.gifContractedSubBlock.gif        {
277            SwapMouseButton(1);
278        }

279
280        public static void DefaultLeftButton()
281ExpandedSubBlockStart.gifContractedSubBlock.gif        {
282            SwapMouseButton(0);
283        }

284
285ContractedSubBlock.gifExpandedSubBlockStart.gif        Nested type: POINTAPI#region Nested type: POINTAPI
286
287        public struct POINTAPI
288ExpandedSubBlockStart.gifContractedSubBlock.gif        {
289            public int x;
290            public int y;
291        }

292
293        #endregion

294
295ContractedSubBlock.gifExpandedSubBlockStart.gif        Nested type: RECT#region Nested type: RECT
296
297        public struct RECT
298ExpandedSubBlockStart.gifContractedSubBlock.gif        {
299            public int bottom;
300            public int left;
301            public int right;
302            public int top;
303        }

304
305        #endregion

306    }

307}

转载于:https://www.cnblogs.com/guozk/archive/2009/08/02/1537050.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#提供了一个`System.Windows.Forms`命名空间,其中包含了许多与用户界面相关的及方法,可以用来模拟键盘鼠标操作。 下面是一些常用的模拟键盘鼠标操作的方法: 1. 模拟按键操作 可以使用`SendKeys`中的`Send`方法来模拟按键操作。例如,要模拟按下“a”键,可以使用以下代码: ``` SendKeys.Send("a"); ``` 2. 模拟组合键操作 如果要模拟组合键操作,例如Ctrl+C复制操作,可以将组合键的按键值用“+”连接起来,如下所示: ``` SendKeys.Send("^c"); ``` 其中,“^”表示Ctrl键,“+”表示Shift键,“%”表示Alt键,“{F1}”表示F1键,等等。 3. 模拟鼠标点击操作 可以使用`System.Windows.Forms.Cursor`中的`Position`属性来获取当前鼠标的位置,然后使用`System.Windows.Forms.Mouse`中的`LeftClick`或`RightClick`方法来模拟鼠标左键或右键点击操作。例如,要模拟在屏幕上(100,100)的位置进行左键点击操作,可以使用以下代码: ``` Cursor.Position = new Point(100, 100); Mouse.LeftClick(); ``` 4. 模拟鼠标移动操作 可以使用`System.Windows.Forms.Cursor`中的`Position`属性来设置鼠标的位置,从而模拟鼠标移动操作。例如,要将鼠标移到屏幕上(200,200)的位置,可以使用以下代码: ``` Cursor.Position = new Point(200, 200); ``` 需要注意的是,模拟键盘鼠标操作可能会对系统产生影响,因此在使用时要谨慎,并且尽量避免在用户不知情的情况下进行操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值