鼠标右键菜单实现

2019-08-18  本文已影响0人  清远_03d9

1.初始化PopupMenu

 private PopupMenu popupMenu1;
 this.popupMenu1 = new PopupMenu(this.components);
 this.popupMenu1.BeginInit();
 this.popupMenu1.LinksPersistInfo.AddRange(new LinkPersistInfo[4]
      {
        new LinkPersistInfo((BarItem) this.barButtonItem1Run),
        new LinkPersistInfo((BarItem) this.barButtonItem1Delete),
        new LinkPersistInfo((BarItem) this.barButtonItem1Rename),
        new LinkPersistInfo((BarItem) this.barButtonItem1Refresh)
      });
      this.popupMenu1.Manager = this.barManager1;
      this.popupMenu1.Name = "popupMenu1";
      this.popupMenu1.EndInit();

2. 在控件MouseDown上增加右键监听事件

 private void AntgisToolBoxTree_MouseDown(object sender, MouseEventArgs e)
    {
      TreeListHitInfo treeListHitInfo = this.CalcHitInfo(new Point(e.X, e.Y));
      TreeListNode node = treeListHitInfo.Node;
      if (treeListHitInfo.HitInfoType == HitInfoType.Button)
        return;
      this.OptionsBehavior.Editable = false;
      if (e.Button == MouseButtons.Right)
      {
        if (node == null)
        {
          this.FocusedNode = (TreeListNode) null;
          this.CloseEditor();
          this.Selection.Clear();
          this.popupMenu3.ShowPopup(Control.MousePosition);
        }
        else
        {
          this.FocusedNode = node;
          if (!node.HasChildren)
          {
            if (node.StateImageIndex <= 1)
              this.popupMenu2.ShowPopup(Control.MousePosition);
            else
              //右键单击时弹出popup
              this.popupMenu1.ShowPopup(Control.MousePosition);
          }
          else
            this.popupMenu2.ShowPopup(Control.MousePosition);
        }
      }
      else if (e.Button == MouseButtons.Left)
      {
        if (node == null)
        {
          this.CloseEditor();
          this.Selection.Clear();
        }
        else if (e.Clicks != 2)
          node.Expanded = !node.Expanded;
      }
    }
上一篇下一篇

猜你喜欢

热点阅读