【问题标题】:Add item to the default TextBox context menu将项目添加到默认的 TextBox 上下文菜单
【发布时间】:2009-08-28 11:39:58
【问题描述】:

有没有什么方法可以在不创建我自己的情况下向默认的 WinForms TextBox 上下文菜单添加额外的项目?

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    子类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);
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-24
          • 2010-10-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-08
          • 2016-03-05
          • 2013-03-26
          相关资源
          最近更新 更多