Make sure action edit combo box for enums is long enough.
1 //=============================================================================
2 // COPYRIGHT: Prosoft-Lanz
3 //=============================================================================
5 // $Workfile: CbtHook.cs $
7 // PROJECT : CodeProject Components
9 // CREATION : 19.02.2003
12 // DETAILS : This class implement the WH_CBT Windows hook mechanism.
13 // From MSDN, Dino Esposito.
14 // WindowCreate, WindowDestroy and WindowActivate user events.
16 //-----------------------------------------------------------------------------
19 using System.Runtime.InteropServices;
21 namespace CodeProject.Win32API.Hook
23 ///////////////////////////////////////////////////////////////////////
24 #region Enum CbtHookAction
29 internal enum CbtHookAction : int
37 HCBT_CLICKSKIPPED = 6,
45 ///////////////////////////////////////////////////////////////////////
46 #region Class CbtEventArgs
49 /// Class used for WH_CBT hook event arguments.
51 public class CbtEventArgs : EventArgs
57 /// Window class name.
58 public string className;
59 /// True if it is a dialog window.
62 internal CbtEventArgs(IntPtr wParam, IntPtr lParam)
64 // cache the parameters
68 // cache the window's class name
69 StringBuilder sb = new StringBuilder();
71 USER32.GetClassName(wParam, sb, 256);
72 className = sb.ToString();
73 IsDialog = (className == "#32770");
79 ///////////////////////////////////////////////////////////////////////
83 /// Class to expose the windows WH_CBT hook mechanism.
85 public class CbtHook : WindowsHook
88 /// WH_CBT hook delegate method.
90 public delegate void CbtEventHandler(object sender, CbtEventArgs e);
93 /// WH_CBT create event.
95 public event CbtEventHandler WindowCreate;
97 /// WH_CBT destroy event.
99 public event CbtEventHandler WindowDestroye;
101 /// WH_CBT activate event.
103 public event CbtEventHandler WindowActivate;
106 /// Construct a WH_CBT hook.
108 public CbtHook() : base(HookType.WH_CBT)
110 this.HookInvoke += new HookEventHandler(CbtHookInvoked);
113 /// Construct a WH_CBT hook giving a hook filter delegate method.
115 /// <param name="func">Hook filter event.</param>
116 public CbtHook(HookProc func) : base(HookType.WH_CBT, func)
118 this.HookInvoke += new HookEventHandler(CbtHookInvoked);
121 // handles the hook event
122 private void CbtHookInvoked(object sender, HookEventArgs e)
124 // handle hook events (only a few of available actions)
125 switch ((CbtHookAction)e.code)
127 case CbtHookAction.HCBT_CREATEWND:
128 HandleCreateWndEvent(e.wParam, e.lParam);
130 case CbtHookAction.HCBT_DESTROYWND:
131 HandleDestroyWndEvent(e.wParam, e.lParam);
133 case CbtHookAction.HCBT_ACTIVATE:
134 HandleActivateEvent(e.wParam, e.lParam);
140 // handle the CREATEWND hook event
141 private void HandleCreateWndEvent(IntPtr wParam, IntPtr lParam)
143 if (WindowCreate != null)
145 CbtEventArgs e = new CbtEventArgs(wParam, lParam);
146 WindowCreate(this, e);
150 // handle the DESTROYWND hook event
151 private void HandleDestroyWndEvent(IntPtr wParam, IntPtr lParam)
153 if (WindowDestroye != null)
155 CbtEventArgs e = new CbtEventArgs(wParam, lParam);
156 WindowDestroye(this, e);
160 // handle the ACTIVATE hook event
161 private void HandleActivateEvent(IntPtr wParam, IntPtr lParam)
163 if (WindowActivate != null)
165 CbtEventArgs e = new CbtEventArgs(wParam, lParam);
166 WindowActivate(this, e);