![]() |
Sylloge
A C# helper library
|
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 using System; 00017 using System.Collections.Generic; 00018 using System.Text; 00019 using System.Runtime.InteropServices; 00020 using Microsoft.Win32.SafeHandles; 00021 00022 namespace Sylloge.Device 00023 { 00027 public class GlobalMouseHandler 00028 { 00029 #region Events 00030 00031 public delegate void DelegateMouseEvent(int wParam, Sylloge.WinAPI.MouseInput mouseHook); 00035 public static event DelegateMouseEvent MouseEvent; 00036 00037 public static void OnMouseEvent(int wParam, Sylloge.WinAPI.MouseInput mouseHook) 00038 { 00039 if (GlobalMouseHandler.MouseEvent != null) { GlobalMouseHandler.MouseEvent(wParam, mouseHook); } 00040 } 00041 00042 #endregion 00043 00044 #region Local Attributes 00045 00046 private static bool IsInitialized = false; 00047 00048 #endregion 00049 00050 #region External Calls (Mouse Specific) 00051 00052 [DllImport("User32.dll", SetLastError = true)] 00053 public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProcDelegate lpfn, IntPtr hmod, int dwThreadId); 00054 [DllImport("User32.dll", SetLastError = true)] 00055 public static extern IntPtr CallNextHookEx(IntPtr hHook, int nCode, int wParam, ref Sylloge.WinAPI.MouseInput lParam); 00056 00057 public delegate IntPtr LowLevelMouseProcDelegate(int nCode, int wParam, ref Sylloge.WinAPI.MouseInput lParam); 00058 00059 public static IntPtr HookLowLevelMouse; 00060 00061 public static LowLevelMouseProcDelegate MouseDelegate; 00062 00063 #endregion 00064 00065 #region Class Methods and Properties 00066 00070 public static void Initialize() 00071 { 00072 if (GlobalMouseHandler.IsInitialized) { return; } // Don't allow multiple initializations 00073 GlobalMouseHandler.MouseDelegate += GlobalMouseHandler.LowLevelMouseProc; 00074 GlobalMouseHandler.HookLowLevelMouse = SetWindowsHookEx(Sylloge.WinAPI.Constants.WH_MOUSE_LL, 00075 GlobalMouseHandler.MouseDelegate, 00076 System.Diagnostics.Process.GetCurrentProcess().MainModule.BaseAddress, 00077 0); 00078 GlobalMouseHandler.IsInitialized = true; 00079 } 00080 00084 public static void Dispose() 00085 { 00086 Sylloge.WinAPI.User32.UnhookWindowsHookEx(GlobalMouseHandler.HookLowLevelMouse); 00087 GlobalMouseHandler.MouseDelegate -= GlobalMouseHandler.LowLevelMouseProc; 00088 GlobalMouseHandler.IsInitialized = false; 00089 } 00090 00091 public static IntPtr LowLevelMouseProc(int nCode, int wParam, ref Sylloge.WinAPI.MouseInput lParam) 00092 { 00093 try { 00094 GlobalMouseHandler.OnMouseEvent(wParam, lParam); 00095 } catch (Exception ex) { 00096 Console.WriteLine("LowLevelProc Ex: {0}", ex); 00097 } 00098 return CallNextHookEx(HookLowLevelMouse, nCode, wParam, ref lParam); 00099 } 00100 00101 public static System.Windows.Forms.Cursor CreateCursor(System.Drawing.Bitmap bmp, int x, int y) 00102 { 00103 IntPtr ptr = bmp.GetHicon(); 00104 WinAPI.IconInfo tmp = new WinAPI.IconInfo(); 00105 WinAPI.User32.GetIconInfo(ptr, ref tmp); 00106 tmp.xHotspot = x; 00107 tmp.yHotspot = y; 00108 tmp.fIcon = false; 00109 ptr = WinAPI.User32.CreateIconIndirect(ref tmp); 00110 return new System.Windows.Forms.Cursor(ptr); 00111 } 00112 00113 #endregion 00114 } 00115 }