CPaintDC 、CWindowDC、 CClientDC、 CDC

来源:互联网 发布:数组增加元素的方法 编辑:程序博客网 时间:2024/05/10 22:47


关系图:

一句话概括:

CPaintDC            无效区dc,      相当于BeginPaint,   EndPaint 
CClientDC          客户区dc,      相当于GetDC,   ReleaseDC 
CWindowDC      整窗口dc,      相当于GetWindowDC,   ReleaseDC 
CDC                     任何dc,          相当于CreateDC,   DeleteDC

 一、CPaintDC

 Mfc自动生成的OnPaint函数都会定义一个CPaintDC类型的变量。

如: 

[cpp] view plaincopy
  1. void CXXXXXX::OnPaint()  
  2. {  
  3.     CPaintDC dc(this); // device context for painting  
  4.     // TODO: Add your message handler code here  
  5.     // Do not call CView::OnPaint() for painting messages  
  6. }  
  7. // If you add a minimize button to your dialog, you will need the code below  
  8. //  to draw the icon.  For MFC applications using the document/view model,  
  9. //  this is automatically done for you by the framework.  
  10. void CXXXXDlg::OnPaint()  
  11. {  
  12.     if (IsIconic())  
  13.     {  
  14.         CPaintDC dc(this); // device context for painting  
  15.   
  16.         SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);  
  17.   
  18.         // Center icon in client rectangle  
  19.         int cxIcon = GetSystemMetrics(SM_CXICON);  
  20.         int cyIcon = GetSystemMetrics(SM_CYICON);  
  21.         CRect rect;  
  22.         GetClientRect(&rect);  
  23.         int x = (rect.Width() - cxIcon + 1) / 2;  
  24.         int y = (rect.Height() - cyIcon + 1) / 2;  
  25.   
  26.         // Draw the icon  
  27.         dc.DrawIcon(x, y, m_hIcon);  
  28.     }  
  29.     else  
  30.     {  
  31.         CDialog::OnPaint();  
  32.     }  
  33. }  

MSDN上的解释:

The CPaintDC class is a device-context class derived from CDC. It performs a CWnd::BeginPaint at construction time and CWnd::EndPaint at destruction time.

(翻译:CPaintDC继承自CDC,它在构造函数中执行CWnd::BeginPaint,在析构函数中调用CWnd::EndPaint

 A CPaintDC object can only be used when responding to aWM_PAINT message, usually in yourOnPaintmessage-handler member function.

(翻译:CPaintDC对象通常当响应WM_PAINT消息时在OnPaint消息处理函数中被使用。)

题外话,这个英文按照中国人的逻辑来说应该这样表达:

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

 二、CClientDC

MSDN上的解释:

The CClientDC class is derived from CDC and takes care of calling the Windows functionsGetDC at construction time andReleaseDC at destruction time. This means that the device context associated with aCClientDC object is the client area of a window.

(翻译:CClientDC继承自CDC,它在构造函数中调用GetDC,在析构函数中调用ReleaseDC.这意味着与CClientDC对象联系的DC是窗口的客户区。)

 三、CWindowDC

The CWindowDC class is derived from CDC. It calls the Windows functionsGetWindowDC at construction time andReleaseDC at destruction time. This means that aCWindowDC object accesses the entire screen area of aCWnd (both client and nonclient areas).翻译:获取整个屏幕区域,包括客户区和非客户区。)

 

CSDN:

CPaintDC只能在OnPaint()中使用,CClientDC只和客户区有关,可以在任何地方使用。

 CPaintDC代表整个窗口,而CClientDC代表窗口的客户区(不包括标题栏、边框),要选择合适的DC进行绘制。

 CPaintDC一般是用在OnPaint()函数里的。CPaintDC对象一构造,消息队列中的WM_PAINT会被取走,而CClientDC是用在非OnPaint()函数里画图的。

用CClientDC时WM_PAINT消息没有从消息队列中清除,CPaintDC结束时会自动清除该消息。

 CPaintDC是一个特殊的设备环境封闭类,它主要处理windows的wm_paint消息。CClientDC可以自动调用GetDC和ReleaseDC函数。CwindowDC是从CDC类继承,用于得到桌面窗口设备环境指针。

CclinetDC用于窗口客户区,CwindowDC用于整个窗口,包括非客户区。

 

在OnPaint中用CPaintDC仅仅对需要刷新的地方进行重绘。

> 往一个对话框上画图,在OnPaint()里是不是只能用   CPaintDC? 
恰恰相反,CPaintDC是推荐只用在OnPaint中的。诚如   i_noname(晚九朝五)   所言:“它的范围是WM_PAINT消息所要重画的区域大小”,因为这个类的构造函数中调用了BeginPaint,析构函数中调用了EndPaint。这两个API是针对WM_PAINT消息使用的,所以不要把CPaintDC用在非对WM_PAINT消息响应的函数中。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

CDC是Windows绘图设备的基类。

CClientDC:
(1)(客户区设备上下文)用于客户区的输出,与特定窗口关联,可以让开发者访问目标窗口中客户区,其构造函数中包含了GetDC,析构函数中包含了ReleaseDC。

CPaintDC:
(1)用于响应窗口重绘消息(WM_PAINT)是的绘图输出。
(2)CPaintDC在构造函数中调用BeginPaint()取得设备上下文,在析构函数中调用EndPaint()释放设备上下文。EndPaint()除了释放设备上下文外,还负责从消息队列中清除WM_PAINT消息。因此,在处理窗口重画时,必须使用CPaintDC,否则WM_PAINT消息无法从消息队列中清除,将引起不断的窗口重画。
(3)CPaintDC也只能用在WM_PAINT消息处理之中。

CWindowDC:
(1)可在非客户区绘制图形,而CClientDC,CPaintDC只能在客户区绘制图形。
(2)坐标原点是在屏幕的左上角,CClientDC,CPaintDC下坐标原点是在客户区的左上角。
(3)关联一特定窗口,允许开发者在目标窗口的任何一部分进行绘图,包含边界与标题,这种DC同WM_NCPAINT消息一起发送。


0 0
原创粉丝点击