Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to remove duplicate WM_MOUSEMOVE?

Status
Not open for further replies.

q0987

Programmer
Nov 3, 2008
5
0
0
US
Hello all,

I need to do an expensive calculation in WM_MOUSEMOVE event.

I would like to know if I could remove extra WM_MOUSEMOVE and only process the most lastest WM_MOUSEMOVE?

Use PeekMessage??

Thank you
 
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
done=TRUE; // If So done=TRUE
}
else // If Not, Deal With Window Messages
{

if (msg.message == WM_MOUSEMOVE)
{
MSG oldMsg;

while (PeekMessage(&oldMsg,NULL,0,0,PM_NOREMOVE) && oldMsg.message == WM_MOUSEMOVE)
{
PeekMessage(&msg,NULL,0,0,PM_REMOVE);
}
}

TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}

This code doesn't help too much.
also I don't know whether the operations are correct.
 
Something like this should work (SINGLE-THREADED ONLY).
The idea is :
- Every WM_MOUSEMOVE stores its parameters in global variables, so every WM_MOUSEMOVE is lost except the last
- The message you post to yourself comes last in the message queue. Every WM_MOUSEMOVE between the first and the actual calculation is eaten.



Code:
// Assume (WM_APP+1) is not already in use by the program
// Otherwise use (WM_APP+something else)

#define WM_CALCULATION (WM_APP+1)


// At global scope 

BOOL   fMouseMove = FALSE;
WPARAM MouseMovewParam;
LPARAM MouseMovelParam;
HWND   hWndMouseMove;

void ExpensiveCalculation ( )
{
  // Calculates something using hWndMouseMove, MouseMovewParam and MouseMovelParam
}

// In your Windows procedure

case WM_MOUSEMOVE :
     MouseMovewParam = wParam;
     MouseMovelParam = lParam;
     hWmdMouseMove = hWnd;
     if ( !fMouseMove )
        { fMouseMove = TRUE;
          PostMessage ( hWnd, WM_CALCULATION, 0, 0 ); }
     break;

case WM_CALCULATION :
     ExpensiveCalculation ( );
     fMouseMove = FALSE;
     break;

Marcel
 
Hello Marcel,

So you think my code works in the way such that it only processed the last message for WM_MOUSEMOVE if more than one exists in the message queue?

Thank you
-Daniel
 
Sorry Daniel, I saw a reply but did not see you replied yourself.
Your code might work, I don't know without having examined it in detail.
The code I have posted is how I have solved a similar problem in the past. It does work and may be an alternative if yours does not work. If your code does work, forget mine.

Marcel
 
Dear Marcel,

Your solution looks very interesting to me.

I will give you a try:).

In fact, the major problem is that the user feels the mouse movement is not that smooth due to this heavy calculation. Your solution addresses the problem in the another way.

Thank you
-Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top