C#屏蔽ComboBox系统右键菜单的实现代码

发布时间:2020-09-28编辑:脚本学堂
为大家介绍一个使用C#屏蔽ComboBox系统右键菜单的代码,有需要的朋友不妨参考下,希望对您有所帮助。

代码如下:

//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));
}