Add item to the default TextBox context menu

前端 未结 3 1889
暖寄归人
暖寄归人 2020-12-11 06:26

is there any way to add an extra item to the default WinForms TextBox context menu without creating my own?

相关标签:
3条回答
  • 2020-12-11 06:51

    Subclass TextBox (derive from it) or native handle (with NativeWindow), and then override window procedure as follows:

    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);
    }
    
    0 讨论(0)
  • 2020-12-11 06:55

    I think you should override WndProc and capture the messages that the textbox receive.

    0 讨论(0)
  • 2020-12-11 07:01

    It is possible, but complicated. I suggest you implement your own menu using "modern" ContextMenuStrip class instead of standard ContextMenu.

    0 讨论(0)
提交回复
热议问题