将项目添加到默认的TextBox上下文菜单

5
有没有办法在不创建自己的菜单的情况下向默认的WinForms TextBox上下文菜单中添加额外的项?
3个回答

2

继承TextBox(从它派生)或原生句柄(使用NativeWindow),然后按照以下方式重写窗口过程:

protected override void WndProc(ref Message m)
{
  if (m.Msg == <your menu id>) { ... return; }
  ...

  if (m.Msg == 0x0093 /*WM_UAHINITMENU*/ || m.Msg == 0x0117 /*WM_INITMENUPOPUP*/ || m.Msg == 0x0116 /*WM_INITMENU*/)
  {
    IntPtr shortcut = m.Msg == 0x0093 ? Marshal.ReadIntPtr(m.LParam) : m.WParam;
    // add <your menu id> to shortcut
    ...
  }
  ...
  base.WndProc(ref m);
}

1
我认为你应该重写WndProc并捕获文本框接收到的消息。

1

这是可能的,但比较复杂。我建议您使用“现代”的ContextMenuStrip类来实现自己的菜单,而不是标准的ContextMenu。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接