【问题标题】:How to change the fore color of Hot Tracking in treeview in winforms?如何在winforms的treeview中更改Hot Tracking的前景色?
【发布时间】:2015-01-28 12:42:22
【问题描述】:

我有一个黑白主题的 winforms 应用程序。我有一个树视图,其背景颜色设置为黑色,文本显示为白色。我已将 hotTracking 设置为 true。现在按照以下 MSDN 链接: https://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.hottracking.aspx

HotTracking属性设置为true时,每个树节点标签 鼠标指针经过时呈现超链接的外观 超过它。下划线字体样式应用于字体和 ForeColor 设置为蓝色以使标签显示为链接。这 外观不受用户的互联网设置控制 操作系统。

似乎前景色被硬编码为蓝色。但这会在我的应用程序中产生问题,如随附的示例图像所示。由于这种蓝色,文本在黑色背景上变得不可读。虽然我可以禁用热跟踪,但客户需要它。有没有办法超越热跟踪的前景色。

这是我用于将主题应用于树视图的示例代码:

this.trvUsers.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;            
this.trvUsers.FullRowSelect = true;
this.trvUsers.HideSelection = false;
this.trvUsers.HotTracking = true;            
this.trvUsers.ShowLines = false;
this.treeView1.BackColor = System.Drawing.Color.Black;
this.treeView1.ForeColor = System.Drawing.Color.White;

【问题讨论】:

  • 您是否尝试过设置特定的 TreeNode back/Fore Color ?
  • 我的要求是,当UI加载时,背景应该是黑色的,所有的节点都应该是白色的,这很好。但是在热跟踪期间,前景色变成蓝色,这使得它不可读。我应该能够在热跟踪时更改前景色。

标签: c# winforms treeview


【解决方案1】:

您应该处理 TreeView 的 DrawNode 事件。更多信息在这里:TreeView.DrawNode Event

    private void Form1_Load(object sender, EventArgs e)
    {
        this.treeView1.Nodes.Add("Node1");
        this.treeView1.Nodes.Add("Node2");
        this.treeView1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.treeView1.FullRowSelect = true; this.treeView1.HideSelection = false; 
        this.treeView1.HotTracking = true;
        this.treeView1.ShowLines = false;
        this.treeView1.BackColor = System.Drawing.Color.Black; 

        this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
        this.treeView1.DrawNode += treeView1_DrawNode;
    }

    private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {
        Font font = e.Node.NodeFont ?? e.Node.TreeView.Font;
        Color foreColor = e.Node.ForeColor;

        if (e.State == TreeNodeStates.Hot)
        {
            foreColor = Color.Red;
        }
        else
        {
            foreColor = Color.White;
        }

        TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, foreColor, Color.Black, TextFormatFlags.GlyphOverhangPadding);            
    }

【讨论】:

    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    相关资源
    最近更新 更多