用户控制实例数量的上限(合理) [英] Upper (reasonable) limit to number of user control instances

查看:50
本文介绍了用户控制实例数量的上限(合理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个曾经是树状视图控件的菜单,但现在我想使每个项目更加直观,并向树中的每个对象添加更多信息.

I have a menu that used to be a treeview control but now I want to make each item a bit more visual and add some more information to each object in the tree.

我的第一个意图是制作一个代表项目的用户控件,并在运行时将其添加到面板中.这是一个好方法吗?有时可能有一百多个项目.我知道理论上可以在表单上使用的控件数量最多,但这不是我主要关心的问题.我主要关心的是性能.

My first intention was to make a user control that represents an item and add them to a panel at runtime. Is this a good aproach? There could sometimes be over one hundred items. I know that there is a maximum number of controls you can theoretically hav on a form, but that is not my main concern. My concern is mainly about performance.

我正在考虑的另一种方法是制作一个列表框,并在onPaint方法中执行其他操作.但这似乎有点不稳定,而且维护起来太复杂了.

Another aproach I was thinking about was to make a listbox and do the extra stuff in the onPaint method. But that seems a bit unstable and a bit too complex to maintain.

有什么想法吗?

我已经通过将200个用户控件添加到面板att form_Load中来测试了usercontrol-approach实际添加需要花费大量时间,但除此之外似乎没有其他性能问题.滚动效果很好,我使每个用户控件都可折叠,即使面板上有大约一百个功能,功能也丝毫不会滞后.

I've tested the usercontrol-approach by adding 200 usercontrols to the panel att form_Load and it takes a fair amount of time for the actual adding but there doesn't seem to be any performance issues aother than that. Scrolling works fine and I've made each usercontrol collapsable and that functionallity doesn't lag in any way, even when there are about a hundred above and a hundred under it in the panel.

但还是...我在这里完全可以跟踪吗?

But still ... Am I totally of track here?

推荐答案

UserControl与System.Windows.Forms.Control的任何实例一样,都是非常繁重的"动物,因为每个实例都包装了实际的基础本机Win32 Window.每个窗口都需要由操作系统进行管理,进行命中测试,发送绘画消息等.

UserControls are very "heavy" animals, as is any instance of System.Windows.Forms.Control, since each one wraps an actual underlying native Win32 Window. Every Window needs to be managed by the OS, hit-tested, sent paint messages, etc.

在Windows中,此方案的传统解决方案是虚拟化"控件.而不是创建200个UserControl,而是维护代表每个项目的200个对象"的数组.创建一个代表整个菜单的大"控件,向其中添加一个ScrollBar,然后覆盖OnPaint,仅绘制可见项.

The traditional solution for this scenario in Windows is to "virtualize" the control. Instead of creating 200 UserControls, maintain an array of 200 "objects" representing each item. Create one "big" control that represents the entire menu, add a ScrollBar to it, and override OnPaint, drawing only the visible items.

这就是老式的本机控件(如ListBox和TreeView)所做的事情.

This is what the old-school native controls like ListBox and TreeView do.

现在,我相信Windows可以在这里为您提供一些帮助,具体取决于您需要获得的幻想.您要查找的关键字是所有者绘制的".从另一个答案:

Now I believe Windows can help you out a bit here, depending on how fancy you need to get. The keyword you're looking for is "owner-drawn". Cribbing from another answer:

子类ListBox.在ctor中,将绘制模式设置为OwnerDrawVariable并覆盖OnDrawItem和OnMeasureItem.

Subclass ListBox. In the ctor, set the draw mode to OwnerDrawVariable and override OnDrawItem and OnMeasureItem.

这样,本机控件将处理所有滚动和数学运算,以弄清您在列表中的位置以及从哪里开始绘制.

This way, the native controls will handle all the scrolling and math necessary to figure out where you're at in the list and where to start painting.

这篇关于用户控制实例数量的上限(合理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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