I am trying to add some more icons to the elements of the standard System.Windows.Forms.TreeView control.
My plan was to change the shortcut area of ββthe treeview control, but this shows strange behavior. If I click on a node to select it, when I click the mouse button, the background will be correctly drawn with the highlight color. However, the text is the wrong unselected color until I release the mouse button. It is as if e.State contains an incorrect state between clicking and releasing the mouse button.
Here's what I do: I start with this.DrawMode = TreeViewDrawMode.OwnerDrawText , and then register the this.DrawNode += LayoutTreeView_DrawNode event handler. 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 the default case ...
void LayoutTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e) { e.DefaultDraw = true; }
... the same thing happens, which is strange, because the windows actually draw it now. This behavior has been observed in Windows XP with .Net 3.5.
Is there any way around this weird behavior?
source share