ibennz

Windows hook

Aug 27th, 2014
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Runtime.InteropServices;
  11.  
  12. //author ibennz
  13.  
  14. namespace wnprocsharp
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         //http://www.pinvoke.net/default.aspx/user32/GetWindowLongPtr.html
  19.         [DllImport("user32.dll", EntryPoint = "GetWindowLongW")]
  20.         private static extern int GetWindowLongPtr32(IntPtr hWnd, int nIndex);
  21.  
  22.         [DllImport("user32.dll", EntryPoint = "GetWindowLongPtrW")]
  23.         private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
  24.  
  25.         public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
  26.         {
  27.             if (IntPtr.Size == 8)
  28.                 return GetWindowLongPtr64(hWnd, nIndex);
  29.             else
  30.                 return new IntPtr(GetWindowLongPtr32(hWnd, nIndex));
  31.         }
  32.  
  33.         [DllImport("user32.dll")]
  34.         public static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
  35.         [DllImport("user32.dll")]
  36.         public static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  37.  
  38.         //http://pinvoke.net/default.aspx/user32/SetWindowLongPtr.html
  39.         public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
  40.         {
  41.             if (IntPtr.Size == 8)
  42.                 return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
  43.             else
  44.                 return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
  45.         }
  46.  
  47.         [DllImport("user32.dll", EntryPoint = "SetWindowLongW")]
  48.         private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);
  49.  
  50.         [DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW")]
  51.         private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
  52.  
  53.         [DllImport("user32.dll")]
  54.         [return: MarshalAs(UnmanagedType.Bool)]
  55.         public static extern bool IsWindow(IntPtr hWnd);
  56.         public delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
  57.         public static WndProcDelegate m_CtrlWndProcDelegate;
  58.         public static WndProcDelegate m_DefWndProcDelegate;
  59.         public static Dictionary<IntPtr, IntPtr> m_WndProc = new Dictionary<IntPtr, IntPtr>(); //List<IntPtr> mList = new List<IntPtr>();  m_WndProc
  60.  
  61.         public static IntPtr CtrlWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
  62.         {
  63.             if (!m_WndProc.Keys.Contains (hWnd))
  64.                 return m_DefWndProcDelegate(hWnd, msg, wParam, lParam);
  65.  
  66.  
  67.             IntPtr nRet = CallWindowProc(m_WndProc[hWnd], hWnd, msg, wParam, lParam);
  68.  
  69.             switch (msg)
  70.             {
  71.                 case WindowsMessage.WM_SIZE:
  72.                     // do your stuff here
  73.                     break;
  74.                 case WindowsMessage.WM_PAINT:
  75.                 case WindowsMessage.WM_CTLCOLOREDIT:
  76.                 case WindowsMessage.WM_CTLCOLORBTN:
  77.                 case WindowsMessage.WM_CTLCOLORSTATIC:
  78.                 case WindowsMessage.WM_CTLCOLORMSGBOX:
  79.                 case WindowsMessage.WM_CTLCOLORDLG:
  80.                 case WindowsMessage.WM_CTLCOLORLISTBOX:
  81.                 case WindowsMessage.WM_CTLCOLORSCROLLBAR:
  82.                 case WindowsMessage.WM_CAPTURECHANGED:
  83.  
  84.                     break;
  85.  
  86.                 default:
  87.                     break;
  88.             }
  89.  
  90.             return nRet;
  91.         }
  92.  
  93.         struct WindowsMessage
  94.         {
  95.             public const Int32 WM_SIZE = 0x0005;
  96.             public const Int32 WM_LBUTTONDOWN = 0x0201;
  97.             public const Int32 WM_NCLBUTTONDOWN = 0x00A1;
  98.             public const Int32 WM_SYSCOMMAND = 0x112;
  99.             public const Int32 WM_PAINT = 0x000F;
  100.             public const Int32 WM_MOVE = 0x0003;
  101.             public const Int32 WM_CTLCOLORMSGBOX = 0x0132;
  102.             public const Int32 WM_CTLCOLOREDIT = 0x0133;
  103.             public const Int32 WM_CTLCOLORLISTBOX = 0x0134;
  104.             public const Int32 WM_CTLCOLORBTN = 0x0135;
  105.             public const Int32 WM_CTLCOLORDLG = 0x0136;
  106.             public const Int32 WM_CTLCOLORSCROLLBAR = 0x0137;
  107.             public const Int32 WM_CTLCOLORSTATIC = 0x0138;
  108.             public const Int32 WM_CAPTURECHANGED = 0x0215;
  109.         }
  110.  
  111.         public Form1()
  112.         {
  113.             InitializeComponent();
  114.         }
  115.  
  116.         private void Form1_Load(object sender, EventArgs e)
  117.         {
  118.             m_DefWndProcDelegate = new WndProcDelegate(DefWindowProc);
  119.             m_CtrlWndProcDelegate = new WndProcDelegate(CtrlWndProc);
  120.  
  121.  
  122.         }
  123.  
  124.         private void button1_Click(object sender, EventArgs e)
  125.         {
  126.             if (IsWindow(this.Handle))
  127.             {
  128.                 m_WndProc[this.Handle] = GetWindowLongPtr(this.Handle, -4);
  129.                 SetWindowLongPtr(this.Handle
  130.                     , -4
  131.                     , Marshal.GetFunctionPointerForDelegate(m_CtrlWndProcDelegate)
  132.                     );
  133.             }
  134.         }
  135.  
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment