CPaintDC 、CWindowDC、 CClientDC、 CDC的區別與聯繫

CPaintDC 、CWindowDC、 CClientDC、 CDC

關係圖:

一句話概括:

CPaintDC            無效區dc,      相當於BeginPaint,   EndPaint 
CClientDC          客戶區dc,      相當於GetDC,   ReleaseDC 
CWindowDC      整窗口dc,      相當於GetWindowDC,   ReleaseDC 
CDC                     任何dc,          相當於CreateDC,   DeleteDC

 一、CPaintDC

 Mfc自動生成的OnPaint函數都會定義一個CPaintDC類型的變量。

如: 

  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消息響應的函數中。

發佈了3 篇原創文章 · 獲贊 1 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章