如何在 winforms 中的 treeview 中更改 Hot Tracking 的前景色?
How to change the fore color of Hot Tracking in treeview in winforms?
我有一个具有黑白主题的 winforms 应用程序。我有一个树视图,其背景颜色设置为黑色,文本显示为白色。我已将 hotTracking 设置为 true。现在根据以下 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.
前景色似乎被硬编码为蓝色。但这会在我的应用程序中产生问题,如所附示例图像所示。由于这种蓝色,文本在黑色背景上变得不可读。虽然我可以禁用热跟踪,但客户需要它。有没有办法覆盖热跟踪的前景色。
这是我用来将主题应用到树视图的示例代码:
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;
您应该处理 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);
}
我有一个具有黑白主题的 winforms 应用程序。我有一个树视图,其背景颜色设置为黑色,文本显示为白色。我已将 hotTracking 设置为 true。现在根据以下 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.
前景色似乎被硬编码为蓝色。但这会在我的应用程序中产生问题,如所附示例图像所示。由于这种蓝色,文本在黑色背景上变得不可读。虽然我可以禁用热跟踪,但客户需要它。有没有办法覆盖热跟踪的前景色。
这是我用来将主题应用到树视图的示例代码:
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;
您应该处理 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);
}