问题描述
C#3.0,.net框架3.5
我在Windows窗体上垂直绘制了许多实心矩形(使用graphics类中的draw方法)。表单开始于500 x 500 px,并且仅在从网上下载数据后才在运行时绘制矩形-矩形的数量取决于下载,因此我不预先知道。
C#3.0,.net framework 3.5I am drawing ( using the draw method in the graphics class) a lot of solid rectangles on a windows form vertically. The form starts at 500 x 500 px and the rectangles are only drawn at runtime after data is downloaded from the net -and the number of rectangles depends on the download so I do not know it upfront.
因此,由于表单的大小是固定的,因此只绘制了几个矩形。
所以我用谷歌搜索/ Binged(以免有人建议我这样做)并发现了一些技巧,但在这种情况下它们不起作用-例如将Forms AutoScroll属性设置为true或尝试双缓冲。我还尝试绘制
So only a few rectangles are drawn as the size of the form is fixed.So I googled/Binged ( lest someone suggest I do that) and found a few tips but they don't work in this case -like setting the forms AutoScroll property to true or trying double buffering.I also tried to draw on a listbox control and set it's scroll property etc...but no dice.
我想没有办法显示,比如说在窗户上垂直放置200个矩形表格使用平局。我需要其他解决方案...请提出任何想法。
I'm guessing there is no way to display , say 200 rectangles vertically on a windows form using draw. I need some other solution... any ideas please.
也许是一个图片框列表,然后用纯色填充每个图片框?
Maybe a list of pictureboxes and then populate each picturebox with the solid color ?
谢谢
推荐答案
您是否在绘制事件期间在窗体上绘制GDI +矩形?表单不知道您正在剪贴空间之外创建对象,因此不知道您需要滚动。
You are drawing GDI+ rectangles on a form during the paint event? The form would have no idea that you are creating objects outside of the clipping space and would therefore have no idea that you need to scroll.
您需要添加滚动条到窗体,然后计算滚动条的值位置,并使用该值确定在绘制事件上绘制矩形的哪一部分。这将涉及一些手动工作。您可以将它们全部绘制到适当大小的内存位图中,然后在绘制时将其部分复制到表单中。
You would need to add a scrollbar to the form and then calculate the value\position of the scrollbar and use that to determine what portion of your rectangles to draw upon the paint event. This would involve a bit of manual effort. You could draw them all to an in-memory bitmap of the appropriate size and then just copy the portions of that to the form upon draw.
或者:
如果您希望表单为您完成此操作,请创建一个自定义矩形控件并放置表格上的200个。由于它们是组件并且具有混凝土高度&宽度,表单将知道它需要滚动,并在设置了自动滚动的情况下进行相应的滚动。
If you wanted the form to do this for you, create a custom rectangle control and place 200 of those on the form. Since they are components and have a concrete height & width, the form would then know it needed to scroll, and would do so accordingly provided that autoscroll was set.
它可以像这样简单:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
this.AutoScroll = true;
for (int i = 0; i < 100; i++)
this.Controls.Add(new Rectangle() { Top = i * 120, Left = 10 });
}
}
public class Rectangle : Control
{
public Rectangle()
{
this.Width = 100;
this.Height = 100;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawRectangle(new Pen(Color.Black, 5), 0, 0, 100, 100);
}
}
这篇关于Winform在其上绘制对象时不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!