1
votes

enter image description here

I have a winforms application which is having Black-White theme. I have a treeview, whose background color is set to black and text appears white. I have set hotTracking to true. Now as per the following MSDN link: https://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.hottracking.aspx

When the HotTracking property is set to true, each tree node label takes on the appearance of a hyperlink as the mouse pointer passes over it. The Underline font style is applied to the Font and the ForeColor is set to blue to make the label appear as a link. The appearance is not controlled by the Internet settings of the user's operating system.

It seems that the fore-color is hard-coded to Blue. But this creates issue in my application, as shown in the attached sample image. Due to this blue color, the text becomes un-readable on a black background. Although I can disable hot-tracking, but client needs it. Is there a way to over-ride the fore-color of hot tracking.

Here is my sample code that I use to apply theme to tree view:

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;
1
Have you tried setting the specific TreeNode back/Fore Color ?Rohit Prakash
my requirement is, when the UI loads, background should be black, and all the nodes should appear white, which is fine. But during hot tracking, the fore color becomes Blue, which makes it un-readable. I should be able to change the fore color while hot tracking.Shah Faisal

1 Answers

3
votes

You should handle TreeView's DrawNode event. More information here: 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);            
    }