Skip to main content
added 37 characters in body
Source Link
IronGeek
  • 4.9k
  • 29
  • 28

If retaining the SystemColors.Highlight background color is all you wanted, then you don't need to set the TreeView's DrawMode property to TreeViewDrawMode.OwnerDrawAll. Setting it to TreeViewDrawMode.OwnerDrawText should be sufficient, thus you don't need to worry about drawing the TreeNode's corresponding ImageKey.

  1. Set the TreeView.DrawMode to TreeViewDrawMode.OwnerDrawText:

    treeView.DrawMode = TreeViewDrawMode.OwnerDrawText; 
  2. Set the Treview.HideSelection to false, so that the node states will be kept as selected:

    treeView.HideSelection= false; 
  3. Add DrawNode event handler to draw the background using SystemColors.Highlight color:

    private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e) { if (e.Node == null) return; // if treeview's HideSelection property is "True",  // this will always returns "False" on unfocused treeview var selected = (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected; var unfocused = !e.Node.TreeView.Focused; // we need to do owner drawing only on a selected node // and when the treeview is unfocused, else let the OS do it for us if (selected && unfocused) { var font = e.Node.NodeFont ?? e.Node.TreeView.Font; e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, SystemColors.HighlightText, TextFormatFlags.GlyphOverhangPadding); } else { e.DrawDefault = true; } } 

If retaining the SystemColors.Highlight background color is all you wanted, then you don't need to set the TreeView's DrawMode property to TreeViewDrawMode.OwnerDrawAll. Setting it to TreeViewDrawMode.OwnerDrawText should be sufficient, thus you don't need to worry about drawing the TreeNode's corresponding ImageKey.

  1. Set the TreeView.DrawMode to TreeViewDrawMode.OwnerDrawText:

    treeView.DrawMode = TreeViewDrawMode.OwnerDrawText; 
  2. Set the Treview.HideSelection to false, so that the node states will be kept as selected:

    treeView.HideSelection= false; 
  3. Add DrawNode event handler to draw the background using SystemColors.Highlight color:

    private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e) { if (e.Node == null) return; // if treeview's HideSelection property is "True", this will always returns "False" var selected = (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected; var unfocused = !e.Node.TreeView.Focused; // we need to do owner drawing only on a selected node // and when the treeview is unfocused, else let the OS do it for us if (selected && unfocused) { var font = e.Node.NodeFont ?? e.Node.TreeView.Font; e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, SystemColors.HighlightText, TextFormatFlags.GlyphOverhangPadding); } else { e.DrawDefault = true; } } 

If retaining the SystemColors.Highlight background color is all you wanted, then you don't need to set the TreeView's DrawMode property to TreeViewDrawMode.OwnerDrawAll. Setting it to TreeViewDrawMode.OwnerDrawText should be sufficient, thus you don't need to worry about drawing the TreeNode's corresponding ImageKey.

  1. Set the TreeView.DrawMode to TreeViewDrawMode.OwnerDrawText:

    treeView.DrawMode = TreeViewDrawMode.OwnerDrawText; 
  2. Set the Treview.HideSelection to false, so that the node states will be kept as selected:

    treeView.HideSelection= false; 
  3. Add DrawNode event handler to draw the background using SystemColors.Highlight color:

    private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e) { if (e.Node == null) return; // if treeview's HideSelection property is "True",  // this will always returns "False" on unfocused treeview var selected = (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected; var unfocused = !e.Node.TreeView.Focused; // we need to do owner drawing only on a selected node // and when the treeview is unfocused, else let the OS do it for us if (selected && unfocused) { var font = e.Node.NodeFont ?? e.Node.TreeView.Font; e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, SystemColors.HighlightText, TextFormatFlags.GlyphOverhangPadding); } else { e.DrawDefault = true; } } 
Fix answer
Source Link
IronGeek
  • 4.9k
  • 29
  • 28

If retaining the SystemColors.Highlight background color is all you wanted, then you don't need to set the TreeView's DrawMode property to TreeViewDrawMode.OwnerDrawAll. Setting it to TreeViewDrawMode.OwnerDrawText should be sufficient, thus you don't need to worry about drawing the TreeNode's corresponding ImageKey.

  1. Set the TreeView.DrawMode to TreeViewDrawMode.OwnerDrawText:

    treeView.DrawMode = TreeViewDrawMode.OwnerDrawText; 
  2. Set the Treview.HideSelection to false, so that the node states will be kept as selected:

    treeView.HideSelection= false; 
  3. Add DrawNode event handler to draw the background using SystemColors.Highlight color:

    private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e) { if (e.Node == null) return;    // if treeview's HideSelection property is "True", this will always returns "False" var selected = (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected; var unfocused = !e.Node.TreeView.Focused; // we need to do owner drawing only on a selected node // and when the treeview is unfocused, else let the OS do it for us if (selected && unfocused) { var font = e.Node.NodeFont ?? e.Node.TreeView.Font; e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, SystemColors.HighlightText, TextFormatFlags.GlyphOverhangPadding); } else { e.DrawDefault = true; } } 

If retaining the SystemColors.Highlight background color is all you wanted, then you don't need to set the TreeView's DrawMode property to TreeViewDrawMode.OwnerDrawAll. Setting it to TreeViewDrawMode.OwnerDrawText should be sufficient, thus you don't need to worry about drawing the TreeNode's corresponding ImageKey.

  1. Set the TreeView.DrawMode to TreeViewDrawMode.OwnerDrawText:

    treeView.DrawMode = TreeViewDrawMode.OwnerDrawText; 
  2. Add DrawNode event handler to draw the background using SystemColors.Highlight color:

    private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e) { if (e.Node == null) return; var selected = (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected; var unfocused = !e.Node.TreeView.Focused; // we need to do owner drawing only on a selected node // and when the treeview is unfocused, else let the OS do it for us if (selected && unfocused) { var font = e.Node.NodeFont ?? e.Node.TreeView.Font; e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, SystemColors.HighlightText, TextFormatFlags.GlyphOverhangPadding); } else { e.DrawDefault = true; } } 

If retaining the SystemColors.Highlight background color is all you wanted, then you don't need to set the TreeView's DrawMode property to TreeViewDrawMode.OwnerDrawAll. Setting it to TreeViewDrawMode.OwnerDrawText should be sufficient, thus you don't need to worry about drawing the TreeNode's corresponding ImageKey.

  1. Set the TreeView.DrawMode to TreeViewDrawMode.OwnerDrawText:

    treeView.DrawMode = TreeViewDrawMode.OwnerDrawText; 
  2. Set the Treview.HideSelection to false, so that the node states will be kept as selected:

    treeView.HideSelection= false; 
  3. Add DrawNode event handler to draw the background using SystemColors.Highlight color:

    private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e) { if (e.Node == null) return;    // if treeview's HideSelection property is "True", this will always returns "False" var selected = (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected; var unfocused = !e.Node.TreeView.Focused; // we need to do owner drawing only on a selected node // and when the treeview is unfocused, else let the OS do it for us if (selected && unfocused) { var font = e.Node.NodeFont ?? e.Node.TreeView.Font; e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, SystemColors.HighlightText, TextFormatFlags.GlyphOverhangPadding); } else { e.DrawDefault = true; } } 
Source Link
IronGeek
  • 4.9k
  • 29
  • 28

If retaining the SystemColors.Highlight background color is all you wanted, then you don't need to set the TreeView's DrawMode property to TreeViewDrawMode.OwnerDrawAll. Setting it to TreeViewDrawMode.OwnerDrawText should be sufficient, thus you don't need to worry about drawing the TreeNode's corresponding ImageKey.

  1. Set the TreeView.DrawMode to TreeViewDrawMode.OwnerDrawText:

    treeView.DrawMode = TreeViewDrawMode.OwnerDrawText; 
  2. Add DrawNode event handler to draw the background using SystemColors.Highlight color:

    private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e) { if (e.Node == null) return; var selected = (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected; var unfocused = !e.Node.TreeView.Focused; // we need to do owner drawing only on a selected node // and when the treeview is unfocused, else let the OS do it for us if (selected && unfocused) { var font = e.Node.NodeFont ?? e.Node.TreeView.Font; e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, SystemColors.HighlightText, TextFormatFlags.GlyphOverhangPadding); } else { e.DrawDefault = true; } }