C# Winform TreeView 控件以及右键 ContextMenuStrip 菜单简单测试_CMS

▲ 样式

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WF_TREE_VIEW_TEST
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            treeView1.NodeMouseDoubleClick += TreeView1_NodeMouseClick;  // 节点双击事件
            treeView1.NodeMouseClick += TreeView1_NodeMouseClick1;  // 节点单机事件
            m_treeView = new TreeViewOperator(treeView1);
            treeView1.MouseDown += TreeView1_MouseDown;  // 树控件鼠标健按下事件
            InitTreeView();

            m_CMS = new ContextMenuStrip();
            treeView1.ContextMenuStrip = m_CMS;
        }

        // 节点展开
        private void TreeView1_NodeMouseClick1(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeView treeView = sender as TreeView;
            TreeNode treeNode = treeView.GetNodeAt(e.X, e.Y);
            treeNode.Expand(); // 点击节点的时候,展开。
        }

        private void TreeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)  // 右键
            {
                treeView1.ContextMenuStrip.Tag = m_treeView.GetSelectedNodePath();

                m_CMS.Items.Clear();
                ToolStripMenuItem hi = new ToolStripMenuItem("Hello", null, tsmi_Clicked, "tsmi_Hello");
                ToolStripMenuItem hii = new ToolStripMenuItem("Hi", null, tsmi_Clicked, "tsmi_Hi");
                ToolStripSeparator separator = new ToolStripSeparator();    
      
                m_CMS.Items.Add(hi);
                m_CMS.Items.Add(separator);   // 添加分割行
                m_CMS.Items.Add(hii);

                hi.Enabled = false;   // 控制启用
            }
        }

        // 右键菜单项点击事件
        private void tsmi_Clicked(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            MessageBox.Show(item.Name + ":" + item.GetCurrentParent().Tag.ToString());
        }

        private ContextMenuStrip m_CMS;
        private readonly TreeViewOperator m_treeView;

        // 节点双击事件
        private void TreeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            MessageBox.Show(m_treeView.GetSelectedNodePath());
        }

        // 添加节点
        private void InitTreeView()
        {
            string line1 = "\\Recipe\\DIE\\9257\\三点找圆";
            m_treeView.AddTreeNode(line1, VisionBranch.Recipe);
            string line2 = "\\Recipe\\DIE\\9257\\找中心";
            m_treeView.AddTreeNode(line2, VisionBranch.Recipe);
            string line3 = "\\Recipe\\DIE\\9258\\找中心";
            m_treeView.AddTreeNode(line3, VisionBranch.Recipe);
            string line4 = "\\Recipe\\DIETRAY\\9258\\找中心";
            m_treeView.AddTreeNode(line4, VisionBranch.Recipe);

            string a = "\\SysVision\\Upcams\\cam_1\\9点标定";
            string b = "\\SysVision\\DownCams\\cam_1\\9点标定";
            m_treeView.AddTreeNode(a, VisionBranch.System);
            m_treeView.AddTreeNode(b, VisionBranch.System);
        }
    }
}// end namespace