Custom TreeNode bounds

狂风中的少年 提交于 2021-02-08 04:37:25

问题


How to extend the standard mouse behavior of a TreeView node (like selecting, hot tracking, expansion on double click etc.) over a wider node area? I've got a custom-drawn node that has a wider label than it would have if it was system-drawn and it only reacts to mouse actions when the cursor is within the ordinary node bounds.

Any Ideas?

BTW, intercepting TVM_GETITEMRECT and TVM_HITTEST won't do the trick.

A few words about what I'm doing: I'm developing a WinForms tree control that normally acts exactly the same as WinForms TreeView, but also allows for adding special nodes that can have custom fonts and colors applied to any parts of their labels. So it's kinda "RichTreeView" that can for example have nodes with bolded single words in their text. With some heavy font tinkering a node label can get way wider than regular, but also narrower (which causes another problem of suppressing mouse actions), hence my question.


回答1:


Yes, this is a problem with the native Windows control. It doesn't have any way to specify a custom item bounds. The TVN_GETDISPINFO notification is probably closest, but NMTVDISPINFO is missing a way to specify the bounds. The .NET wrapper doesn't implement it. It is really only good enough to implement a VirtualMode.

That keeps hit testing and TreeNode.Bounds based on the original metrics. It is especially a problem with the horizontal scrollbar, it won't scroll far enough to reveal all the custom-drawn text if it is wider than the original. You can somewhat solve the hit testing problem by implementing MouseDown and iterating the TreeNode.Bounds properties but not the scrollbar problem.

There's no clean fix for this, only an fugly one. Set the TreeNode.Text to an arbitrary string that's wide enough to solve the problem. You could use the Tag property to store the actual text, if necessary. It depends on how custom you draw, prefixing the Text with a couple of M's might be good enough. Not pretty.



来源:https://stackoverflow.com/questions/4545931/custom-treenode-bounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!