?
Solved

Detecting mouse focus change between WM_LBUTTONDOWN and WM_LBUTTONUP

Posted on 2010-01-12
8
Medium Priority
?
2,691 Views
Last Modified: 2013-12-03
I need to check if the mouse has been moved from the window that was click to trigger a WM_LBUTTONDOWN message, but only when a WM_LBUTTONUP message is sent. This is being  handled in the PretranslateMessage() function of the dialogs class. Im currently using GetWindowLong() to check the selected window.  The reason for this is the class(Button) that is being clicked will not process a click if the mouse is not over the window on WM_LMBUTTONUP. I need to catch this scenario. I've tried checking the return of GetWindowLong() and it returns the same window as when the mouse is in the target window(one or two) during a WM_LBUTTONDOW message.
BOOL MyDialogAppDlg::PreTranslateMessage(MSG* lpMsg) 
{
switch( lpMsg->message )
        {
case WM_LBUTTONDOWN:{
         
         int wnd=GetWindowLong(lpMsg->hwnd,GWL_ID);

         if(wnd==1001){
          //Button one click
         }else if(wnd==1002){
          //Button two clicked
         }
         
      }
case WM_LBUTTONUP:{         
         
         int wnd=GetWindowLong(lpMsg->hwnd,GWL_ID);

         if(wnd==1001){
          //Button one unclick
         }else if(wnd==1002){
          //Button two unclicked
         }else{
          //Some other window, process appropriately.
         }
    }
}

Open in new window

0
Comment
Question by:microwhat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 26300080
Are you attempting to process something like a "drag" of a button from one window to another?
If so, I suggest avoiding that.  Users expect that buttons work a certain way... if you click down on a button and then move off of it (while still pressed) then the button is supposed to spring back up and cause no action.  It's an important U/I "failsafe mechanism and all users expect it.
If you are going for some sort of drag operation, then I recommend using something other than a button as the object to be dragged --  perhaps a CStatic icon or something like that.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 26301249
I think you are desscribing that you want the default behaviour - to achieve that remove this BUTTONDOWN/UP code from the PreTranslateMessage and have handlers for the button click events.

Default behaviour - the button click event only fires when the mouse up is over the same button as the mouse down.

Resource editor, right mouse click on button, select add event handler, choose button click event.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 26301452
ps.  If you only want to respond to the mouse down event (button event independant of where the mouse is when button is released) then you need something slightly different eg.

BOOL MyDialogAppDlg::PreTranslateMessage(MSG* lpMsg)
{
static int wndFireEvent = 0;

switch( lpMsg->message )
        {
case WM_LBUTTONDOWN:{
         
         wndFireEvent =GetWindowLong(lpMsg->hwnd,GWL_ID);  //Get the current button
         
      }
case WM_LBUTTONUP:{        
         
        if(wndFireEvent != 0)  //was the mouse down on a button ?
{
  Do the work now
  wndFireEvent = 0;  //unset for future usage
}

       
    }
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 26301455
BOOL MyDialogAppDlg::PreTranslateMessage(MSG* lpMsg)
{
static int wndFireEvent = 0;

switch( lpMsg->message )
        {
case WM_LBUTTONDOWN:{
         
         wndFireEvent =GetWindowLong(lpMsg->hwnd,GWL_ID);  //Get the current button
break;
         
      }
case WM_LBUTTONUP:{        
         
        if(wndFireEvent != 0)  //was the mouse down on a button ?
{
  Do the work now
  wndFireEvent = 0;  //unset for future usage
}
break;
       
    }
}
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 1500 total points
ID: 26302172
Please read about WM_MOUSEMOVE notification:
http://msdn.microsoft.com/en-us/library/ms645616(VS.85).aspx

I think, you need to handle this message and it will be enough (or almost enough).

You can use GetCapture/SetCapture/ReleaseCapture if you need:
http://msdn.microsoft.com/en-us/library/ms646257(VS.85).aspx

GetCapture return the window handle (instead of GetWindowLong).

The simplest algorithm can be:
1. On WM_LBUTTONDOWN, call SetCapture (if GetCapture returns TRUE you need to call ReleaseCapture before). Assign TRUE to an internal boolean variable, for example, m_bPressed.

2. In mouse move (WM_MOUSEMOVE) check this m_bPressed and if it is TRUE check the coordinates - if you are still in the window. Because of SetCapture, even when the cursor is not in the rectangle of this window you are still receiving these messages. In this case you need to set m_bPressed as FALSE, ReleaseCapture and redraw the button.

3. On WM_BUTTONUP you release the capture, if m_bPressed is TRUE you sen WM_COMMAND to the parent window and redraw the button. m_bPressed becomes FALSE.

0
 

Author Comment

by:microwhat
ID: 26429295
Thanks for the suggestions.  Just getting back to this as i was pulled away from work due to private matters.  This issue has been resolved by other means.  
0
 

Author Closing Comment

by:microwhat
ID: 31676413
Although this isn't the exact answer and none of the others are, it help me think more outside the box and come to a alternate solution.
0
 

Author Comment

by:microwhat
ID: 26429340
Just wanted to note.  I appreciate the detailed examples provided by everyone else.
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question