對LockWindowUpdate與GetDCEx的理解

來源:互聯網
上載者:User

MSDN如是說:
The LockWindowUpdate function disables or enables drawing in the specified window. Only one window can be locked at a time. 

BOOL LockWindowUpdate(
  HWND hWndLock   // handle to window
);

Parameters
hWndLock 
[in] Specifies the window in which drawing will be disabled. If this parameter is NULL, drawing in the locked window is enabled. 
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero, indicating that an error occurred or another window was already locked. 

通俗的說,就是這個函數把參數---控制代碼--所關聯的視窗鎖住了,不再讓他繼續更新了。
GetDCEx
The GetDCEx function retrieves a handle to a display device context (DC) for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the DC. 

This function is an extension to the GetDC function, which gives an application more control over how and whether clipping occurs in the client area. 

HDC GetDCEx(
  HWND hWnd,      // handle to window
  HRGN hrgnClip,  // handle to clipping region
  DWORD flags     // creation options
);
此函數參數怪多的,這裡就不貼了,參考 MSDN吧

關鍵在於第三個參數:標誌位的設定
前面我們已經知道了,LockWindowUpdate可以鎖住某個視窗,如果我們鎖住的話,那將不能在這個視窗中進行一切繪製操作(用GetDC,BeginPaint,CreateIC,CreateDC這些API建立的DC都被禁止),比如TextOut也將失效,因為第一個參數是hdc,如果是前面括弧中的方式得到的DC,那就失去作用。
然而,GetDCEx卻十分例外,如果我們把第三個參數設定成DCX_LOCKWINDOWUPDATE,那即使你把視窗鎖住,用此API得到的DC,依然可以對此視窗進行繪製,十分特殊。
最後提醒一點:同一時刻,只能有一個視窗被鎖住。
利用反證法比較容易證明此觀點:
比如LockWindowUpdate(A);
LockWindowUpdate(B);
那我釋放的時候只能用LockWindowUpdate(NULL);//看函數參數介紹,NULL表示釋放
那請問此刻釋放的是哪個視窗?你能說清楚嗎?
因此肯定莫一時刻只能鎖住一個視窗。
最後給出一個測試代碼吧:

#include<iostream><br />#include<windows.h><br />using namespace std;<br />void main()<br />{<br /> TCHAR szAppName[]=TEXT("I love you!!!");<br /> HWND hwnd=FindWindow(NULL,TEXT("伊銳銳"));<br /> //HDC hdc=GetDC(hwnd);<br /> HDC hdc=GetDCEx(hwnd,NULL,DCX_LOCKWINDOWUPDATE);<br /> LockWindowUpdate(hwnd);<br /> SetBkMode(hdc,TRANSPARENT);<br /> TextOut(hdc,0,50,szAppName,lstrlen(szAppName));</p><p> ReleaseDC(hwnd,hdc);<br /> LockWindowUpdate(hwnd);<br />}<br />

代碼通俗易懂,這裡其實我用的就是往QQ聊天視窗中輸入I LOVE YOU罷了,測試起來效果還行。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.