Paint on panel allowing auto scroll

风流意气都作罢 提交于 2019-12-01 06:54: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)));
  }
}

回答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:

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


来源:https://stackoverflow.com/questions/2406720/paint-on-panel-allowing-auto-scroll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!