Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Paint messages will come in as fast as you let them

  blogs.msdn.com        2011-12-21 09:37:14       2,286        0    

There is a class of messages which are generated on demand rather than explicitly posted into a message queue. If you call Get­Message or Peek­Message and the queue is empty, then the window manager will look to see if one of these generated-on-demand messages is due, messages like WM_TIMER, WM_MOUSE­MOVE, and WM_PAINT.

Neil wonders, "In that program that called Invalidate­Rect 100,000 times, how many paint messages were generated?"

The Zen answer to this question is "Yes."

A more practical answer is "As many as you can get."

When somebody calls Invalidate­Rect, the window manager adds the specified rectangle to the window's invalid region (or invalidates the entire client area if no rectangle is provided) and sets a flag that says "Yo, there's painting to be done!" (It's not actually a flag, but you can think of it that way.)

When a message retrieval function finds that there are no incoming sent messages to be dispatched nor any applicable messages in the queue to be retrieved, it looks at these extra flags to see if it should generate a message on the fly. If the "Yo, there's painting to be done!" flag is set on a window that the thread is responsible for, a WM_PAINT message is generated for that window. (Similarly, a WM_TIMER is generated if a timer has elapsed, and a WM_MOUSE­MOVE is generated if the mouse has moved since the last time this thread retrieved a mouse message.)

Therefore, the number of WM_PAINT messages by 100,000 invalidations is not deterministic, but it'll be at least one and may be as high as 100,000. It's basically just a race between the invalidation thread and the paint thread.

Invalidate­Rect
Invalidate­Rect

Get­Message (retrieves WM_PAINT)

WM_PAINT dispatched

Get­Message (waits for a message)
Invalidate­Rect

Get­Message (returns with WM_PAINT)
Invalidate­Rect

WM_PAINT dispatched
Invalidate­Rect

Get­Message (retrieves WM_PAINT)
Invalidate­Rect
Invalidate­Rect

WM_PAINT dispatched

Get­Message (retrieves WM_PAINT)
Invalidate­Rect

WM_PAINT dispatched

Get­Message (retrieves WM_PAINT)

WM_PAINT dispatched

Get­Message (waits for a message)

If the thread doing the painting manages to call Get­Message between each call to Invalidate­Rect, then it will see every invalidation. On the other hand (which is more likely), it only manages to call Get­Message after a few invalidations have taken place, it will see the accumulated invalidation in a single WM_PAINT message.

Now that you understand how generated messages work, you can answer this question which sometimes comes in:

If the user is continuously moving the mouse, how many WM_MOUSE­MOVE messages will I get?

Source : http://blogs.msdn.com/b/oldnewthing/archive/2011/12/19/10249000.aspx

WORK  WM_PAINT  PAINT MESSAGE  MOUSE MOVE 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.