在面板上绘画允许自动滚动
我正在实现一个想要在面板中画线的应用程序。但面板必须自动滚动,因为它的大小可以在运行时扩展。我使用的面板绘制方法如下。当我运行程序时,它会绘制线条,但是当我向下滚动面板时,线条会崩溃。我该如何避免这种情况?
private void panel1_Paint(object sender, PaintEventArgs e)
{
this.DoubleBuffered = true;
Pen P = new Pen(Color.Red);
for (int i = 0; i < 10; i++) {
e.Graphics.DrawLine(P, (new Point(i * 40, 0)), (new Point(i * 40, 60 * 40)));
}
for (int i = 0; i < 60; i++)
{
e.Graphics.DrawLine(P, (new Point(0, i *40)), (new Point(10 * 40, i * 40)));
}
}
I'm implementing an application which want to draw lines in the panel. But the panel must be auto scrolled as it size can be expand at run time. The panel paint method I have used is as below.When I run the program it draws lines, but when I scroll down the panel the lines get crashes.How can I avoid that?
private void panel1_Paint(object sender, PaintEventArgs e)
{
this.DoubleBuffered = true;
Pen P = new Pen(Color.Red);
for (int i = 0; i < 10; i++) {
e.Graphics.DrawLine(P, (new Point(i * 40, 0)), (new Point(i * 40, 60 * 40)));
}
for (int i = 0; i < 60; i++)
{
e.Graphics.DrawLine(P, (new Point(0, i *40)), (new Point(10 * 40, i * 40)));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设“崩溃”实际上并不意味着您的代码崩溃。您需要将绘图偏移滚动量。这很容易做到:
I'll assume that "get crashes" doesn't actually mean that your code crashes. You'll need to offset the drawing by the scroll amount. That's easy to do: