sl@0: //============================================================================= sl@0: // COPYRIGHT: Prosoft-Lanz sl@0: //============================================================================= sl@0: // sl@0: // $Workfile: CbtHook.cs $ sl@0: // sl@0: // PROJECT : CodeProject Components sl@0: // VERSION : 1.00 sl@0: // CREATION : 19.02.2003 sl@0: // AUTHOR : JCL sl@0: // sl@0: // DETAILS : This class implement the WH_CBT Windows hook mechanism. sl@0: // From MSDN, Dino Esposito. sl@0: // WindowCreate, WindowDestroy and WindowActivate user events. sl@0: // sl@0: //----------------------------------------------------------------------------- sl@0: using System; sl@0: using System.Text; sl@0: using System.Runtime.InteropServices; sl@0: sl@0: namespace CodeProject.Win32API.Hook sl@0: { sl@0: /////////////////////////////////////////////////////////////////////// sl@0: #region Enum CbtHookAction sl@0: sl@0: /// sl@0: /// CBT hook actions. sl@0: /// sl@0: internal enum CbtHookAction : int sl@0: { sl@0: HCBT_MOVESIZE = 0, sl@0: HCBT_MINMAX = 1, sl@0: HCBT_QS = 2, sl@0: HCBT_CREATEWND = 3, sl@0: HCBT_DESTROYWND = 4, sl@0: HCBT_ACTIVATE = 5, sl@0: HCBT_CLICKSKIPPED = 6, sl@0: HCBT_KEYSKIPPED = 7, sl@0: HCBT_SYSCOMMAND = 8, sl@0: HCBT_SETFOCUS = 9 sl@0: } sl@0: sl@0: #endregion sl@0: sl@0: /////////////////////////////////////////////////////////////////////// sl@0: #region Class CbtEventArgs sl@0: sl@0: /// sl@0: /// Class used for WH_CBT hook event arguments. sl@0: /// sl@0: public class CbtEventArgs : EventArgs sl@0: { sl@0: /// wParam parameter. sl@0: public IntPtr wParam; sl@0: /// lParam parameter. sl@0: public IntPtr lParam; sl@0: /// Window class name. sl@0: public string className; sl@0: /// True if it is a dialog window. sl@0: public bool IsDialog; sl@0: sl@0: internal CbtEventArgs(IntPtr wParam, IntPtr lParam) sl@0: { sl@0: // cache the parameters sl@0: this.wParam = wParam; sl@0: this.lParam = lParam; sl@0: sl@0: // cache the window's class name sl@0: StringBuilder sb = new StringBuilder(); sl@0: sb.Capacity = 256; sl@0: USER32.GetClassName(wParam, sb, 256); sl@0: className = sb.ToString(); sl@0: IsDialog = (className == "#32770"); sl@0: } sl@0: } sl@0: sl@0: #endregion sl@0: sl@0: /////////////////////////////////////////////////////////////////////// sl@0: #region Class CbtHook sl@0: sl@0: /// sl@0: /// Class to expose the windows WH_CBT hook mechanism. sl@0: /// sl@0: public class CbtHook : WindowsHook sl@0: { sl@0: /// sl@0: /// WH_CBT hook delegate method. sl@0: /// sl@0: public delegate void CbtEventHandler(object sender, CbtEventArgs e); sl@0: sl@0: /// sl@0: /// WH_CBT create event. sl@0: /// sl@0: public event CbtEventHandler WindowCreate; sl@0: /// sl@0: /// WH_CBT destroy event. sl@0: /// sl@0: public event CbtEventHandler WindowDestroye; sl@0: /// sl@0: /// WH_CBT activate event. sl@0: /// sl@0: public event CbtEventHandler WindowActivate; sl@0: sl@0: /// sl@0: /// Construct a WH_CBT hook. sl@0: /// sl@0: public CbtHook() : base(HookType.WH_CBT) sl@0: { sl@0: this.HookInvoke += new HookEventHandler(CbtHookInvoked); sl@0: } sl@0: /// sl@0: /// Construct a WH_CBT hook giving a hook filter delegate method. sl@0: /// sl@0: /// Hook filter event. sl@0: public CbtHook(HookProc func) : base(HookType.WH_CBT, func) sl@0: { sl@0: this.HookInvoke += new HookEventHandler(CbtHookInvoked); sl@0: } sl@0: sl@0: // handles the hook event sl@0: private void CbtHookInvoked(object sender, HookEventArgs e) sl@0: { sl@0: // handle hook events (only a few of available actions) sl@0: switch ((CbtHookAction)e.code) sl@0: { sl@0: case CbtHookAction.HCBT_CREATEWND: sl@0: HandleCreateWndEvent(e.wParam, e.lParam); sl@0: break; sl@0: case CbtHookAction.HCBT_DESTROYWND: sl@0: HandleDestroyWndEvent(e.wParam, e.lParam); sl@0: break; sl@0: case CbtHookAction.HCBT_ACTIVATE: sl@0: HandleActivateEvent(e.wParam, e.lParam); sl@0: break; sl@0: } sl@0: return; sl@0: } sl@0: sl@0: // handle the CREATEWND hook event sl@0: private void HandleCreateWndEvent(IntPtr wParam, IntPtr lParam) sl@0: { sl@0: if (WindowCreate != null) sl@0: { sl@0: CbtEventArgs e = new CbtEventArgs(wParam, lParam); sl@0: WindowCreate(this, e); sl@0: } sl@0: } sl@0: sl@0: // handle the DESTROYWND hook event sl@0: private void HandleDestroyWndEvent(IntPtr wParam, IntPtr lParam) sl@0: { sl@0: if (WindowDestroye != null) sl@0: { sl@0: CbtEventArgs e = new CbtEventArgs(wParam, lParam); sl@0: WindowDestroye(this, e); sl@0: } sl@0: } sl@0: sl@0: // handle the ACTIVATE hook event sl@0: private void HandleActivateEvent(IntPtr wParam, IntPtr lParam) sl@0: { sl@0: if (WindowActivate != null) sl@0: { sl@0: CbtEventArgs e = new CbtEventArgs(wParam, lParam); sl@0: WindowActivate(this, e); sl@0: } sl@0: } sl@0: } sl@0: #endregion sl@0: }