While Scrolling, the WM_LBUTTONUP message should not be retrieved from the queue by any message retrieval loop other than the scroll bar's internal one.
An application may come across this problem as follows:
- An application implements a message retrieval loop to implement background processing, for example, background processing while performing a time consuming paint.
- An application implements a message retrieval loop to implement communication with another application or DLL. For example, in order to scroll, the application needs to receive data from elsewhere.
Possible workarounds
Two possible workarounds are listed below. The first workaround is used by many exiting applications and by Windows; however, under rare circumstances the first workaround may not be a feasible one. In this case, the second workaround may be used. However, if possible, please try to avoid implementing message retrieval completely while scrolling.
- Use timer-message-based processing. Break down the complicated processing into smaller tasks and keep track of where each task starts and ends, then perform each task based on a timer message. When all components of the processing are complete, kill the timer. See below for an example of this workaround.
- Implement a message retrieval loop, but make sure WM_LBUTTONUP is not retrieved by it. This can be accomplished by using filters. See below for some examples of this workaround.
Example demonstrating Workaround 1
An application has a complex paint procedure. Calling ScrollWindow(), to scroll, generates paint messages. Background processing takes place while painting.
- When you receive the WM_PAINT message do the following:
- Call BeginPaint().
- Copy the invalidated rect to a global rect variable (for example, grcPaint) to be used in step 2. The global rect grcPaint would be a union of the previously obtained rect (grcPaint) and the new invalidated rect (ps.rcPaint). The code for this will resemble the following:
RECT grcPaint; // Should be initialized before getting the
// first paint message.
:
:
UnionRect(&grcPaint, &ps.rcPaint,&grcPaint);
- Call ValidateRect() with ps.rcPaint.
- Call EndPaint().
- Set a Timer.
This way, no more WM_PAINT messages are generated, because there are no invalid regions, and a timer is set up, which will generate WM_TIMER messages.
- Upon receiving a WM_TIMER message, check the global rect variable; if it is not empty, take a section and paint it. Then adjust the global rect variable so it no longer includes the painted region.
- Once the global rect variable is empty then kill the timer.
Example demonstrating Workaround 2
An application needs to obtain some data through DDE or some other mechanism from another application, which is then displayed in the window. In order to scroll, the application needs to request and then obtain the data from a server application.
There are three different filters that can be used to set up a PeekMessage() and get the information. The filters can be set up by using the uFilterFirst and uFilterLast parameters of PeekMessage(). uFilterFirst specifies the fist message in the range to be checked and uFilterLast specifies the last message in the range to be checked.
- Check and retrieve only the related message(s) for obtaining the needed data.
- Check for WM_LBUTTONUP without removing it form the queue; if it is in the queue, break. Otherwise, retrieve and dispatch all messages.
- Retrieve all messages less than WM_LBUTTONUP and greater than WM_LBUTTONUP, but do not retrieve WM_LBUTTONUP.