コード例 #1
0
        public WpfTreeNode AddWPFTreeNode(string text)
        {
            WpfTreeNode node = new WpfTreeNode(text);

            this.Nodes.Add(node);
            return(node);
        }
コード例 #2
0
        public WpfTreeNode AddWPFTreeNode(string text, ButtonMenu buttons)
        {
            WpfTreeNode node = new WpfTreeNode(text, buttons);

            this.Nodes.Add(node);
            return(node);
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: pengfeiw/wpftreeview
        public Form1()
        {
            InitializeComponent();

            ButtonMenu bm1 = new ButtonMenu(this.wpfTreeView1);
            ButtonMenu bm2 = new ButtonMenu(this.wpfTreeView1);

            bm1.AddButtonItems(new ButtonItem(newImage, "New File", new ButtonItemClickEventHandler(newFileClick)));
            bm1.AddButtonItems(new ButtonItem(openImage, "Open File", new ButtonItemClickEventHandler(openFileClick)));
            bm2.AddButtonItems(new ButtonItem(saveImage, "Save File", new ButtonItemClickEventHandler(saveFileClick)));
            bm2.AddButtonItems(new ButtonItem(deleteImage, "Delete File", new ButtonItemClickEventHandler(deleteFileClick)));
            bm2.AddButtonItems(new ButtonItem(copyImage, "Save File", new ButtonItemClickEventHandler(copyFileClick)));

            WpfTreeNode root     = this.wpfTreeView1.AddWPFTreeNode("WpfTreeView Demo");
            WpfTreeNode child1   = root.AddWPFTreeNode("节点1", bm1);
            WpfTreeNode child1_1 = child1.AddWPFTreeNode("节点1-1", bm2);

            child1_1.AddWPFTreeNode("1");
            child1_1.AddWPFTreeNode("2");
            child1_1.AddWPFTreeNode("3");
            child1.AddWPFTreeNode("节点1-2", bm2);
            WpfTreeNode child2 = root.AddWPFTreeNode("节点2", bm1);

            child2.AddWPFTreeNode("节点2-1", bm2);
            WpfTreeNode child3 = root.AddWPFTreeNode("节点3", bm1);

            child3.AddWPFTreeNode("节点2-1", bm2);

            this.wpfTreeView1.ExpandAll();
        }
コード例 #4
0
        protected override void OnDrawNode(DrawTreeNodeEventArgs e)
        {
            Font nodeFont   = e.Node.NodeFont == null ? this.Font : e.Node.NodeFont;
            int  fontHeight = nodeFont.Height;

            e.Graphics.DrawString(e.Node.Text, nodeFont, new SolidBrush(this.ForeColor), new Point(e.Node.Bounds.Location.X, e.Node.Bounds.Location.Y + e.Node.Bounds.Height / 2 - fontHeight / 2));

            WpfTreeNode customNode = e.Node as WpfTreeNode;

            if (customNode != null && customNode.ButtonRight == true)
            {
                Graphics graphics = e.Graphics;

                for (int i = 0; i < customNode.buttonMenu.buttonItems.Count; i++)
                {
                    Rectangle btnBounds = getBtnBoundsOfNode(customNode, i);
                    Image     image     = getBtnImageOfNode(customNode, i);
                    graphics.DrawImage(image, btnBounds);

                    //Draw tip
                    if (customNode.buttonMenu.buttonItems[i]._mouseHover)
                    {
                        string tipStr = customNode.buttonMenu.buttonItems[i].tip;
                        if (tipStr != null && tipStr != "")
                        {
                            Brush brush1 = new SolidBrush(Color.White);
                            Brush brush2 = new SolidBrush(Color.Gray);
                            Pen   pen    = new Pen(Color.FromArgb(197, 197, 197));
                            Font  font   = new Font("微软雅黑", 10);

                            Rectangle lastBtnBounds = getBtnBoundsOfNode(customNode, customNode.buttonMenu.buttonItems.Count - 1);

                            Point textLocation = new Point(lastBtnBounds.Location.X + lastBtnBounds.Width, lastBtnBounds.Location.Y - 30);
                            SizeF textSize     = graphics.MeasureString(tipStr, font);

                            PointF location = new PointF(textLocation.X - 3, textLocation.Y - 2);

                            RectangleF rect = new RectangleF(location, new SizeF(textSize.Width + 6, textSize.Height + 4));
                            graphics.FillRectangle(brush1, rect);
                            graphics.DrawRectangle(pen, rect.Location.X, rect.Location.Y, rect.Width, rect.Height);
                            graphics.DrawString(tipStr, font, brush2, textLocation);
                        }
                    }
                }
            }
        }
コード例 #5
0
        protected override void OnMouseDown(MouseEventArgs e)
        {
            WpfTreeNode tnode = GetNodeAt(e.Location) as WpfTreeNode;

            if (tnode == null)
            {
                return;
            }

            for (int i = 0; i < tnode.buttonMenu.buttonItems.Count; i++)
            {
                Rectangle btnBounds = getBtnBoundsOfNode(tnode, i);
                if (btnBounds.Contains(e.Location))
                {
                    tnode.BtnClick(i);
                    break;
                }
            }
        }
コード例 #6
0
        private void MouseMoveListener(object sender, MouseEventArgs e)
        {
            List <TreeNode> nodes = this.GetAllNodes();

            foreach (TreeNode node in nodes)
            {
                WpfTreeNode customTreeNode = node as WpfTreeNode;
                if (customTreeNode != null)
                {
                    Rectangle nodeBounds = getNodeBounds(customTreeNode);
                    if (e.Location.X >= nodeBounds.X && e.Location.X <= nodeBounds.X + nodeBounds.Width && e.Location.Y >= nodeBounds.Y && e.Location.Y <= nodeBounds.Y + nodeBounds.Height)
                    {
                        //Whether mouse is hover on the button
                        foreach (ButtonItem item in customTreeNode.buttonMenu.buttonItems)
                        {
                            item._mouseHover = false;
                        }
                        for (int i = 0; i < customTreeNode.buttonMenu.buttonItems.Count; i++)
                        {
                            Rectangle btnBounds = getBtnBoundsOfNode(customTreeNode, i);
                            if (btnBounds.Contains(e.Location))
                            {
                                customTreeNode.buttonMenu.buttonItems[i]._mouseHover = true;
                            }
                        }

                        if (customTreeNode.mouseHover == false)
                        {
                            customTreeNode.mouseEnter();
                        }
                    }
                    else
                    {
                        if (customTreeNode.mouseHover == true)
                        {
                            customTreeNode.mouseLeave();
                        }
                    }

                    this.Invalidate();
                }
            }
        }
コード例 #7
0
        private Image getBtnImageOfNode(WpfTreeNode node, int btnIndex)
        {
            Image image = node.buttonMenu.buttonItems[btnIndex].image;

            return(image);
        }