Link to home
Start Free TrialLog in
Avatar of microwhat
microwhat

asked on

Detecting mouse focus change between WM_LBUTTONDOWN and WM_LBUTTONUP

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

Avatar of DanRollins
DanRollins
Flag of United States of America image

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.
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.
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
}

       
    }
}
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;
       
    }
}
ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of microwhat
microwhat

ASKER

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.  
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.
Just wanted to note.  I appreciate the detailed examples provided by everyone else.