Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- //author ibennz
- namespace wnprocsharp
- {
- public partial class Form1 : Form
- {
- //http://www.pinvoke.net/default.aspx/user32/GetWindowLongPtr.html
- [DllImport("user32.dll", EntryPoint = "GetWindowLongW")]
- private static extern int GetWindowLongPtr32(IntPtr hWnd, int nIndex);
- [DllImport("user32.dll", EntryPoint = "GetWindowLongPtrW")]
- private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
- public static IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex)
- {
- if (IntPtr.Size == 8)
- return GetWindowLongPtr64(hWnd, nIndex);
- else
- return new IntPtr(GetWindowLongPtr32(hWnd, nIndex));
- }
- [DllImport("user32.dll")]
- public static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
- [DllImport("user32.dll")]
- public static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
- //http://pinvoke.net/default.aspx/user32/SetWindowLongPtr.html
- public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
- {
- if (IntPtr.Size == 8)
- return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
- else
- return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
- }
- [DllImport("user32.dll", EntryPoint = "SetWindowLongW")]
- private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);
- [DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW")]
- private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
- [DllImport("user32.dll")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool IsWindow(IntPtr hWnd);
- public delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
- public static WndProcDelegate m_CtrlWndProcDelegate;
- public static WndProcDelegate m_DefWndProcDelegate;
- public static Dictionary<IntPtr, IntPtr> m_WndProc = new Dictionary<IntPtr, IntPtr>(); //List<IntPtr> mList = new List<IntPtr>(); m_WndProc
- public static IntPtr CtrlWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
- {
- if (!m_WndProc.Keys.Contains (hWnd))
- return m_DefWndProcDelegate(hWnd, msg, wParam, lParam);
- IntPtr nRet = CallWindowProc(m_WndProc[hWnd], hWnd, msg, wParam, lParam);
- switch (msg)
- {
- case WindowsMessage.WM_SIZE:
- // do your stuff here
- break;
- case WindowsMessage.WM_PAINT:
- case WindowsMessage.WM_CTLCOLOREDIT:
- case WindowsMessage.WM_CTLCOLORBTN:
- case WindowsMessage.WM_CTLCOLORSTATIC:
- case WindowsMessage.WM_CTLCOLORMSGBOX:
- case WindowsMessage.WM_CTLCOLORDLG:
- case WindowsMessage.WM_CTLCOLORLISTBOX:
- case WindowsMessage.WM_CTLCOLORSCROLLBAR:
- case WindowsMessage.WM_CAPTURECHANGED:
- break;
- default:
- break;
- }
- return nRet;
- }
- struct WindowsMessage
- {
- public const Int32 WM_SIZE = 0x0005;
- public const Int32 WM_LBUTTONDOWN = 0x0201;
- public const Int32 WM_NCLBUTTONDOWN = 0x00A1;
- public const Int32 WM_SYSCOMMAND = 0x112;
- public const Int32 WM_PAINT = 0x000F;
- public const Int32 WM_MOVE = 0x0003;
- public const Int32 WM_CTLCOLORMSGBOX = 0x0132;
- public const Int32 WM_CTLCOLOREDIT = 0x0133;
- public const Int32 WM_CTLCOLORLISTBOX = 0x0134;
- public const Int32 WM_CTLCOLORBTN = 0x0135;
- public const Int32 WM_CTLCOLORDLG = 0x0136;
- public const Int32 WM_CTLCOLORSCROLLBAR = 0x0137;
- public const Int32 WM_CTLCOLORSTATIC = 0x0138;
- public const Int32 WM_CAPTURECHANGED = 0x0215;
- }
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- m_DefWndProcDelegate = new WndProcDelegate(DefWindowProc);
- m_CtrlWndProcDelegate = new WndProcDelegate(CtrlWndProc);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (IsWindow(this.Handle))
- {
- m_WndProc[this.Handle] = GetWindowLongPtr(this.Handle, -4);
- SetWindowLongPtr(this.Handle
- , -4
- , Marshal.GetFunctionPointerForDelegate(m_CtrlWndProcDelegate)
- );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment