I'm trying to add a few more icons to elements of a standard System.Windows.Forms.TreeView control.
My plan was to only change the label area of the treeview control, but it shows a strange behaviour. If I click a node to select it, when the mouse button is depressed the background is draw correctly with the highlight color. However, the text is the wrong unselected color until I release the mouse button. It's as if e.State
contains the wrong state between when the mouse button is pressed and released.
Here is what I'm doing: I init with this.DrawMode = TreeViewDrawMode.OwnerDrawText
and then register my event handler with this.DrawNode += LayoutTreeView_DrawNode
. Here is the handler:
void LayoutTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
Color color = (e.State & TreeNodeStates.Selected) != 0 ?
SystemColors.HighlightText : SystemColors.WindowText;
TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.SingleLine |
TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis;
TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, color, flags);
}
If I set the handler to its default case...
void LayoutTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
e.DefaultDraw = true;
}
...the same thing happens, which is weird since windows is actually drawing it now. This behaviour is in Windows XP with .Net 3.5.
Is there any way to work around this strange behaviour?