views:

262

answers:

2

Hi all,

i have small doubt regarding the window functions in c++. what exactly "invalidate()" function do? what message does it sends?when we need to call this? also what is "update()" function? is "invalidaterect()" works similar to "invalidate()" function?.

Thanks

+1  A: 

The invalidate function marks the drawing area as invalidated. This flag is used when the window is redrawn, if the area is invalidated it will redraw the area, if not it will just leave the screen area as it is. invalidate invalidates the whole client area of a given object, invalidaterect invalidates a specific region of the client area. The update function does the actual redraw.

The reason for such a mechanism, and not just drawing immediately when you know something has to be changed is that you can prevent multiple redraws, say for example you received three keydown messages in one go, which scroll some text area. If you redrew the text area for each of the key presses you would have to draw the text area three times and the application would become unresponsive. By instead processing all the key events, updating the data structure attached to the text area and invalidating the text area before you redraw the textarea, you only redraw the textarea once.

wich
+2  A: 

CWnd::Invalidate() invalidates the entire client area of a window, which indicates that the area is out of date, and should be repainted. You would typically call this on a control that needs to be redrawn. CWnd::InvalidateRect() invalidates only part of the window.

With the Invalidate functions, the WM_PAINT message will posted [not strictly true; see the comments] to the message queue and handled at some point in the future. CWnd::UpdateWindow() sends (as opposed to posts) a WM_PAINT message, causing the invalidated regions to be redrawn immediately.

Really, this is all in the docs.

Thomas
you mean, we should not call "Invalidate" or "CWnd::UpdateWindow()"at one instance in application right?we need to call either one of it right to get wm_paint message
Shadow
Indeed. Calling both is not necessary. You would typically call `UpdateWindow` only if the same thread would do some lengthy computation afterwards, so it wouldn't get back to its message loop soon enough. In that case you want to repaint immediately, not wait until a more convenient time. But in general, use `Invalidate`.
Thomas
oh ok Thank u..
Shadow
@Thomas: Actually, `WM_PAINT` isn't posted. An internal flag is just set in the window manager and that flag is checked by `::Peek/GetMessage` in the message pump. If the flag is set and no higher priority messages are in the queue, `WM_PAINT` is returned in the `MSG` structure.
Johann Gerell
In practice, you probably won't be able to tell the difference. But thanks for the clarification!
Thomas