如何控制treeview节点文本宽度? [英] How to control treeview node text width?

查看:148
本文介绍了如何控制treeview节点文本宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中开发自己的treeview with column用户控件,但是如果树视图节点的文本内容大于我的列的宽度,那么文本内容占据了另一列的空间,看起来很丑,所以我怎么能将该文本隐藏在列表视图的宽度内。

I am developing my own "treeview with column" user control in c# but if the text content of tree view node is larger than width of my column then text content occupy the space of another column and it looks ugly so how i can hide that text within width of list view.

推荐答案

只需一种快速而又简单的解决方法就是为每个节点分配一些字符串值。它们将不可见,因为你使用了所有者绘制,但是将定义渲染的节点边界。



无论你如何控制宽度,都有一个困难的小问题:字符串宽度的测量永远不准确。我看到 System.Drawing 的几个解决方案与 System.Windows.Forms 一起使用,但这些解决方案仍然没有给我所需的准确性,所以文本仍然不完全适合(当然,这对于等宽字体不会有问题)。因此,我必须为节点保留大量的额外空间,如果注释的背景颜色与树视图的背景颜色不同,这看起来真的很难看。



总体而言, System.Windows.Forms 中使用的Windows树视图对于复杂的高度自定义控件而言看起来过时且非常丑陋;其中一个问题是与闪烁作斗争。当视频卡真的很慢时,感觉它就像设计的那样。



它带给我们另一种我用过的选择。最终,我决定通过覆盖 TreeView.OnPaint 以自定义方式绘制整个树。基类仍然保留每个节点的边界,因此您可以使用这些边界进行渲染,但是您可以忽略树节点边界的右边界。这个解决方案对我来说效果更好,因为单个节点宽度的测量超出了等式。我通过使用双缓冲完全消除了闪烁。 (我必须承认我的渲染是高度自定义的,每次更改选择和语义操作时都会对视图进行复杂的更新。对于更简单的应用程序,可能不会出现闪烁。)



这些天我可能会考虑的另一种选择是转向WPF。在WPF中,您可以完全自定义每个节点的表示。



-SA
Just one quick and dirty work-around method is to assign some string values to each node. They won't be visible, because you use owner draw, but will define rendered node bounds.

No matter how you control the width, there is one difficult little problem: the measurement of the string width is never accurate. I saw several solutions for System.Drawing used with System.Windows.Forms, but those solution still did not give me required accuracy, so the text still did not fit exactly (it would not be a problem for monospace fonts, of course). So, I had to reserve good amount of extra space for the node, which would look really ugly if the background color of the note was different from the background color of the tree view.

Overall, Windows tree view used in System.Windows.Forms looks obsolete and pretty ugly for complicated highly customized controls; one of the problems was fighting with the flicker. It feels like it was all designed when the video cards were really slow.

That brings us to another alternative I used. Ultimately, I decided to draw the whole tree in a custom way, by overriding TreeView.OnPaint. The base class still preserves bounds of each node, so you can use these bounds for your rendering, but then you can just ignore the right bound of tree node bounds. This solution works better for me, as the measurement of the individual node widths went out of equation. I completely eliminated flicker through the use of double buffering. (I must admit my rendering was highly customized, with complicated updates to the view on each change of selection and semantic operations. For simpler applications, the flicker may not appear.)

Another alternative I would probably considered first these days would be moving to WPF. In WPF, you can fully customize representation of each node.

—SA


这篇关于如何控制treeview节点文本宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆