代码如下:
//C#屏蔽ComboBox系统右键菜单 //by http://www.jb200.com 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)); }