Server/CbtHook.cs
author StephaneLenclud
Thu, 25 Aug 2016 13:34:05 +0200
changeset 255 c57b8ac80fc6
parent 0 f6eca6facd07
permissions -rw-r--r--
Published v1.0.2.0.
Fixed Harmony async issue prevent the config to be fetched.
     1 //=============================================================================
     2 // COPYRIGHT: Prosoft-Lanz
     3 //=============================================================================
     4 //
     5 // $Workfile: CbtHook.cs $
     6 //
     7 // PROJECT : CodeProject Components
     8 // VERSION : 1.00
     9 // CREATION : 19.02.2003
    10 // AUTHOR : JCL
    11 //
    12 // DETAILS : This class implement the WH_CBT Windows hook mechanism.
    13 //           From MSDN, Dino Esposito.
    14 //           WindowCreate, WindowDestroy and WindowActivate user events.
    15 //
    16 //-----------------------------------------------------------------------------
    17 using System;
    18 using System.Text;
    19 using System.Runtime.InteropServices;
    20 
    21 namespace CodeProject.Win32API.Hook
    22 {
    23 	///////////////////////////////////////////////////////////////////////
    24 	#region Enum CbtHookAction
    25 
    26 	/// <summary>
    27 	/// CBT hook actions.
    28 	/// </summary>
    29 	internal enum CbtHookAction : int
    30 	{
    31 		HCBT_MOVESIZE = 0,
    32 		HCBT_MINMAX = 1,
    33 		HCBT_QS = 2,
    34 		HCBT_CREATEWND = 3,
    35 		HCBT_DESTROYWND = 4,
    36 		HCBT_ACTIVATE = 5,
    37 		HCBT_CLICKSKIPPED = 6,
    38 		HCBT_KEYSKIPPED = 7,
    39 		HCBT_SYSCOMMAND = 8,
    40 		HCBT_SETFOCUS = 9
    41 	}
    42 
    43 	#endregion
    44 
    45 	///////////////////////////////////////////////////////////////////////
    46 	#region Class CbtEventArgs
    47 
    48 	/// <summary>
    49 	/// Class used for WH_CBT hook event arguments.
    50 	/// </summary>
    51 	public class CbtEventArgs : EventArgs
    52 	{
    53 		/// wParam parameter.
    54 		public IntPtr wParam;
    55 		/// lParam parameter.
    56 		public IntPtr lParam;
    57 		/// Window class name.
    58 		public string className;
    59 		/// True if it is a dialog window.
    60 		public bool IsDialog;
    61 
    62 		internal CbtEventArgs(IntPtr wParam, IntPtr lParam)
    63 		{
    64 			// cache the parameters
    65 			this.wParam = wParam;
    66 			this.lParam = lParam;
    67 
    68 			// cache the window's class name
    69 			StringBuilder sb = new StringBuilder();
    70 			sb.Capacity = 256;
    71 			USER32.GetClassName(wParam, sb, 256);
    72 			className = sb.ToString();
    73 			IsDialog = (className == "#32770");
    74 		}
    75 	}
    76 
    77 	#endregion
    78 
    79 	///////////////////////////////////////////////////////////////////////
    80 	#region Class CbtHook
    81 	
    82 	/// <summary>
    83 	/// Class to expose the windows WH_CBT hook mechanism.
    84 	/// </summary>
    85 	public class CbtHook : WindowsHook
    86 	{
    87 		/// <summary>
    88 		/// WH_CBT hook delegate method.
    89 		/// </summary>
    90 		public delegate void CbtEventHandler(object sender, CbtEventArgs e);
    91 
    92 		/// <summary>
    93 		/// WH_CBT create event.
    94 		/// </summary>
    95 		public event CbtEventHandler WindowCreate;
    96 		/// <summary>
    97 		/// WH_CBT destroy event.
    98 		/// </summary>
    99 		public event CbtEventHandler WindowDestroye;
   100 		/// <summary>
   101 		/// WH_CBT activate event.
   102 		/// </summary>
   103 		public event CbtEventHandler WindowActivate;
   104 
   105 		/// <summary>
   106 		/// Construct a WH_CBT hook.
   107 		/// </summary>
   108 		public CbtHook() : base(HookType.WH_CBT)
   109 		{
   110 			this.HookInvoke += new HookEventHandler(CbtHookInvoked);
   111 		}
   112 		/// <summary>
   113 		/// Construct a WH_CBT hook giving a hook filter delegate method.
   114 		/// </summary>
   115 		/// <param name="func">Hook filter event.</param>
   116 		public CbtHook(HookProc func) : base(HookType.WH_CBT, func)
   117 		{
   118 			this.HookInvoke += new HookEventHandler(CbtHookInvoked);
   119 		}
   120 
   121 		// handles the hook event
   122 		private void CbtHookInvoked(object sender, HookEventArgs e)
   123 		{
   124 			// handle hook events (only a few of available actions)
   125 			switch ((CbtHookAction)e.code)
   126 			{
   127 				case CbtHookAction.HCBT_CREATEWND:
   128 					HandleCreateWndEvent(e.wParam, e.lParam);
   129 					break;
   130 				case CbtHookAction.HCBT_DESTROYWND:
   131 					HandleDestroyWndEvent(e.wParam, e.lParam);
   132 					break;
   133 				case CbtHookAction.HCBT_ACTIVATE:
   134 					HandleActivateEvent(e.wParam, e.lParam);
   135 					break;
   136 			}
   137 			return;
   138 		}
   139 
   140 		// handle the CREATEWND hook event
   141 		private void HandleCreateWndEvent(IntPtr wParam, IntPtr lParam)
   142 		{
   143 			if (WindowCreate != null)
   144 			{
   145 				CbtEventArgs e = new CbtEventArgs(wParam, lParam);
   146 				WindowCreate(this, e);
   147 			}
   148 		}
   149 
   150 		// handle the DESTROYWND hook event
   151 		private void HandleDestroyWndEvent(IntPtr wParam, IntPtr lParam)
   152 		{
   153 			if (WindowDestroye != null)
   154 			{
   155 				CbtEventArgs e = new CbtEventArgs(wParam, lParam);
   156 				WindowDestroye(this, e);
   157 			}
   158 		}
   159 
   160 		// handle the ACTIVATE hook event
   161 		private void HandleActivateEvent(IntPtr wParam, IntPtr lParam)
   162 		{
   163 			if (WindowActivate != null)
   164 			{
   165 				CbtEventArgs e = new CbtEventArgs(wParam, lParam);
   166 				WindowActivate(this, e);
   167 			}
   168 		}
   169 	}
   170 	#endregion
   171 }