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