简体   繁体   English

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

[英]Add item to the default TextBox context menu

有没有办法在没有自己创建的情况下将额外的项添加到默认的WinForms TextBox上下文菜单中?

Subclass TextBox (derive from it) or native handle (with NativeWindow), and then override window procedure as follows: 子类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);
}

我认为你应该覆盖WndProc并捕获文本框接收的消息。

It is possible, but complicated. 这是可能的,但很复杂。 I suggest you implement your own menu using "modern" ContextMenuStrip class instead of standard ContextMenu. 我建议您使用“现代”ContextMenuStrip类而不是标准ContextMenu来实现自己的菜单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM