The goal is to ensure that the text of the TreeView nodes is drawn with its own code. As follows from the description, for this you need to set the DrawMode property in OwnerDrawText and write code to handle the DrawNode event. However, I was faced with the situation that decisions in the forehead do not lead to the expected effect in the proper measure, i.e. There are some shifts, artifacts or text alignment problems.
For example, the following code:
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) { if ((e.State & TreeNodeStates.Selected) != 0) { e.Graphics.FillRectangle(Brushes.Green, e.Node.Bounds); Font nodeFont = e.Node.NodeFont; if (nodeFont == null) nodeFont = ((TreeView)sender).Font; e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, e.Bounds); } else { e.DrawDefault = true; } } leads to the loss of the last character of the text when selected in the child nodes and the text slightly shifted to the left:
From the answer to a similar topic on enSO, this can be explained by the need to use TextRenderer.DrawText() instead of Graphics.DrawString() . But in this case there is also some shift of the text to the left. Code:
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) { if ((e.State & TreeNodeStates.Selected) != 0) { e.Graphics.FillRectangle(Brushes.Green, e.Node.Bounds); Font nodeFont = e.Node.NodeFont; if (nodeFont == null) nodeFont = ((TreeView)sender).Font; TextRenderer.DrawText(e.Graphics, e.Node.Text, nodeFont, e.Node.Bounds, Color.White); } else { e.DrawDefault = true; } } gives the following (almost appropriate) result:
In general, I want to understand how to do everything right, and ideally find the code that provides the drawing when e.DrawDefault = true; . In this case, you can tweak it to fit your needs.

