在面板上绘画允许自动滚动

发布于 2024-08-24 08:15:03 字数 506 浏览 6 评论 0原文

我正在实现一个想要在面板中画线的应用程序。但面板必须自动滚动,因为它的大小可以在运行时扩展。我使用的面板绘制方法如下。当我运行程序时,它会绘制线条,但是当我向下滚动面板时,线条会崩溃。我该如何避免这种情况?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

站稳脚跟 2024-08-31 08:15:03

我假设“崩溃”实际上并不意味着您的代码崩溃。您需要将绘图偏移滚动量。这很容易做到:

private void panel1_Paint(object sender, PaintEventArgs e) {
  e.Graphics.TranslateTransform(panel1.AutoScrollPosition.X, panel1.AutoScrollPosition.Y);
  // etc
  //...
}

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:

private void panel1_Paint(object sender, PaintEventArgs e) {
  e.Graphics.TranslateTransform(panel1.AutoScrollPosition.X, panel1.AutoScrollPosition.Y);
  // etc
  //...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文