Invalidate和UpdateWindow的區別

來源:互聯網
上載者:User
  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的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.