我的程序很简单,就是启动一个Timer,然后DrawLine重复画一条连续的折线。因为效率的问题,我的程序不要求在窗口被覆盖时重绘,由此不要onPaint里重绘,但现在的问题是我的窗口在绘图时,当鼠标操作其他程序时,测试程序有时也会刷新,即使测试程序的窗口并没有被覆盖或者被影响。网上有这样说,去掉Onpaint,但是也没有用:
protected override void OnPaint(PaintEventArgs e)
{
// base.OnPaint(e);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// base.OnPaintBackground(e);
}请问怎么办?
protected override void OnPaint(PaintEventArgs e)
{
// base.OnPaint(e);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// base.OnPaintBackground(e);
}请问怎么办?
这个思路是对的,创建一个 Image 对象
Image img = new Bitmap(500, 500);
this.BackgroundImage = img;
using (Graphics g = Graphics.FromImage(img))
{
g.DrawLine(/*...*/);
g.Save();
}
g.DrawLine(new Pen(Color.Red), new Point(15, 15), new Point(15, 100));//画
t.Tick += delegate
{
if (Form.ActiveForm == this)
{
//update bitmap;
}
};
protected override void OnPaintBackground(PaintEventArgs e)
{
// 注释 base.OnPaintBackground(e);
e.Graphics.DrawRectangle(Pens.Black, 0, 0, 100, 100);
}