C#屏蔽ComboBox系统右键菜单介绍(通过API拦截消息)

C#屏蔽ComboBox系统右键菜单代码如下:

  
  
  
  
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)); }
  
  
  
  
 
C#创建右键菜单

范例说明:本范例讲解如何创建右键菜单,重点请注意菜单控件ContextMenuStrip的使用.

关键步骤:

1.创建一个新的Windows工程。

2.把ContextMenuStrip控件拖放到设计界面上,Name属性设置为cmMenu.

3.单击ContextMenuStrip控件的Type Here文本区域,可以输入菜单项的名称,如&new,然后回车.

4.添加RichTextBox控件,设置Name属性为"rchShow".

设置Form和RichTextBox的ContextMenuStript属性为“cmMenu”。

7.双击ContextMenuStrip控件的菜单项,添加Open事件。

/// <summary>
/// 右键菜单Open的处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void miOpen_Click(object sender, EventArgs e)
{
OpenFileDialog dlgOpen = new OpenFileDialog();

dlgOpen.Filter = "文本文件(*.txt)|*.txt";

if (dlgOpen.ShowDialog() == DialogResult.OK)
{
this.rchShow.LoadFile(dlgOpen.FileName,RichTextBoxStreamType.PlainText);
}
}

 

private void Form1_Load(object sender, EventArgs e)
{
ContextMenuStrip cms = new ContextMenuStrip();
cms.Items.Add("第一个");
cms.Items.Add("第二个");
this.ContextMenuStrip = cms;
}

你可能感兴趣的:(C#屏蔽ComboBox系统右键菜单介绍(通过API拦截消息))