我编写了以下简单的程序,它每 100 毫秒在屏幕上绘制线条(由 timer1 触发)。我注意到这幅画有点闪烁(也就是说,窗口并不总是完全是蓝色的,但有一些灰色的光透过)。所以我的想法是使用双缓冲。但是当我这样做时,事情变得更糟了。现在屏幕几乎总是灰色的,只是偶尔会出现蓝色(由 timer2 演示,DoubleBuffered
每 2000 毫秒切换一次属性)。
对此有何解释?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e) {
Graphics g = CreateGraphics();
Pen pen = new Pen(Color.Blue, 1.0f);
Random rnd = new Random();
for (int i = 0; i < Height; i++)
g.DrawLine(pen, 0, i, Width, i);
}
// every 100 ms
private void timer1_Tick(object sender, EventArgs e) {
Invalidate();
}
// every 2000 ms
private void timer2_Tick(object sender, EventArgs e) {
DoubleBuffered = !DoubleBuffered;
this.Text = DoubleBuffered ? "yes" : "no";
}
}
}