天天看點

Invalidate和UpdateWindow的差別

  1. Invalidate 在消息隊列中加入一條 WM_PAINT 消息,其無效區為整個客戶區。
  2. UpdateWindow 直接發送一個 WM_PAINT 消息,其無效區範圍就是消息隊列中 WM_PAINT 消息(最多隻有一條)的無效區。
  3. 效果很明顯,當調用 Invalidate 之後,螢幕不一定馬上更新,因為 WM_PAINT 消息不一定在隊列頭部,而調用 UpdateWindow 會使 WM_PAINT 消息馬上執行的,繞過了消息隊列。
  4. 如果調用 Invalidate 之後想馬上更新螢幕,那就加上 UpdateWindow() 這條語句。 

MSDN的解釋

UpdateWindow

The UpdateWindow function updates the client area of the specified window by sending a WM_PAINT 

message to the window if the window's update region is not empty. The function sends a WM_PAINT

 message directly to the window procedure of the specified window, bypassing the application queue. 

If the update region is empty, no message is sent. 

InvalidateRect

The system sends a WM_PAINT message to a window whenever its update region is not empty and

 there are no other messages in the application queue for that window. 

翻譯成中文大概的解釋如下:

  UpdateWindow:如果有無效區,則馬上sending a WM_PAINT message到視窗處理過程,不進消息隊列進行排隊等待,立即重新整理視窗,否則,什麼都不做。

 InvalidateRect:設定無效區,如果為NULL參數,則設定整個視窗為無效區。當應用程式的那個視窗的消息隊列為空時,則sending a WM_PAINT message(即使更新區域為空).在sending a WM_PAINT message的所有InvalidateRect的更新區域會累加。

 1:設定無效區

 InvalidateRect

 2:立即重新整理

 UpdateWindow();

如果不調用 InvalidateRect就調用 UpdateWindow,那麼UpdateWindow什麼都不做。 ??????

如果調用 InvalidateRect 後不調用UpdateWindow,則系統會自動在視窗消息隊列為空的時候,系統自動發送一WM_PAINT消息。

調用UpdateWindow()時将會發送一個WM_PAINT消息,而應用程式在接收到WM_PAINT消息後,将自動地調用Invalidate(),是以,在程式代碼中,不一定要出現Invalidate()!

UpdateWindow()就是立即發送WM_PAINT消息,隻對聲明無效的區域起作用,  

  Invalidate()則是聲明無效的方式之一。

Invalidate()表示客戶區域無效,在下次WM_PAINT發生時重繪。而WM_PAINT是由系統進行維護的,每當CWnd的更新區域不為空,并且在應用程式的視窗消息隊列中沒有其它消息時,Windows就發送一條WM_PAINT消息。   

  Invalidate裡面有個bool型的參數,用來辨別重繪的時候是否用背景色填充。是不是用SetBkcolor函數?下去繼續研究。

 updateWindow則是要求系統對區域進行立即重繪。

 看到有人在網上提出問題,他在Invalidate後面又寫了繪圖的函數但是沒有執行,因為invalidate執行過以後轉到PAINT指令了。是以後面的都沒有顯示。

 也終于想通我繪的圖一直在閃啊閃,因為我在PAINT裡面用到Invalidate()函數,是以他不停的自嵌套,倒是繪的圖不停的閃。

Invalidate讓客戶區處于可以重畫的狀态,而UpdateWindow開始重畫,但是它先判斷客戶區是否為空,不空UpdateWindow不執行,為空才執行重畫。

Invalidat最後也是調用InvalidatRect,在windows API裡隻有InvalidatRect的

繼續閱讀