C# get form to invalidate when form is resized?
我刚刚接受了一个挑战,创建一个64个相同矩形或正方形的颜色交替的棋盘,从技术上来说,我已经完成了这个挑战;但是出于好奇,我注意到当用户调整窗体的大小并拖动它时,窗体会一直重绘自己,这几乎就像是一个断断续续的图像。我在想我写的代码,也许可以加入一些代码,让它只在用户完成尺寸调整后才会失效?
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 namespace CodeProjectDemo 9 { 10 public class checkerboard : Form 11 { 12 public checkerboard() 13 { 14 Size = new Size(400, 400); 15 Text = "CheckerBoard"; 16 BackColor = Color.Red; 17 } 18 19 protected override void OnPaint(PaintEventArgs e) 20 { 21 Graphics g = e.Graphics; 22 23 int h = DisplayRectangle.Height; 24 int w = DisplayRectangle.Width; 25 26 for (int i = 0; i < 8; i = i + 2) 27 28 for (int j = 0; j < 8; j = j + 2) 29 { 30 Color Black = Color.Black; 31 Brush brush = new SolidBrush(Black); 32 g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8); 33 } 34 35 36 for (int i = 1; i < 8; i = i + 2) 37 for (int j = 0; j < 8; j = j + 2) 38 { 39 Color White = Color.White; 40 Brush brush = new SolidBrush(White); 41 g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8); 42 } 43 for (int i = 1; i < 8; i = i + 2) 44 45 for (int j = 1; j < 8; j = j + 2) 46 { 47 Color Black = Color.Black; 48 Brush brush = new SolidBrush(Black); 49 g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8); 50 } 51 for (int i = 0; i < 8; i = i + 2) 52 for (int j = 1; j < 8; j = j + 2) 53 { 54 Color White = Color.White; 55 Brush brush = new SolidBrush(White); 56 g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8); 57 } 58 base.OnPaint(e); 59 60 } 61 62 static void Main() 63 { 64 Application.Run(new checkerboard()); 65 } 66 } 67 }
注:拖动的时候会有叠影
Jibesh:
based on his drawing code he must call the invalidate. situation is when he resizes the forms it takes a new DisplayRectangle and starts drawing the graphics for the new size.
Jibesh:
根据他的绘图代码,他必须调用invalidate。情况是,当他调整Form尺寸大小时,窗体采用新的DisplayRectangle并开始为新的尺寸绘制图形。
---------------------------------------------------------------------------------------------------------------------------
Solution 3
You can call Invalidate method inside the Form Resize handler. In addition to this move your code from OnPaint to OnPainBackground to avoid flickering. Check the code modifications.
可以在Form Resize事件处理程序中调用Invalidate函数,除此之外,将你的代码从OnPaint移至OnPaintBackground中以避免闪烁。下面是修改的代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 8 namespace CodeProjectDemo 9 { 10 public class checkerboard : Form 11 { 12 static void Main() 13 { 14 Application.Run(new checkerboard()); 15 } 16 17 public checkerboard() 18 { 19 Size = new Size(400, 400); 20 Text = "CheckerBoard"; 21 BackColor = Color.Red; 22 DoubleBuffered = true; 23 Resize += new System.EventHandler(this.Form1_Resize); 24 } 25 26 protected override void OnPaintBackground(PaintEventArgs e) 27 { 28 Graphics g = e.Graphics; 29 30 int h = DisplayRectangle.Height; 31 int w = DisplayRectangle.Width; 32 33 for (int i = 0; i < 8; i = i + 2) 34 for (int j = 0; j < 8; j = j + 2) 35 { 36 Color Black = Color.Black; 37 Brush brush = new SolidBrush(Black); 38 g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8); 39 brush.Dispose(); // added this line to clear object else this will produce resource leak 40 } 41 42 43 for (int i = 1; i < 8; i = i + 2) 44 for (int j = 0; j < 8; j = j + 2) 45 { 46 Color White = Color.White; 47 Brush brush = new SolidBrush(White); 48 g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8); 49 brush.Dispose(); // added this line to clear object else this will produce resource leak 50 } 51 for (int i = 1; i < 8; i = i + 2) 52 53 for (int j = 1; j < 8; j = j + 2) 54 { 55 Color Black = Color.Black; 56 Brush brush = new SolidBrush(Black); 57 g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8); 58 brush.Dispose(); // added this line to clear object else this will produce resource leak 59 } 60 for (int i = 0; i < 8; i = i + 2) 61 for (int j = 1; j < 8; j = j + 2) 62 { 63 Color White = Color.White; 64 Brush brush = new SolidBrush(White); 65 g.FillRectangle(brush, i * w / 8, j * h / 8, w / 8, h / 8); 66 brush.Dispose(); 67 } 68 } 69 private void Form1_Resize(object sender, EventArgs e) 70 { 71 this.Invalidate(); 72 } 73 74 } 75 }
引用:https://www.codeproject.com/Questions/525585/answer