sl@0: //=============================================================================
sl@0: // COPYRIGHT: Prosoft-Lanz
sl@0: //=============================================================================
sl@0: //
sl@0: // $Workfile: WndProcRetHook.cs $
sl@0: //
sl@0: // PROJECT : CodeProject Components
sl@0: // VERSION : 1.00
sl@0: // CREATION : 19.02.2003
sl@0: // AUTHOR : JCL
sl@0: //
sl@0: // DETAILS : This class implement the WH_CALLWNDPROCRET Windows hook mechanism.
sl@0: // From MSDN, Dino Esposito.
sl@0: //
sl@0: // WindowCreate, WindowDestroye and WindowActivate user events.
sl@0: //
sl@0: //-----------------------------------------------------------------------------
sl@0: using System;
sl@0: using System.Runtime.InteropServices;
sl@0: using System.Diagnostics;
sl@0:
sl@0: namespace CodeProject.Win32API.Hook
sl@0: {
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: #region Enum WndMessage
sl@0:
sl@0: ///
sl@0: /// windows message.
sl@0: ///
sl@0: public enum WndMessage : int
sl@0: {
sl@0: /// Sent to the dialog procedure immediately before the dialog is displayed.
sl@0: WM_INITDIALOG = 0x0110,
sl@0: /// Sent to the dialog procedure immediately before the dialog is displayed.
sl@0: WM_UNKNOWINIT = 0x0127
sl@0: }
sl@0: #endregion
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: #region Class WndProcRetEventArgs
sl@0:
sl@0: /// Class used for WH_CALLWNDPROCRET hook event arguments.
sl@0: public class WndProcRetEventArgs : EventArgs
sl@0: {
sl@0: /// wParam parameter.
sl@0: public IntPtr wParam;
sl@0: /// lParam parameter.
sl@0: public IntPtr lParam;
sl@0: /// CWPRETSTRUCT structure.
sl@0: public CwPRetStruct cw;
sl@0:
sl@0: internal WndProcRetEventArgs(IntPtr wParam, IntPtr lParam)
sl@0: {
sl@0: this.wParam = wParam;
sl@0: this.lParam = lParam;
sl@0: cw = new CwPRetStruct();
sl@0: Marshal.PtrToStructure(lParam, cw);
sl@0: }
sl@0: }
sl@0:
sl@0: ///
sl@0: /// CWPRETSTRUCT structure.
sl@0: ///
sl@0: [StructLayout(LayoutKind.Sequential)]
sl@0: public class CwPRetStruct
sl@0: {
sl@0: /// Return value.
sl@0: public int lResult;
sl@0: /// lParam parameter.
sl@0: public int lParam;
sl@0: /// wParam parameter.
sl@0: public int wParam;
sl@0: /// Specifies the message.
sl@0: public WndMessage message;
sl@0: /// Handle to the window that processed the message.
sl@0: public IntPtr hwnd;
sl@0: }
sl@0:
sl@0: #endregion
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: #region Class WndProcRetHook
sl@0:
sl@0: ///
sl@0: /// Class to expose the windows WH_CALLWNDPROCRET hook mechanism.
sl@0: ///
sl@0: public class WndProcRetHook : WindowsHook
sl@0: {
sl@0: ///
sl@0: /// WH_CALLWNDPROCRET hook delegate method.
sl@0: ///
sl@0: public delegate void WndProcEventHandler(object sender, WndProcRetEventArgs e);
sl@0:
sl@0: private IntPtr hWndHooked;
sl@0:
sl@0: ///
sl@0: /// Window procedure event.
sl@0: ///
sl@0: public event WndProcEventHandler WndProcRet;
sl@0:
sl@0: ///
sl@0: /// Construct a WH_CALLWNDPROCRET hook.
sl@0: ///
sl@0: ///
sl@0: /// Handle of the window to be hooked. IntPtr.Zero to hook all window.
sl@0: ///
sl@0: public WndProcRetHook(IntPtr hWndHooked) : base(HookType.WH_CALLWNDPROCRET)
sl@0: {
sl@0: this.hWndHooked = hWndHooked;
sl@0: this.HookInvoke += new HookEventHandler(WndProcRetHookInvoked);
sl@0: }
sl@0: ///
sl@0: /// Construct a WH_CALLWNDPROCRET hook giving a hook filter delegate method.
sl@0: ///
sl@0: ///
sl@0: /// Handle of the window to be hooked. IntPtr.Zero to hook all window.
sl@0: ///
sl@0: /// Hook filter event.
sl@0: public WndProcRetHook(IntPtr hWndHooked, HookProc func) : base(HookType.WH_CALLWNDPROCRET, func)
sl@0: {
sl@0: this.hWndHooked = hWndHooked;
sl@0: this.HookInvoke += new HookEventHandler(WndProcRetHookInvoked);
sl@0: }
sl@0:
sl@0: // handles the hook event
sl@0: private void WndProcRetHookInvoked(object sender, HookEventArgs e)
sl@0: {
sl@0: WndProcRetEventArgs wpe = new WndProcRetEventArgs(e.wParam, e.lParam);
sl@0: if ((hWndHooked == IntPtr.Zero || wpe.cw.hwnd == hWndHooked) && WndProcRet != null)
sl@0: WndProcRet(this, wpe);
sl@0: return;
sl@0: }
sl@0: }
sl@0: #endregion
sl@0: }