【发布时间】:2011-11-26 09:24:40
【问题描述】:
我编写了一个事件处理程序方法并将其附加到Form 的Paint 事件(只是主窗口)。此事件发送一个PaintEventArgs,其中包含一个名为Graphics 的属性,它是System.Drawing.Graphics 的一个实例。
这是我正在使用的代码:
private void Form1_Paint(object sender, PaintEventArgs e) {
Bitmap bm = new Bitmap("fruit-dealer-full.jpg");
Graphics g1 = this.CreateGraphics();
Graphics g2 = e.Graphics;
// g1.DrawImage(bm, 0, 0, this.Width, this.Height);
// g1.DrawRectangle(
// Pens.White, 10.0f, 10.0f, this.Width - 200, this.Height - 200);
g2.DrawImage(bm, 0, 0, this.Width, this.Height);
g2.DrawRectangle(
Pens.White, 10.0f, 10.0f, this.Width - 200, this.Height - 200);
}
最终我只是想更好地了解这里发生的事情,但具体来说,我有以下三个问题:
- 为什么
g1在整个窗口中重绘图像,而g2只绘制新的部分,即使我在绘制之前调用g2.Clear()? - 为什么对于
Graphics对象,图像仅在窗口增大时才重绘,而不是在窗口变小时时重绘? - 如果
PaintEventArgs.Graphics可以(或不应该)用于绘图,它的用途是什么?我想如果不需要重新绘制表单,它只会阻止您创建一个新的Graphics实例;我还缺少什么吗?
【问题讨论】:
-
刚注意到ResizeRedraw控件样式标志,大概会用在这种情况下,不过还是想明白上面的意思!