When the user focus leaves the Treeview, the selected node of the Treeview is still highlighted, but the color is too light to be seen.
Override drawmode () here to control the color of the selected node.
Treeview. hideselection = false; the selected node can be highlighted.
Refer: 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 draw node event, draw by yourself
private void treeview1_drawnode ( Object sender, drawtreenodeeventargs e)
{< br> E. drawdefault = true ; /// use the default color here, you only need to highlight the selected node when the Treeview loses focus
return ;
If (E. State & treenodestates. Selected )! = 0 )
{< br> /// White text with green background
E. graphics. fillrectangle (brushes. darkblue, 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 );
}
}
}
URL: http://greatverve.cnblogs.com/archive/2012/03/21/treeview-HideSelection.html