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

Diff of /trunk/NexusPowerControl/MouseTrapper.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/NexusPowerControl/MouseTrapper.cs	2011/10/23 00:29:11	16
+++ trunk/NexusPowerControl/MouseTrapper.cs	2011/10/23 00:44:44	17
@@ -4,30 +4,75 @@
 using System.Text;
 using System.Windows.Forms;
 using System.Drawing;
+using System.Reflection;
+using System.Runtime.InteropServices;
 
 namespace NexusPowerControl
 {
     public class MouseTrapper
     {
         // code paritally obtained from: http://stackoverflow.com/questions/5399000/lock-mouse-cursor-movement-only-in-a-control
+        // and here: http://www.gamedev.net/topic/321029-how-to-simulate-a-mouse-click-in-c/
+        // and here: http://pinvoke.net/default.aspx/user32.mouse_event
+
+        [DllImport("user32.dll")]
+        static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
+        [Flags]
+        public enum MouseEventFlags : uint
+        {
+            LEFTDOWN = 0x00000002,
+            LEFTUP = 0x00000004,
+            MIDDLEDOWN = 0x00000020,
+            MIDDLEUP = 0x00000040,
+            MOVE = 0x00000001,
+            ABSOLUTE = 0x00008000,
+            RIGHTDOWN = 0x00000008,
+            RIGHTUP = 0x00000010,
+            WHEEL = 0x00000800,
+            XDOWN = 0x00000080,
+            XUP = 0x00000100
+        }
+        //Use the values of this enum for the 'dwData' parameter
+        //to specify an X button when using MouseEventFlags.XDOWN or
+        //MouseEventFlags.XUP for the dwFlags parameter.
+        public enum MouseEventDataXButtons : uint
+        {
+            XBUTTON1 = 0x00000001,
+            XBUTTON2 = 0x00000002
+        }
 
         public MouseTrapper()
         {       
         }
 
 
-        public void TrapMouse(Form control)
-        {            
-            Cursor.Position = new Point(control.Location.X+45, control.Location.Y+35);
-            Rectangle rc = new Rectangle(Cursor.Position, new Size(control.DisplayRectangle.Width - 105, control.DisplayRectangle.Height-85));
+        public void TrapMouse(Control c)
+        {
+            RaiseClick(c);
+            Cursor.Position = new Point(c.Location.X+45, c.Location.Y+35);
+            Rectangle rc = new Rectangle(Cursor.Position, new Size(c.DisplayRectangle.Width - 105, c.DisplayRectangle.Height-85));
             Cursor.Clip = rc;
-            control.Capture = true;
+            
+            c.Capture = true;
         }
-        public void ReleaseMouse(Form control)
+        public void ReleaseMouse(Control c)
         {
-            control.Capture = false;
-            control.Cursor = Cursors.Default;
+            c.Capture = false;
+            c.Cursor = Cursors.Default;
             Cursor.Clip = new Rectangle(0, 0, 0, 0);
         }
+
+        private void RaiseClick(Control c)
+        {
+            //Type t = typeof(Control);
+            //object[] p = new object[1];
+            //p[0] = EventArgs.Empty;
+            //MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
+            //m.Invoke(c, p);
+            Cursor.Position = new System.Drawing.Point(c.Location.X, c.Location.Y);
+            mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
+            mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
+        }
     }
 }
+

 

  ViewVC Help
Powered by ViewVC 1.1.22