Invalidate和UpdateWindow的區別
Invalidate在消息隊列中加入一條WM_PAINT消息,其無效區為整個客戶區。
UpdateWindow直接發送一個WM_PAINT消息,其無效區范圍就是消息隊列中WM_PAINT消息(最多只有一條)的無效區。
效果很明顯,當調用Invalidate之后,屏幕不一定馬上更新,因為WM_PAINT消息不一定在隊列頭部,而調用UpdateWindow會使WM_PAINT消息馬上執行的,繞過了消息隊列。
如果調用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的