/[NexusPowerControl]/trunk/NexusPowerControl/MouseTrapper.cs
ViewVC logotype

Contents of /trunk/NexusPowerControl/MouseTrapper.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (show annotations) (download)
Sun Oct 23 00:44:44 2011 UTC (7 years, 11 months ago) by william
File size: 2713 byte(s)
*** fix issues with haveing to click the form once, when trapping the mouse, before the form can recieve mouse clicks

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 using System.Drawing;
7 using System.Reflection;
8 using System.Runtime.InteropServices;
9
10 namespace NexusPowerControl
11 {
12 public class MouseTrapper
13 {
14 // code paritally obtained from: http://stackoverflow.com/questions/5399000/lock-mouse-cursor-movement-only-in-a-control
15 // and here: http://www.gamedev.net/topic/321029-how-to-simulate-a-mouse-click-in-c/
16 // and here: http://pinvoke.net/default.aspx/user32.mouse_event
17
18 [DllImport("user32.dll")]
19 static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
20 [Flags]
21 public enum MouseEventFlags : uint
22 {
23 LEFTDOWN = 0x00000002,
24 LEFTUP = 0x00000004,
25 MIDDLEDOWN = 0x00000020,
26 MIDDLEUP = 0x00000040,
27 MOVE = 0x00000001,
28 ABSOLUTE = 0x00008000,
29 RIGHTDOWN = 0x00000008,
30 RIGHTUP = 0x00000010,
31 WHEEL = 0x00000800,
32 XDOWN = 0x00000080,
33 XUP = 0x00000100
34 }
35 //Use the values of this enum for the 'dwData' parameter
36 //to specify an X button when using MouseEventFlags.XDOWN or
37 //MouseEventFlags.XUP for the dwFlags parameter.
38 public enum MouseEventDataXButtons : uint
39 {
40 XBUTTON1 = 0x00000001,
41 XBUTTON2 = 0x00000002
42 }
43
44 public MouseTrapper()
45 {
46 }
47
48
49 public void TrapMouse(Control c)
50 {
51 RaiseClick(c);
52 Cursor.Position = new Point(c.Location.X+45, c.Location.Y+35);
53 Rectangle rc = new Rectangle(Cursor.Position, new Size(c.DisplayRectangle.Width - 105, c.DisplayRectangle.Height-85));
54 Cursor.Clip = rc;
55
56 c.Capture = true;
57 }
58 public void ReleaseMouse(Control c)
59 {
60 c.Capture = false;
61 c.Cursor = Cursors.Default;
62 Cursor.Clip = new Rectangle(0, 0, 0, 0);
63 }
64
65 private void RaiseClick(Control c)
66 {
67 //Type t = typeof(Control);
68 //object[] p = new object[1];
69 //p[0] = EventArgs.Empty;
70 //MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
71 //m.Invoke(c, p);
72 Cursor.Position = new System.Drawing.Point(c.Location.X, c.Location.Y);
73 mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
74 mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
75 }
76 }
77 }
78

  ViewVC Help
Powered by ViewVC 1.1.22