美文网首页
鼠标右键菜单实现

鼠标右键菜单实现

作者: 清远_03d9 | 来源:发表于2019-08-18 19:56 被阅读0次

    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;
          }
        }
    

    相关文章

      网友评论

          本文标题:鼠标右键菜单实现

          本文链接:https://www.haomeiwen.com/subject/tmkdsctx.html