Panel paint to automatically scroll

I am implementing an application that wants to draw lines in a panel. But the panel should scroll automatically, as the size may expand at runtime. The panel paint method that I used is given below. When I run the program, it draws lines, but when I scroll down the panel, the lines get glitches. How can i avoid this?

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)));
  }
}
+3
source share
1 answer

I assume that “getting failed” does not really mean that your code will work. You will need to compensate for the drawing by the amount of scroll. This is easy to do:

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

Source: https://habr.com/ru/post/1736056/


All Articles