All pastes #298112 Raw Edit

Someone

public text v1 · immutable
#298112 ·published 2006-12-31 15:59 UTC
rendered paste body
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using System.Data;
using System.Collections;
using System.ComponentModel;
using System.Drawing;

namespace toon
{

    public class Mouse : Object 
    {
        #region Declair Constants
        //values from Winuser.h in Microsoft SDK.
        public const int WH_MOUSE_LL = 14;	//mouse hook constant
        public const int WH_KEYBOARD_LL = 13;	//keyboard hook constant	

        private const int WM_MOUSEMOVE = 0x200;
        private const int WM_LBUTTONDOWN = 0x201;
        private const int WM_RBUTTONDOWN = 0x204;
        private const int WM_MBUTTONDOWN = 0x207;
        private const int WM_LBUTTONUP = 0x202;
        private const int WM_RBUTTONUP = 0x205;
        private const int WM_MBUTTONUP = 0x208;
        private const int WM_LBUTTONDBLCLK = 0x203;
        private const int WM_RBUTTONDBLCLK = 0x206;
        private const int WM_MBUTTONDBLCLK = 0x209;

        private const int WM_KEYDOWN = 0x100;
        private const int WM_KEYUP = 0x101;
        private const int WM_SYSKEYDOWN = 0x104;
        private const int WM_SYSKEYUP = 0x105;
        #endregion
       
        public Mouse()
        {
			Start();
		}
        ~Mouse()
        { 
			Stop();
		} 
        public event MouseEventHandler OnMouseActivity;

        static int hMouseHook = 0; //Declare mouse hook handle as int.
        HookProc MouseHookProcedure; //Declare MouseHookProcedure as HookProc type.

        public void MouseClick(MouseAPI.MouseEventTFlags ClickType)
        {
            MouseAPI.mouse_event((uint)ClickType, 0, 0, 0, 0);
        }
        public void MouseTo(int x, int y)
        {
            MouseAPI.SetCursorPos(x, y);
            MouseAPI.mouse_event((uint)MouseAPI.MouseEventTFlags.MOVE, 0, 0, 0, 0);
        }
        public System.Drawing.Point MouseGetPos()
        {
            System.Drawing.Point p = new System.Drawing.Point();
            MouseAPI.GetCursorPos(out p);
            return p;
        }
        
        public void Start()
		{
			// install Mouse hook 
			if(hMouseHook == 0)
			{
				// Create an instance of HookProc.
				MouseHookProcedure = new HookProc(MouseHookProc);

                hMouseHook = MouseAPI.SetWindowsHookEx(WH_MOUSE_LL,
					MouseHookProcedure, 
					Marshal.GetHINSTANCE(
						Assembly.GetExecutingAssembly().GetModules()[0]),
					0);

				//If SetWindowsHookEx fails.
				if(hMouseHook == 0 )	{
					Stop();
					throw new Exception("SetWindowsHookEx failed.");
				}

			}
		}
		public void Stop()
		{
			bool retMouse =true;
			if(hMouseHook != 0)
			{
                retMouse = MouseAPI.UnhookWindowsHookEx(hMouseHook);
				hMouseHook = 0;
			} 
			//If UnhookWindowsHookEx fails.
			//if (!retMouse) throw new Exception("UnhookWindowsHookEx for Mouse failed.");
		}

        private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
        {
            // if ok and someone listens to our events
            if ((nCode >= 0) && (OnMouseActivity != null))
            {

                MouseButtons button = MouseButtons.None;
                switch (wParam)
                {
                    case WM_LBUTTONDOWN:
                        //case WM_LBUTTONUP: 
                        //case WM_LBUTTONDBLCLK: 
                        button = MouseButtons.Left;
                        break;
                    case WM_RBUTTONDOWN:
                        //case WM_RBUTTONUP: 
                        //case WM_RBUTTONDBLCLK: 
                        button = MouseButtons.Right;
                        break;
                }
                int clickCount = 0;
                if (button != MouseButtons.None)
                    if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK) clickCount = 2;
                    else clickCount = 1;

                //Marshall the data from callback.
                MouseAPI.MouseHookStruct MyMouseHookStruct = (MouseAPI.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseAPI.MouseHookStruct));
                MouseEventArgs e = new MouseEventArgs(
                                                    button,
                                                    clickCount,
                                                    MyMouseHookStruct.pt.x,
                                                    MyMouseHookStruct.pt.y,
                                                    0);
                OnMouseActivity(this, e);
            }
            return MouseAPI.CallNextHookEx(hMouseHook, nCode, wParam, lParam);
        }
    }

    public class MouseAPI
    {
        #region DllImports

        //Use this function to install a hook.
        [DllImport("user32.dll", CharSet = CharSet.Auto,
            CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
            IntPtr hInstance, int threadId);

        //Import for UnhookWindowsHookEx.
        //Call this function to uninstall the hook.
        [DllImport("user32.dll", CharSet = CharSet.Auto,
             CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        //Import for CallNextHookEx.
        //Use this function to pass the hook information to next hook procedure in chain.
        [DllImport("user32.dll", CharSet = CharSet.Auto,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook,
                                                int nCode,
                                                Int32 wParam,
                                                IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent,
                                                 IntPtr hwndChildAfter,
                                                 string lpszClass,
                                                 string lpszWindow);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName,
                                               string lpWindowName);


        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern void SendMessage(IntPtr hWnd,
                                              uint msg,
                                              IntPtr wParam,
                                              IntPtr lParam);

        [DllImport("user32.dll")]

        public static extern void mouse_event(uint dwFlags,
                                        uint dx,
                                        uint dy,
                                        uint dwData,
                                        int dwExtraInfo);

        [DllImport("user32.dll")]
        public static extern void SetCursorPos(int x, int y);
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out System.Drawing.Point lpPoint);

        #endregion

        #region Structures
        //Declare wrapper managed MouseHookStruct class.
        [StructLayout(LayoutKind.Sequential)]
        public class MouseHookStruct
        {
            public KeyboardAPI.POINT pt;
            public int hwnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }

        [Flags]
        public enum MouseEventTFlags
        {
            LEFTDOWN = 0x00000002,
            LEFTUP = 0x00000004,
            MIDDLEDOWN = 0x00000020,
            MIDDLEUP = 0x00000040,
            MOVE = 0x00000001,
            ABSOLUTE = 0x00008000,
            RIGHTDOWN = 0x00000008,
            RIGHTUP = 0x00000010
        }
        public struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public int mouseData;
            public int dwFlags;
            public int time;
            public int dwExtraInfo;
        }
        #endregion 
    }


}