CPaint、CClientDC、CWindowDC以及CDC的区别

来源:互联网 发布:苹果cms精仿模板 编辑:程序博客网 时间:2024/05/17 06:12

1、BeginPaint()和EndPaint(),GetDC()和EndDC()区别

      BeginPaint()和EndPaint(),BeginPaint()函数原型如下:HDC BeginPaint( __in   HWND hwnd, __out  LPPAINTSTRUCT lpPaint);它为指定,窗口准备绘制,并填充lpPaint结构,该结构存储了绘图的相关信息。(The BeginPaint function prepares the specified window for painting and fills a PAINTSTRUCT structure with information about the painting.)

      The BeginPaint function automatically sets the clipping region of the device context to exclude any area outside the update region. The update region is set by the InvalidateRect or InvalidateRgn function and by the system after sizing, moving, creating, scrolling, or any other operation that affects the client area. If the update region is marked for erasing, BeginPaint sends a WM_ERASEBKGND message to the window.

      牵涉到无效区域的概念,由InvalidateRect和InvalidateRgn可以设定无效区域,BeginPaint获得的是无效区域的设备句柄,用来重绘无效区域的,并使得该区域由无效区域转为有效区域,在使用EndPaint()函数后,消息队列中WM_PAINT消息被删除。

      The BeginPaint function automatically sets the clipping region of the device context to exclude any area outside the update region. BeginPaint returns a handle to the display device context used for drawing in the client area; EndPaint ends the paint request and releases the device context.

 

     Unlike the device context handle returned from BeginPaint, the device context handle returned from GetDC has a clipping rectangle equal to the entire client area. You can paint on any part of the client area, not merely on the invalid rectangle (if indeed there is an invalid rectangle). Unlike BeginPaint, GetDC does not validate any invalid regions. If you need to validate the entire client area, you can call

ValidateRect (hwnd, NULL) ;

       由于GetDC不能使无效区域变成有效区域,而在存在无效区域的时候,消息队列会不断出现WM_PAINT消息,导致不断重绘,应该使用ValidateRect函数是该区域变成有效区域,而BeginPaint函数封装了该函数。

      GetDC()和ReleaseDC(),这是一对成对出现的函数。GetDC()原型如下:HDC GetDC(__in  HWND hWnd)该函数返回的设备句柄是特定窗口的客户区或者整个屏幕区域

(The GetDC function retrieves a handle to a device context (DC) for the client area of a specified window or for the entire screen)在利用GetDC获得的设备句柄绘制完毕后,应该调用ReleaseDC()函数释放该句柄。

    

  在使用方面,一般应该在处理WM_PAINT消息时使用BeginPaint,在处理鼠标或键盘消息时使用GetDC。
  GetWindowDC()函数是获取整个窗口的设备句柄(包括标题栏,菜单栏)等。
  无效区域的存在使得消息队列中存在WM_PAINT消息,而ValidateRect使区域变为有效区域又能够使消息队列中WM_PAINT消息被删除。
2、CClientDC、CPaintDC、CWindowDC、CDC
   CDC是基类,用来定义一个设备上下文对象。它下面有四个派生类分别是:CClientDC、CPaintDC、CWindowDC、CMetaFileDC四个派生类。
   

The CDC object provides member functions for working with a device context, such as a display or printer, as well as members for working with a display context associated with the client area of a window.

Do all drawing through the member functions of a CDC object. The class provides member functions for device-context operations, working with drawing tools, type-safe graphics device interface (GDI) object selection, and working with colors and palettes. It also provides member functions for getting and setting drawing attributes, mapping, working with the viewport, working with the window extent, converting coordinates, working with regions, clipping, drawing lines, and drawing simple shapes, ellipses, and polygons. Member functions are also provided for drawing text, working with fonts, using printer escapes, scrolling, and playing metafiles.

CDC contains two device contexts, m_hDC and m_hAttribDC, which, on creation of a CDC object, refer to the same device. CDC directs all output GDI calls to m_hDC and most attribute GDI calls to m_hAttribDC. (An example of an attribute call is GetTextColor, while SetTextColor is an output call.)

指出了CDC能够做的事情。当然如果有其他用途,也可以使用它派生类对象。

CPaintDC:A CPaintDC object can only be used when responding to a WM_PAINT message, usually in your OnPaint message-handler member function.

主要在类的构造函数中封装了BeginPaint,而在析构函数中封装了EndPaint,原则上与API函数的使用原理相似,它不过是MFC封装的一个易用类而已。

CClientDC:This means that the device context associated with a CClientDC object is the client area of a window

在类的构造函数中封装了GetDC,而在类的析构函数中封装了ReleaseDC,对应窗口的客户区绘制操作。MFC的易用类封装

CWindowDC:Calls the Windows functions GetWindowDC at construction time and ReleaseDC at destruction time. This means that a CWindowDC object accesses the entire screen area of a CWnd (both client and nonclient areas).

3、UpdateWindow()、Invalidate()、InvalidateRect()、ValidateRect()、RedrawWindow()

 

 

原创粉丝点击