TreeView owner fails to select

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?

+4
source share
1 answer

Change

 Color color = (e.State & TreeNodeStates.Selected) != 0 ? SystemColors.HighlightText : SystemColors.WindowText; 

to

 Color color = (e.State & TreeNodeStates.Focused) != 0 ? SystemColors.HighlightText : SystemColors.WindowText; 

This worked on Vista x64 and VS 2008 with .Net 3.5. Let me know if this works for you.

What I observed when viewing the default behavior on Windows was that the text and highlight were not drawn until the node was selected and there was no focus. So I decided to check the focused state to change the color of the text. However, this does not accurately mimic the behavior of widows where new colors are not used until the mouse is released. It appears when he chooses to draw blue status changes when in ownerdrawn mode compared to the windows that draw it ... Which, admittedly, is confusing.

EDIT However, when you create your own derived tree, you have full control over when everything is drawn.

 public class MyTreeView : TreeView { bool isLeftMouseDown = false; bool isRightMouseDown = false; public MyTreeView() { DrawMode = TreeViewDrawMode.OwnerDrawText; } protected override void OnMouseDown(MouseEventArgs e) { TrackMouseButtons(e); base.OnMouseDown(e); } protected override void OnMouseUp(MouseEventArgs e) { TrackMouseButtons(e); base.OnMouseUp(e); } protected override void OnMouseMove(MouseEventArgs e) { TrackMouseButtons(e); base.OnMouseMove(e); } private void TrackMouseButtons(MouseEventArgs e) { isLeftMouseDown = e.Button == MouseButtons.Left; isRightMouseDown = e.Button == MouseButtons.Right; } protected override void OnDrawNode(DrawTreeNodeEventArgs e) { // don't call the base or it will goof up your display! // capture the selected/focused states bool isFocused = (e.State & TreeNodeStates.Focused) != 0; bool isSelected = (e.State & TreeNodeStates.Selected) != 0; // set up default colors. Color color = SystemColors.WindowText; Color backColor = BackColor; if (isFocused && isRightMouseDown) { // right clicking on a color = SystemColors.HighlightText; backColor = SystemColors.Highlight; } else if (isSelected && !isRightMouseDown) { // if the node is selected and we're not right clicking on another node. color = SystemColors.HighlightText; backColor = SystemColors.Highlight; } using (Brush sb = new SolidBrush(backColor)) e.Graphics.FillRectangle(sb,e.Bounds); TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis; TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, color, backColor, flags); } } 
+3
source

Source: https://habr.com/ru/post/1303603/


All Articles