WndProcRetHook.cs
changeset 0 f6eca6facd07
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/WndProcRetHook.cs	Sat Jun 14 12:51:25 2014 +0200
     1.3 @@ -0,0 +1,135 @@
     1.4 +//=============================================================================
     1.5 +// COPYRIGHT: Prosoft-Lanz
     1.6 +//=============================================================================
     1.7 +//
     1.8 +// $Workfile: WndProcRetHook.cs $
     1.9 +//
    1.10 +// PROJECT : CodeProject Components
    1.11 +// VERSION : 1.00
    1.12 +// CREATION : 19.02.2003
    1.13 +// AUTHOR : JCL
    1.14 +//
    1.15 +// DETAILS : This class implement the WH_CALLWNDPROCRET Windows hook mechanism.
    1.16 +//           From MSDN, Dino Esposito.
    1.17 +//
    1.18 +//           WindowCreate, WindowDestroye and WindowActivate user events.
    1.19 +//
    1.20 +//-----------------------------------------------------------------------------
    1.21 +using System;
    1.22 +using System.Runtime.InteropServices;
    1.23 +using System.Diagnostics;
    1.24 +
    1.25 +namespace CodeProject.Win32API.Hook
    1.26 +{
    1.27 +	///////////////////////////////////////////////////////////////////////
    1.28 +	#region Enum WndMessage
    1.29 +
    1.30 +	/// <summary>
    1.31 +	/// windows message.
    1.32 +	/// </summary>
    1.33 +	public enum WndMessage : int
    1.34 +	{
    1.35 +		/// Sent to the dialog procedure immediately before the dialog is displayed.
    1.36 +		WM_INITDIALOG = 0x0110,
    1.37 +		/// Sent to the dialog procedure immediately before the dialog is displayed.
    1.38 +		WM_UNKNOWINIT = 0x0127
    1.39 +	}
    1.40 +	#endregion
    1.41 +
    1.42 +	///////////////////////////////////////////////////////////////////////
    1.43 +	#region Class WndProcRetEventArgs
    1.44 +
    1.45 +	/// Class used for WH_CALLWNDPROCRET hook event arguments.
    1.46 +	public class WndProcRetEventArgs : EventArgs
    1.47 +	{
    1.48 +		/// wParam parameter.
    1.49 +		public IntPtr wParam;
    1.50 +		/// lParam parameter.
    1.51 +		public IntPtr lParam;
    1.52 +		/// CWPRETSTRUCT structure.
    1.53 +		public CwPRetStruct cw;
    1.54 +
    1.55 +		internal WndProcRetEventArgs(IntPtr wParam, IntPtr lParam)
    1.56 +		{
    1.57 +			this.wParam = wParam;
    1.58 +			this.lParam = lParam;
    1.59 +			cw = new CwPRetStruct();
    1.60 +			Marshal.PtrToStructure(lParam, cw);
    1.61 +		}
    1.62 +	}
    1.63 +
    1.64 +	/// <summary>
    1.65 +	/// CWPRETSTRUCT structure.
    1.66 +	/// </summary>
    1.67 +	[StructLayout(LayoutKind.Sequential)]
    1.68 +	public class CwPRetStruct
    1.69 +	{
    1.70 +		/// Return value.
    1.71 +		public int lResult;
    1.72 +		/// lParam parameter.
    1.73 +		public int lParam;
    1.74 +		/// wParam parameter.
    1.75 +		public int wParam;
    1.76 +		/// Specifies the message.
    1.77 +		public WndMessage message;
    1.78 +		/// Handle to the window that processed the message.
    1.79 +		public IntPtr hwnd;
    1.80 +	}
    1.81 +
    1.82 +	#endregion
    1.83 +
    1.84 +	///////////////////////////////////////////////////////////////////////
    1.85 +	#region Class WndProcRetHook
    1.86 +	
    1.87 +	/// <summary>
    1.88 +	/// Class to expose the windows WH_CALLWNDPROCRET hook mechanism.
    1.89 +	/// </summary>
    1.90 +	public class WndProcRetHook : WindowsHook
    1.91 +	{
    1.92 +		/// <summary>
    1.93 +		/// WH_CALLWNDPROCRET hook delegate method.
    1.94 +		/// </summary>
    1.95 +		public delegate void WndProcEventHandler(object sender, WndProcRetEventArgs e);
    1.96 +
    1.97 +		private IntPtr hWndHooked;
    1.98 +
    1.99 +		/// <summary>
   1.100 +		/// Window procedure event.
   1.101 +		/// </summary>
   1.102 +		public event WndProcEventHandler WndProcRet;
   1.103 +
   1.104 +		/// <summary>
   1.105 +		/// Construct a WH_CALLWNDPROCRET hook.
   1.106 +		/// </summary>
   1.107 +		/// <param name="hWndHooked">
   1.108 +		/// Handle of the window to be hooked. IntPtr.Zero to hook all window.
   1.109 +		/// </param>
   1.110 +		public WndProcRetHook(IntPtr hWndHooked) : base(HookType.WH_CALLWNDPROCRET)
   1.111 +		{
   1.112 +			this.hWndHooked = hWndHooked;
   1.113 +			this.HookInvoke += new HookEventHandler(WndProcRetHookInvoked);
   1.114 +		}
   1.115 +		/// <summary>
   1.116 +		/// Construct a WH_CALLWNDPROCRET hook giving a hook filter delegate method.
   1.117 +		/// </summary>
   1.118 +		/// <param name="hWndHooked">
   1.119 +		/// Handle of the window to be hooked. IntPtr.Zero to hook all window.
   1.120 +		/// </param>
   1.121 +		/// <param name="func">Hook filter event.</param>
   1.122 +		public WndProcRetHook(IntPtr hWndHooked, HookProc func) : base(HookType.WH_CALLWNDPROCRET, func)
   1.123 +		{
   1.124 +			this.hWndHooked = hWndHooked;
   1.125 +			this.HookInvoke += new HookEventHandler(WndProcRetHookInvoked);
   1.126 +		}
   1.127 +
   1.128 +		// handles the hook event
   1.129 +		private void WndProcRetHookInvoked(object sender, HookEventArgs e)
   1.130 +		{
   1.131 +			WndProcRetEventArgs wpe = new WndProcRetEventArgs(e.wParam, e.lParam);
   1.132 +			if ((hWndHooked == IntPtr.Zero || wpe.cw.hwnd == hWndHooked) && WndProcRet != null)
   1.133 +				WndProcRet(this, wpe);
   1.134 +			return;
   1.135 +		}
   1.136 +	}
   1.137 +	#endregion
   1.138 +}