PAINTSTRUCT 结构的作用

本文介绍了PAINTSTRUCT结构,它是Windows绘图中的关键组件之一。文章详细解释了该结构的各个字段及其用途,并讨论了如何在绘图过程中使用此结构。特别是关于如何使用BeginPaint函数初始化PAINTSTRUCT结构,以及如何通过WM_ERASEBKGND消息处理背景擦除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The PAINTSTRUCT structure contains information for an application. This information can be used to paint the client area of a window owned by that application.

 

【PAINTSTRUCT结构包含应用程序的某些信息,这些信息可被应用程序所包含的窗口用来绘制客户区。】

 

typedef struct tagPAINTSTRUCT { 

  HDC  hdc; 

  BOOL fErase; 

  RECT rcPaint; 

  BOOL fRestore; 

  BOOL fIncUpdate; 

  BYTE rgbReserved[32]; 

} PAINTSTRUCT, *PPAINTSTRUCT;

 

Windows fills in the fields of this structure when your program calls BeginPaint. Your program can use only the first three fields. The others are used internally by Windows. The hdc field is the handle to the device context. In a redundancy typical of Windows, the value returned from BeginPaint is also this device context handle. In most cases, fErase will be flagged FALSE (0), meaning that Windows has already erased the background of the invalid rectangle. This happens earlier in the BeginPaint function. (If you want to do some customized background erasing in your window procedure, you can process the WM_ERASEBKGND message.) Windows erases the background using the brush specified in the hbrBackground field of the WNDCLASS structure that you use when registering the window class during WinMain initialization. Many Windows programs specify a white brush for the window background. This is indicated when the program sets up the fields of the window class structure with a statement like this: 

 

【当程序调用BeginPaint时,Windows会为PAINTSTRUCT结构字段进行初始化值。但程序只能使用前3个字段,其他字段由Windows内部使用。hdc字段是设备上下文句柄。在旧版本的Windows中,BeginPaint的返回值同样是这个设备关联句柄。大多数情况下,fErase字段会标志为FALSE(0),意味着Windows已经擦除了无效矩形的背景。这最早在BeginPaint函数中发生。(如果你想在窗口过程中自定义背景擦除动作,可以处理WM_ERASEBKGND消息。)Windows用一个画笔来擦除背景。这个画笔是在WinMain初始化期间,注册窗口类时在WNDCLASS结构的hbrBackground字段指定的。许多程序为窗口背景指定一个白色的笔刷,由以下语句为程序设置窗口类的hbrBackground字段。】

 

wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

 

However, if your program invalidates a rectangle of the client area by calling InvalidateRect, the last argument of the function specifies whether you want the background erased. If this argument is FALSE (that is, 0), Windows will not erase the background and the fErase field of the PAINTSTRUCT structure will be TRUE (nonzero) after you call BeginPaint. 

 

【不过,如果程序调用InvalidateRect函数在客户区中定义一个无效矩形时,函数的最后一个参数可指定是否擦除背景。如果这个参数是FALSE(0),调用BeginPaint函数之后,Windows将不会擦除背景并且PAINTSTRUCT结构的fErase字段会设置为TRUE(nonZero)。】

 

【译注:Invalidate函数可为窗口指定一个需要更新的区域,即无效矩形。Windows发现有客户区有无效区域的时,它会在应用程序的消息队列中发送一个WM_PAINT消息,窗口过程可以利用这个消息来更新客户区。】

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值