When the user focus leaves the TreeView, the TreeView selection node is still highlighted, but the color is too light to be seen.
Here, rewrite the DrawMode () to control the color of the selected node.
Treeview.hideselection = FALSE; Allows the selected node to remain highlighted.
Reference: Http://msdn.microsoft.com/zh-cn/library/system.windows.forms.treeview.hideselection (vs.80). aspx
Public Frmmain () {InitializeComponent ();
Treeview1.hideselection = False;
Self-drawn this.treeView1.DrawMode = Treeviewdrawmode.ownerdrawtext;
This.treeView1.DrawNode + = new Drawtreenodeeventhandler (Treeview1_drawnode); ///In the Drawing node event, draw the private void Treeview1_drawnode (object sender, Drawtreenodeeventargs e) {E.drawdefault = True, as you want;
I use the default color here, just want to be in the TreeView lose focus when the selected node still highlighted return; if ((E.state & treenodestates.selected)!= 0) {//Demo for White E.graphics.fillrectangle (Brushes.darkblu
E, e.node.bounds);
Font Nodefont = E.node.nodefont; if (Nodefont = = null) Nodefont = (TreeView) sender).
Font;
E.graphics.drawstring (E.node.text, Nodefont, Brushes.white,rectangle.inflate (e.bounds, 2, 0));
else {E.drawdefault = true;
} if ((E.state & treenodestates.focused)!= 0) {using (Pen Focuspen = new Pen (color.black)) {Focuspen.dashstyle = System.Drawing.DrawinG2D.DashStyle.Dot;
Rectangle focusbounds = e.node.bounds;
Focusbounds.size = new Size (focusbounds.width-1, focusbounds.height-1);
E.graphics.drawrectangle (Focuspen, focusbounds); }
}
}