C#:如何绘制在一个可滚动,可缩放的面板非常大的控制?缩放、非常大、面板

2023-09-07 09:09:00 作者:一颗心在瞬间死掉

想象一下,一个条形图单杠,可能会很宽。我有我想要显示这些酒吧和滚动和缩放他们窗体上的面板。的形式,因而面板,可以调整大小。该酒吧被动态地从数据库中创建。每个用户放大或缩小的时候,所有的酒吧都必须重新创建,调整其大小的面板上。

Imagine a bar graph with horizontal bars that may be very wide. I have a Panel on a Form where I want to display these bars and scroll and zoom them. The Form, and thus the Panel, can be resized. The bars are dynamically created from a database. Each time the user zooms in or out, all bars have to be created anew to adjust their sizes on the Panel.

我用Label控件来创建这些吧,但问题适用于所有其他控制以及:如果我放大到足够程度,我的酒吧将最终超过控制尺寸(> 65536像素)的神奇16位的边界。这使得不可能简单地在面板在启动上创建的所有的控制和让面板处理滚动。

I use Label controls to create these bars, but the problem applies to all other controls as well: If I zoom in far enough, my bars will eventually exceed the magic 16 bit border of control sizes (>65536 pixels). This makes it impossible to simply create all the controls on the panel at start and let the panel handle the scrolling.

我的想法:清除所有栏控件的面板,并创建只有那些在当前视图窗口中可见,根据滚动条的当前位置和缩放级别。从可见光视图超过迄今为止杆将被缩短刚小组之外,因此它们的最大尺寸由面板尺寸的限制。

My idea: Clear the Panel of all bar controls and create only the ones that are visible in the current view window, according to the current position of the scroll bars and the zoom level. The bars exceeding far from the visible view will be cut short just outside the Panel, so their maximum size is limited by the Panel size.

我的问题:

在该面板的事件(S)这应该清楚/创建过程中发生的最好? 可能有数以千计的控制,所以应该尽可能少。 有没有更好的方式来处理呢?也许我完全搞错了从一开始。 At which Panel event(s) should this clear/create process take place best? There could be thousands of controls, so it should be as seldom as possible. Is there a better way to handle this? Maybe I got it all wrong from the start.

这个问题不仅具有巨大的控制,还要在更小的控制是相距甚远(> 65536像素)的一个小组,所以我觉得一个很好的解决方案可能是有帮助的许多项目。

This problem arises not only with huge controls but also when smaller controls are very far apart (>65536 pixels) on a Panel, so I think a good solution may be helpful for many projects.

推荐答案

我不希望有创建/销毁的控制,或隐藏/调整大小控制只为他们的点击事件。这是很容易地创建一个用户控件并覆盖的OnPaint 方法来绘制的条形图,并重写的OnClick OnMouseXxx 事件。

I wouldn't like to have to create / destroy controls, or hide / resize controls just for their click events. It's quite easy to create a UserControl and override the OnPaint method to draw the bars, and override the OnClick or OnMouseXxx events.

既然你已经知道的虚拟空间酒吧的位置,可以很容易地将鼠标光标的位置映射到一间酒吧(或酒吧外的点击)。

Since you already know the positions of the bars in "virtual space", it's easy to map the location of the mouse cursor to a bar (or a click outside a bar).