First contrib.
1 //=============================================================================
2 // COPYRIGHT: Prosoft-Lanz
3 //=============================================================================
5 // $Workfile: WndProcRetHook.cs $
7 // PROJECT : CodeProject Components
9 // CREATION : 19.02.2003
12 // DETAILS : This class implement the WH_CALLWNDPROCRET Windows hook mechanism.
13 // From MSDN, Dino Esposito.
15 // WindowCreate, WindowDestroye and WindowActivate user events.
17 //-----------------------------------------------------------------------------
19 using System.Runtime.InteropServices;
20 using System.Diagnostics;
22 namespace CodeProject.Win32API.Hook
24 ///////////////////////////////////////////////////////////////////////
25 #region Enum WndMessage
30 public enum WndMessage : int
32 /// Sent to the dialog procedure immediately before the dialog is displayed.
33 WM_INITDIALOG = 0x0110,
34 /// Sent to the dialog procedure immediately before the dialog is displayed.
35 WM_UNKNOWINIT = 0x0127
39 ///////////////////////////////////////////////////////////////////////
40 #region Class WndProcRetEventArgs
42 /// Class used for WH_CALLWNDPROCRET hook event arguments.
43 public class WndProcRetEventArgs : EventArgs
49 /// CWPRETSTRUCT structure.
50 public CwPRetStruct cw;
52 internal WndProcRetEventArgs(IntPtr wParam, IntPtr lParam)
56 cw = new CwPRetStruct();
57 Marshal.PtrToStructure(lParam, cw);
62 /// CWPRETSTRUCT structure.
64 [StructLayout(LayoutKind.Sequential)]
65 public class CwPRetStruct
73 /// Specifies the message.
74 public WndMessage message;
75 /// Handle to the window that processed the message.
81 ///////////////////////////////////////////////////////////////////////
82 #region Class WndProcRetHook
85 /// Class to expose the windows WH_CALLWNDPROCRET hook mechanism.
87 public class WndProcRetHook : WindowsHook
90 /// WH_CALLWNDPROCRET hook delegate method.
92 public delegate void WndProcEventHandler(object sender, WndProcRetEventArgs e);
94 private IntPtr hWndHooked;
97 /// Window procedure event.
99 public event WndProcEventHandler WndProcRet;
102 /// Construct a WH_CALLWNDPROCRET hook.
104 /// <param name="hWndHooked">
105 /// Handle of the window to be hooked. IntPtr.Zero to hook all window.
107 public WndProcRetHook(IntPtr hWndHooked) : base(HookType.WH_CALLWNDPROCRET)
109 this.hWndHooked = hWndHooked;
110 this.HookInvoke += new HookEventHandler(WndProcRetHookInvoked);
113 /// Construct a WH_CALLWNDPROCRET hook giving a hook filter delegate method.
115 /// <param name="hWndHooked">
116 /// Handle of the window to be hooked. IntPtr.Zero to hook all window.
118 /// <param name="func">Hook filter event.</param>
119 public WndProcRetHook(IntPtr hWndHooked, HookProc func) : base(HookType.WH_CALLWNDPROCRET, func)
121 this.hWndHooked = hWndHooked;
122 this.HookInvoke += new HookEventHandler(WndProcRetHookInvoked);
125 // handles the hook event
126 private void WndProcRetHookInvoked(object sender, HookEventArgs e)
128 WndProcRetEventArgs wpe = new WndProcRetEventArgs(e.wParam, e.lParam);
129 if ((hWndHooked == IntPtr.Zero || wpe.cw.hwnd == hWndHooked) && WndProcRet != null)
130 WndProcRet(this, wpe);