CbtHook.cs
changeset 0 f6eca6facd07
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/CbtHook.cs	Sat Jun 14 12:51:25 2014 +0200
     1.3 @@ -0,0 +1,171 @@
     1.4 +//=============================================================================
     1.5 +// COPYRIGHT: Prosoft-Lanz
     1.6 +//=============================================================================
     1.7 +//
     1.8 +// $Workfile: CbtHook.cs $
     1.9 +//
    1.10 +// PROJECT : CodeProject Components
    1.11 +// VERSION : 1.00
    1.12 +// CREATION : 19.02.2003
    1.13 +// AUTHOR : JCL
    1.14 +//
    1.15 +// DETAILS : This class implement the WH_CBT Windows hook mechanism.
    1.16 +//           From MSDN, Dino Esposito.
    1.17 +//           WindowCreate, WindowDestroy and WindowActivate user events.
    1.18 +//
    1.19 +//-----------------------------------------------------------------------------
    1.20 +using System;
    1.21 +using System.Text;
    1.22 +using System.Runtime.InteropServices;
    1.23 +
    1.24 +namespace CodeProject.Win32API.Hook
    1.25 +{
    1.26 +	///////////////////////////////////////////////////////////////////////
    1.27 +	#region Enum CbtHookAction
    1.28 +
    1.29 +	/// <summary>
    1.30 +	/// CBT hook actions.
    1.31 +	/// </summary>
    1.32 +	internal enum CbtHookAction : int
    1.33 +	{
    1.34 +		HCBT_MOVESIZE = 0,
    1.35 +		HCBT_MINMAX = 1,
    1.36 +		HCBT_QS = 2,
    1.37 +		HCBT_CREATEWND = 3,
    1.38 +		HCBT_DESTROYWND = 4,
    1.39 +		HCBT_ACTIVATE = 5,
    1.40 +		HCBT_CLICKSKIPPED = 6,
    1.41 +		HCBT_KEYSKIPPED = 7,
    1.42 +		HCBT_SYSCOMMAND = 8,
    1.43 +		HCBT_SETFOCUS = 9
    1.44 +	}
    1.45 +
    1.46 +	#endregion
    1.47 +
    1.48 +	///////////////////////////////////////////////////////////////////////
    1.49 +	#region Class CbtEventArgs
    1.50 +
    1.51 +	/// <summary>
    1.52 +	/// Class used for WH_CBT hook event arguments.
    1.53 +	/// </summary>
    1.54 +	public class CbtEventArgs : EventArgs
    1.55 +	{
    1.56 +		/// wParam parameter.
    1.57 +		public IntPtr wParam;
    1.58 +		/// lParam parameter.
    1.59 +		public IntPtr lParam;
    1.60 +		/// Window class name.
    1.61 +		public string className;
    1.62 +		/// True if it is a dialog window.
    1.63 +		public bool IsDialog;
    1.64 +
    1.65 +		internal CbtEventArgs(IntPtr wParam, IntPtr lParam)
    1.66 +		{
    1.67 +			// cache the parameters
    1.68 +			this.wParam = wParam;
    1.69 +			this.lParam = lParam;
    1.70 +
    1.71 +			// cache the window's class name
    1.72 +			StringBuilder sb = new StringBuilder();
    1.73 +			sb.Capacity = 256;
    1.74 +			USER32.GetClassName(wParam, sb, 256);
    1.75 +			className = sb.ToString();
    1.76 +			IsDialog = (className == "#32770");
    1.77 +		}
    1.78 +	}
    1.79 +
    1.80 +	#endregion
    1.81 +
    1.82 +	///////////////////////////////////////////////////////////////////////
    1.83 +	#region Class CbtHook
    1.84 +	
    1.85 +	/// <summary>
    1.86 +	/// Class to expose the windows WH_CBT hook mechanism.
    1.87 +	/// </summary>
    1.88 +	public class CbtHook : WindowsHook
    1.89 +	{
    1.90 +		/// <summary>
    1.91 +		/// WH_CBT hook delegate method.
    1.92 +		/// </summary>
    1.93 +		public delegate void CbtEventHandler(object sender, CbtEventArgs e);
    1.94 +
    1.95 +		/// <summary>
    1.96 +		/// WH_CBT create event.
    1.97 +		/// </summary>
    1.98 +		public event CbtEventHandler WindowCreate;
    1.99 +		/// <summary>
   1.100 +		/// WH_CBT destroy event.
   1.101 +		/// </summary>
   1.102 +		public event CbtEventHandler WindowDestroye;
   1.103 +		/// <summary>
   1.104 +		/// WH_CBT activate event.
   1.105 +		/// </summary>
   1.106 +		public event CbtEventHandler WindowActivate;
   1.107 +
   1.108 +		/// <summary>
   1.109 +		/// Construct a WH_CBT hook.
   1.110 +		/// </summary>
   1.111 +		public CbtHook() : base(HookType.WH_CBT)
   1.112 +		{
   1.113 +			this.HookInvoke += new HookEventHandler(CbtHookInvoked);
   1.114 +		}
   1.115 +		/// <summary>
   1.116 +		/// Construct a WH_CBT hook giving a hook filter delegate method.
   1.117 +		/// </summary>
   1.118 +		/// <param name="func">Hook filter event.</param>
   1.119 +		public CbtHook(HookProc func) : base(HookType.WH_CBT, func)
   1.120 +		{
   1.121 +			this.HookInvoke += new HookEventHandler(CbtHookInvoked);
   1.122 +		}
   1.123 +
   1.124 +		// handles the hook event
   1.125 +		private void CbtHookInvoked(object sender, HookEventArgs e)
   1.126 +		{
   1.127 +			// handle hook events (only a few of available actions)
   1.128 +			switch ((CbtHookAction)e.code)
   1.129 +			{
   1.130 +				case CbtHookAction.HCBT_CREATEWND:
   1.131 +					HandleCreateWndEvent(e.wParam, e.lParam);
   1.132 +					break;
   1.133 +				case CbtHookAction.HCBT_DESTROYWND:
   1.134 +					HandleDestroyWndEvent(e.wParam, e.lParam);
   1.135 +					break;
   1.136 +				case CbtHookAction.HCBT_ACTIVATE:
   1.137 +					HandleActivateEvent(e.wParam, e.lParam);
   1.138 +					break;
   1.139 +			}
   1.140 +			return;
   1.141 +		}
   1.142 +
   1.143 +		// handle the CREATEWND hook event
   1.144 +		private void HandleCreateWndEvent(IntPtr wParam, IntPtr lParam)
   1.145 +		{
   1.146 +			if (WindowCreate != null)
   1.147 +			{
   1.148 +				CbtEventArgs e = new CbtEventArgs(wParam, lParam);
   1.149 +				WindowCreate(this, e);
   1.150 +			}
   1.151 +		}
   1.152 +
   1.153 +		// handle the DESTROYWND hook event
   1.154 +		private void HandleDestroyWndEvent(IntPtr wParam, IntPtr lParam)
   1.155 +		{
   1.156 +			if (WindowDestroye != null)
   1.157 +			{
   1.158 +				CbtEventArgs e = new CbtEventArgs(wParam, lParam);
   1.159 +				WindowDestroye(this, e);
   1.160 +			}
   1.161 +		}
   1.162 +
   1.163 +		// handle the ACTIVATE hook event
   1.164 +		private void HandleActivateEvent(IntPtr wParam, IntPtr lParam)
   1.165 +		{
   1.166 +			if (WindowActivate != null)
   1.167 +			{
   1.168 +				CbtEventArgs e = new CbtEventArgs(wParam, lParam);
   1.169 +				WindowActivate(this, e);
   1.170 +			}
   1.171 +		}
   1.172 +	}
   1.173 +	#endregion
   1.174 +}