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