为什么PaintEventArgs.Graphics的行为与Control.CreateGraphics不同? [英] Why does PaintEventArgs.Graphics behave differently from Control.CreateGraphics?

查看:40
本文介绍了为什么PaintEventArgs.Graphics的行为与Control.CreateGraphics不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个事件处理程序方法,并将其附加到 Form (只是主窗口)的 Paint 事件.此事件发送一个 PaintEventArgs ,其中包含一个名为 Graphics 的属性,该属性是 System.Drawing.Graphics 的一个实例.

I have written an event handler method and attached it to the Paint event of a Form (just the main window). This event sends a PaintEventArgs containing a property called Graphics, which is an instance of System.Drawing.Graphics.

这是我正在使用的代码:

Here is the code I'm using:

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);

}

最终,我只是想对这里发生的事情有一个更好的了解,但是具体来说,我有以下三个问题:

Ultimately I just want to gain a better understanding of what's happening here, but specifically I have these three questions:

  1. 为什么 g1 在整个窗口中重新绘制图像,而 g2 仅绘制新部分,即使我调用 g2.Clear()绘制之前?
  2. 为什么对于带有 Graphics 对象的图像,仅当窗口尺寸增大时才重绘图像,而当窗口减小时才重绘?
  3. 如果 PaintEventArgs.Graphics 可以(或不应)用于绘图,它的用途是什么?我可以想象,如果不需要重新绘制表单,它只会阻止您创建新的 Graphics 实例.还有更多我想念的东西吗?
  1. Why does g1 redraw the image in the whole window, while g2 only draws the new portion, even if I call g2.Clear() before drawing?
  2. Why, with either Graphics object, is the image only redrawn when the window increases in size, and not when it is made smaller?
  3. If PaintEventArgs.Graphics can (or should) not be used for drawing, what is it used for? I would imagine it just prevents you from having to create a new Graphics instance if the form doesn't need to be redrawn; is there more to it that I'm missing?

推荐答案

.NET WinForms的孩子真的应该学习Win32 API.已经拿起Petzold的副本了!

You .NET WinForms kids really should learn the Win32 API. Pick up a copy of Petzold already!

即使我在绘制之前调用了g2.Clear(),为什么g1会在整个窗口中重新绘制图像,而g2只绘制新部分?

Why does g1 redraw the image in the whole window, while g2 only draws the new portion, even if I call g2.Clear() before drawing?

大概 g2 是从 PAINTSTRUCT :: rcPaint 为您-此变量描述了要绘制的区域.这是预期的行为-不必耗费大量CPU周期,而每当另一个窗口仅将其重叠一个像素时,便会重绘整个窗口,而是可以重绘...仅一个像素!

Presumably g2 is a wrapper around a device context received from BeginPaint. I imagine WinForms wraps PAINTSTRUCT::rcPaint for you - this variable describes the area to be painted. This is expected behaviour - rather than burning CPU cycles redrawing your entire window every single time another window overlaps it by just one pixel, you can redraw ... just the one pixel!

g2.Clear 可能受 rcPaint 限制.

g1 可能是窗口的 GetDC -它为您提供了要绘制的整个表面.

g1 is probably a GetDC for the window - which gives you the entire surface to draw on.

为什么对于任何一个Graphics对象,仅当窗口尺寸增加时才重绘图像,而当窗口变小时才重绘?

Why, with either Graphics object, is the image only redrawn when the window increases in size, and not when it is made smaller?

基础窗口类可能没有 CS_HREDRAW CS_VREDRAW 窗口样式.没有这些,就没有理由要求默认行为在窗口变小时要求您重新绘制窗口:Windows知道整个窗口的外观,可以将不需要的部分剪掉.这与当窗口变大时不一样,它不知道在新区域中绘制什么.

The underlying window class probably doesn't have CS_HREDRAW or CS_VREDRAW window styles. Without these, there's no reason for the default behaviour to request you redraw the window when it gets smaller: Windows knows what the entire window looks like, it can clip the unwanted bits away. This is unlike when the window gets bigger, it doesn't know what to draw in the new area.

如果PaintEventArgs.Graphics可以(或不应该)用于绘图,它的用途是什么?我可以想象,如果不需要重新绘制表单,它只会阻止您创建新的Graphics实例.还有更多我想念的东西吗?

If PaintEventArgs.Graphics can (or should) not be used for drawing, what is it used for? I would imagine it just prevents you from having to create a new Graphics instance if the form doesn't need to be redrawn; is there more to it that I'm missing?

用于绘图.除非您有一些复杂的绘图要求,否则可以使用 PaintEventArgs.Graphics 最小化窗口上的绘画量.(如上所述,这可以节省大量的CPU周期,并且可能是 BeginPaint EndPaint 的简单包装-这是

It's used for drawing. Unless you have some complex drawing requirements, you can use the PaintEventArgs.Graphics minimize the amount of painting that goes on to your window. (As above - this is a huge saver of CPU cycles and it's probably a simple wrapper over BeginPaint and EndPaint - which is how drawing to the client area is meant to be done.)

这篇关于为什么PaintEventArgs.Graphics的行为与Control.CreateGraphics不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆