_TrackMouseEvent not working
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: _TrackMouseEvent not working

  1. #1
    Join Date
    Dec 2004
    Posts
    61

    Unhappy _TrackMouseEvent not working

    Dear Gurus,

    I'm facing a problem with the _TrackMouseEvent
    Below is my coding:

    Code:
    void CMyDlg::OnMouseMove(UINT nFlags, CPoint point)
    {
     
    		if(!m_bStartTracking) //set the m_bStartTracking =FALSE in constructor
    		{
    			SetCapture();
    			m_bStartTracking = TRUE;
    			StartTrackMouseLeave();
    			printf("Start Tracking...\n");
    		}
    		
    		CDialog::OnMouseMove(nFlags, ptNorm);
    }
    
    BOOL CMyDlg::StartTrackMouseLeave()
    {
    	TRACKMOUSEEVENT tme;
    	ZeroMemory( &tme, sizeof(TRACKMOUSEEVENT));
    	tme.cbSize = sizeof(TRACKMOUSEEVENT);
    	tme.dwFlags = TME_LEAVE;
    	tme.hwndTrack = this->m_hWnd;
    	tme.dwHoverTime = HOVER_DEFAULT;
    	_TrackMouseEvent(&tme);
    
    	return TRUE;
    }
    
    LRESULT CMyDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
    {
    	if(message == WM_MOUSELEAVE)
    	{
    		m_bStartTracking = FALSE;
    		ReleaseCapture();
    		printf("Stop Tracking......1\n");
    	}
    
    	return CDialog::WindowProc(message, wParam, lParam);
    }
    as my mouse cursor move inside the dialog, i get this message:
    Start tracking...
    Stop tracking...1
    Start tracking...
    Stop tracking...1
    ...
    Start tracking...
    Stop tracking...1

    The message stop when my mouse leave the dialog client area.
    The above message repeat again when my mouse go inside the dialog client area.

    This is not what i want.
    The code able to detect when the mouse go inside the dialog client area.
    But it seem to be don't know when it leave the client area.
    It entering the dialog area and leave the client area at the same time(refer to the message above)
    That is why i get the message above.
    But the _TrackMouseEvent only will post the message WM_MOUSELEAVE when the mouse leave the
    client area but it did not work that way. Why?

    I have check the MSDN and found that the the default area of the mouse hover area just as the mouse click area.
    That is 4 pixels. so i have try to adjust the area to the dialog client are with the code below:
    But i'm still getting the same result.

    Code:
    void CMyDlg::OnMouseMove(UINT nFlags, CPoint point)
    {
     
    		if(!m_bStartTracking) //set the m_bStartTracking =FALSE in constructor
    		{	
                      CRect rcWnd(0,0,0,0);	
                      GetWindowRect(&rcWnd);
                      SystemParametersInfo(SPI_SETMOUSEHOVERHEIGHT,rcWnd.Height(), 0, 0);
    		  SystemParametersInfo(SPI_SETMOUSEHOVERWIDTH,rcWnd.Width(), 0, 0);
    
    
    			SetCapture();
    			m_bStartTracking = TRUE;
    			StartTrackMouseLeave();
    			printf("Start Tracking...\n");
    		}
    		
    		CDialog::OnMouseMove(nFlags, ptNorm);
    }
    I have search through the similar post in Codeguru but not luck, I did not found the solution there.
    Can somebody help me.
    Thanks.
    Last edited by VbEndUser; October 30th, 2012 at 11:23 PM.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,004

    Re: _TrackMouseEvent not working

    First, after more than 50 posts, you should know how to use code tags. Please edit your post and use them.
    Second, it's not clear to me what you want to achieve.
    Third, why are you using SetCapture and TrackMouseEvent at the same time? You should use either depending on the behavior that you want.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Dec 2004
    Posts
    61

    Re: _TrackMouseEvent not working

    Dear D_Drmmr,

    Thanks for pointing out my thread's problem.
    I always want to post my code in the window just that i did not know the script.
    There was no shortcut key or button.

    Refer back to my question:
    I not sure how to detect the mouse in and mouse leave in a control which attached to my dialogbox.
    Please refer to my attachment.

    I need to use Setcapture to detect my mouse position when it move over the control.
    Of cause we can use control.RectInPt(mouse position) to track the mouse whether is in the control rect or not
    If another window pop up, then i not able to to release the mouse using ReleaseCapture.

    I will ReleaseCapture when user doubleclick on the control and at the same time another window will pop up.
    Hope my question is clear.
    Thanks.
    Attached Images Attached Images  

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,004

    Re: _TrackMouseEvent not working

    Quote Originally Posted by VbEndUser View Post
    I not sure how to detect the mouse in and mouse leave in a control which attached to my dialogbox.
    Please refer to my attachment.

    I need to use Setcapture to detect my mouse position when it move over the control.
    You don't need to call SetCapture to detect when the mouse is inside our outside your control. When the mouse moves inside your control, you receive a WM_MOUSEMOVE message (i.e. your OnMouseMove function will be called). In that function you call TrackMouseEvent, such that you will receive a WM_MOUSELEAVE message whenever the mouse cursor leaves your control (you will not receive WM_MOUSEMOVE messages when the mouse cursor moves outside the control).

    You should only use SetCapture and ReleaseCapture if you want to keep receiving WM_MOUSEMOVE messages when the mouse moves outside the control. E.g. you can call SetCapture when the user starts dragging inside your control and ReleaseCapture when the dragging ends.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center