Phantom C # ownerdraw treeview

Having difficulty understanding treeview treeview, here is the full story:

VS2013 WinForms application (works in Windows 8.1 with TrueType enabled, if that matters ...) with a tree using: DrawMode = OwnerDrawText;

When the form loads, some nodes are added to the tree:

private void Form1_Load(object sender, EventArgs e) { // add some nodes for (int i = 0; i < 20; i++) { TreeNode treeNode = treeView1.Nodes.Add(new String('i', 60)); treeNode.Tag = i; } } 

Next, I draw the entire node myself to show the problem:

  private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) { // use ownerdraw every other item if ((int)(e.Node.Tag) % 2 == 0) { Font font = e.Node.NodeFont; if (font == null) font = e.Node.TreeView.Font; e.Graphics.DrawString(e.Node.Text, font, Brushes.Red, e.Bounds.X, e.Bounds.Y); } else { e.DrawDefault = true; } } 

Look at the results, pay attention to the fact that the nodes of the owner (red) of the elements have an intersymbol spacing different from when the tree structure draws its own nodes. And after some time, the interval will suddenly change. Am I using the wrong font here? Am I missing something?

Thank you for your time.

+6
source share
1 answer

Using TextRenderer.DrawText instead of Graphics.DrawString should fix this. Ian Boyd posted a wonderful answer about the difference between the two and why the text might appear when doing custom drawing.

I contemplated a quote from part of his answer here, but actually, if you are doing a custom drawing, you really should read the whole answer, because every part of it is important when it comes to drawing text - especially when drawing only a part of the text on the control , not all drawings.

+8
source

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


All Articles