TreeView with custom drawn TreeNode

2020-05-03 12:29发布

I am trying to add a custom icon near the text of a TreeNode, so the items could have a "checked/unchecked" state displayed. I don't want to use a checkbox for that.

Any ideas? Thanks

1条回答
一纸荒年 Trace。
2楼-- · 2020-05-03 12:58

Assuming you are using .net and Windows Forms.

You must set DrawMode property of TreeView to TreeViewDrawMode.OwnerDrawAll. Once you do this, treeview's DrawNode event will fire each time a tree node is being drawn. Handle that event and draw your items manually.

You will get DrawTreeNodeEventArgs as the event arguments. State property of it will tell you which state of the tree item you must draw. e.Bounds will help you for determining bounds and you can use e.Graphics for drawing. You can find more detailed information here:

http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.drawnode.aspx

But prepare to invest multiple hours.

查看更多
登录 后发表回答