treeview 鼠标划过显示红色,选择了显示系统缺省蓝色。

https://social.msdn.microsoft.com/Forums/windows/en-US/88382d08-8741-4223-9390-a7df5f946c17/treenode-mouseover-effect?forum=winforms

非常好。测试通过。

public class SharxXTreeView : TreeView
    {
        public SharxXTreeView()
            :base()
        {
            this.HotTracking = true;
            this.DrawMode = TreeViewDrawMode.OwnerDrawText;
            this.DrawNode += new DrawTreeNodeEventHandler(SharxXTreeView_DrawNode);
        }

        void SharxXTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {   
            if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot)
            {
                // get node font and node fore color
                Font nodeFont = GetTreeNodeFont(e.Node);
                Color nodeForeColor = GetTreeNodeForeColor(e.Node, e.State);

                // fill node background
                using (SolidBrush brush = new SolidBrush(Color.Red))
                {
                    e.Graphics.FillRectangle(brush, e.Bounds);
                }

                // draw node text
                TextRenderer.DrawText(e.Graphics, e.Node.Text, nodeFont, e.Bounds, nodeForeColor, TextFormatFlags.Left | TextFormatFlags.Top);

                // draw selected node border
                if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected)
                {
                    using (Pen pen = new Pen(nodeForeColor))
                    {
                        pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                        Rectangle penBounds = e.Bounds;
                        penBounds.Width -= 1;
                        penBounds.Height -= 1;
                        e.Graphics.DrawRectangle(pen, penBounds);
                    }
                }
            }
            else
            {
                e.DrawDefault = true;
            }
        }

        private Font GetTreeNodeFont(TreeNode node)
        {
            Font nodeFont = node.NodeFont;
            if (nodeFont == null)
            {
                nodeFont = this.Font;
            }
            return nodeFont;
        }

        private Color GetTreeNodeForeColor(TreeNode node, TreeNodeStates nodeState)
        {
            Color nodeForeColor = Color.Empty;

            if ((nodeState & TreeNodeStates.Selected) == TreeNodeStates.Selected)
            {
                nodeForeColor = Color.FromKnownColor(KnownColor.HighlightText);
            }
            else
            {
                nodeForeColor = node.ForeColor;
                if (nodeForeColor == Color.Empty)
                {
                    nodeForeColor = this.ForeColor;
                }
            }

            return nodeForeColor;
        }
    }


  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值