在程序中我们会用到文本框,如果要让右键菜单按照我们的要求显示,可以用如下方法实现,自己定义Item控件来显示。

  private const int WMS= 0x007B;     protected override void WndProc(ref   Message m)              {                  if (m.Msg == WMS)                  {                      try                      {                          ContextMenu contextMenu = new ContextMenu();                            MenuItem clearUndo = new MenuItem();                          clearUndo.Text = "撤销(&U)";                          clearUndo.Click += new System.EventHandler(this.clearUndo_Click);                          contextMenu.MenuItems.Add(clearUndo);                                            this.ContextMenu = contextMenu;                          contextMenu.Tag = this;                                this.ContextMenu = contextMenu;                                              Point point = new Point(MousePosition.X, MousePosition.Y);                          ContextMenu.Show(this, point);                      }                      catch (System.Exception ex)                      { }                  }                  if (m.Msg == WMS)                  {                      return;                  }                  base.WndProc(ref   m);              }     private void clearUndo_Click(object sender, System.EventArgs e)     {         this.Undo();     }