当前位置: 编程技术>.net/c#/asp.net
C#屏蔽ComboBox系统右键菜单的实现代码
来源: 互联网 发布时间:2014-08-30
本文导语: 代码如下: //C#屏蔽ComboBox系统右键菜单 //by http://www. using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); public const uint GW_CHILD= 5; private delegate IntPtr WndProcCallBack(IntPtr hwnd, int Ms...
代码如下:
//C#屏蔽ComboBox系统右键菜单 //by http://www. using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); public const uint GW_CHILD= 5; private delegate IntPtr WndProcCallBack(IntPtr hwnd, int Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll")] private static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("User32.dll")] private static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); private int GWL_WNDPROC= -4; private static IntPtr prevWndFunc; private IntPtr newWndProc(IntPtr hWnd, int iMsg, IntPtr wParam, IntPtr lParam) { const int WM_CONTEXTMENU = 0x007B; switch (iMsg) { case WM_CONTEXTMENU: return IntPtr.Zero; } return CallWindowProc(prevWndFunc, hWnd, iMsg, wParam, lParam); } private IntPtr comboBoxEdit; private WndProcCallBack wndProcCallBack; private void Form1_Load(object sender, EventArgs e) { comboBoxEdit = GetWindow(comboBox1.Handle, GW_CHILD); prevWndFunc = new IntPtr(GetWindowLong(comboBoxEdit, GWL_WNDPROC)); wndProcCallBack = new WndProcCallBack(newWndProc); SetWindowLong(comboBoxEdit, GWL_WNDPROC, (int)Marshal.GetFunctionPointerForDelegate(wndProcCallBack)); }