The rcPaint field of the PAINTSTRUCT structure is a structure of type RECT. As you learned in Chapter 3, the RECT structure defines a rectangle with four fields named left, top, right, and bottom. The rcPaint field in the PAINTSTRUCT structure defines the boundaries of the invalid rectangle, as shown in Figure 4-1. The values are in units of pixels relative to the upper left corner of the client area. The invalid rectangle is the area that you should repaint.
【PAINTSTRUCT结构的rcPaint字段也是一个结构,它的结构类型是RECT。就像你在第三章所学的,RECT结构通过四个字段(left, top, right, bottom)定义一个矩形。如图4-1所示,PAINTSTRUCT结构的rcPaint字段定义了无效矩形的边界。这些值均以像素为单位,并且相对于客户区的左上角。无效矩形是需要重画的区域。】
The rcPaint rectangle in PAINTSTRUCT is not only the invalid rectangle; it is also a "clipping" rectangle. This means that Windows restricts painting to within the clipping rectangle. More precisely, if the invalid region is not rectangular, Windows restricts painting to within that region.
To paint outside the update rectangle while processing WM_PAINT messages, you can make this call:
【PAINTSTRUCT中的rcPaint矩形不仅是一个“无效矩形”,它还是一个“剪辑”矩形。这意味着Windows将绘图操作限制在这个剪辑区域中。更确切地说,如果无效矩形不是矩形,则Windows将绘图操作限制在这个区域中。】
【为了在处理WM_PAINT消息时在更新区域外绘图,可以这样调用:】
InvalidateRect (hwnd, NULL, TRUE) ;
before calling BeginPaint. This invalidates the entire client area and causes BeginPaint to erase the background. A FALSE value in the last argument will not erase the background. Whatever was there will stay.
It is usually most convenient for a Windows program to simply repaint the entire client area whenever it receives a WM_PAINT message, regardless of the rcPaint structure. For example, if part of the display output in the client area includes a circle but only part of the circle falls within the invalid rectangle, it makes little sense to draw only the invalid part of the circle. Draw the whole circle. When you use the device context handle returned from BeginPaint, Windows will not paint outside the rcPaint rectangle anyway.
In the HELLOWIN program in Chapter 2, we didn't care about invalid rectangles when processing the WM_PAINT message. If the area where the text was displayed happened to be within the invalid rectangle, DrawText restored it. If not, then at some point during processing of the DrawText call Windows determined it didn't need to write anything on the display. But this determination takes time. A programmer concerned about performance and speed (and that includes all of us, I hope) will want to use the invalid rectangle during processing of the WM_PAINT message to avoid unnecessary GDI calls. This is particularly important if painting requires accessing disk files such as bitmaps.
【在调用BeginPaint之前,它将使整个客户区变为有效,并且使BeginPaint擦除背景。但如果最后一个参数是FALSE。则不会擦除背景,原有的东西将会保留在原处。】
【通常最方便的做法是,当一个Windows应用程序在接收到WM_PAINT消息时,简单地重画整个客户区而不考虑rcPaint结构。例如,如果在显示输出的客户区中包含一个圆,但只有只有部分圆落在无效矩形中,那么只画这个圆在无效矩形中的部分没有意义,而应该画整个圆。当你使用从BeginPaint函数返回的设备关联句柄来绘图时,Windows总不会在rePaint之外的区域操作。】
【在第二章中的HELLOWIN程序中,当我们处理WM_PAINT消息时不关心无效矩形。我们是这样处理的:如果显示文字的区域恰好在无效矩形的范围中,DrawText将文字恢复。否则,若文字区域不在无效矩形的范围中时,在DrawText调用的时候,Windows决定它无需向显示器上输出。但是,决定需要时间。关心程序性能和速度的程序员(我希望,我们也是这样)会在处理WM_PAINT消息的过程中利用无效矩形来避免额外的GDI调用。这在绘图时需要访问诸如位图的磁盘文件时显得尤为重要。】