dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: How to detect mouse event outside dialog on SDI

  1. #1
    Join Date
    Jul 2006
    Posts
    203

    How to detect mouse event outside dialog on SDI

    In SDI application I open a dialog (derived from CFileDialog) by some menu command. On this dialog i am doing some mouse operations. If i press mouse on dialog and release it outside dialog then this dialog class dont detect mouse release event.
    Please suggest how to inform dialog class that mouse has been released outside it.

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: How to detect mouse event outside dialog on SDI

    It might work if you capture the mouse. See SetCapture for details
    Har Har

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: How to detect mouse event outside dialog on SDI

    Yep, SetCapture/ReleaseCapture is what you want...

    Viggy

  4. #4
    Join Date
    Jul 2006
    Posts
    203

    Re: How to detect mouse event outside dialog on SDI

    I used SetCapture in WM_LBUTTONDOWN and releasecapture in WM_LBUTTONUP in my dialog class. I clicked the mouse on dialog and move it to outside dialog then i release it outside dialog. But dialog class did not catch WM_LBUTTONUP. I want to catch the WM_LBUTTONUP outside my dialog. Please suggest to do it or give any example.

  5. #5
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: How to detect mouse event outside dialog on SDI

    The code bellow works perfectly well for me:

    Code:
    void CTestCaptureDlg::OnLButtonDown(UINT nFlags, CPoint point)
    {
    	SetCapture();
    
    	CDialog::OnLButtonDown(nFlags, point);
    }
    
    void CTestCaptureDlg::OnLButtonUp(UINT nFlags, CPoint point)
    {
    	AfxMessageBox( _T("Captured"));
    	ReleaseCapture();
    
    	CDialog::OnLButtonUp(nFlags, point);
    }
    Press the mouse button while being in the dialog, do not release it, move the mouse cursor outside the dialog, then release it. The message box will be shown.
    Har Har

  6. #6
    Join Date
    Jul 2006
    Posts
    203

    Re: How to detect mouse event outside dialog on SDI

    No message box doesn't appear.
    On this dialog i add a static box, i create a class for this static box which is derived from CStatic. and i'm handling mouse events in this static box class.
    From dialog clas in PreTranslateMessage function i write following code

    Suppose ID for this static box is IDC_IMAGE
    CRect rect;
    GetDlgItem(IDC_IMAGE)->GetWindowRect(rect);
    if(pMsg->message >= WM_MOUSEFIRST && pMsg->message <= WM_MBUTTONDBLCLK)
    {
    if(rect.PtInRect(pMsg->pt))
    GetDlgItem(IDC_IMAGE)->PreTranslateMessage(pMsg);
    }

    and in PreTranslateMessage of this static box class i write

    CPoint pt;
    GetCursorPos(&pt);
    ScreenToClient(&pt);

    switch(pMsg->message)
    {
    case WM_MOUSEMOVE :
    MouseMove(pt);
    return TRUE;
    case WM_LBUTTONDOWN :
    OnMouseDown(pt);
    return TRUE;
    case WM_LBUTTONUP :
    MouseUp(pt);
    return TRUE;
    }

    Now in OnMouseDown of static class i write SetCapture() and in MouseUp i write ReleaseCapture
    But when i release mouse outside static box or outside the dialog then it doesn't detect mouse up event

  7. #7
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: How to detect mouse event outside dialog on SDI

    You have to call SetCapture for exactly that window which should handle the WM_xxx events later.

    So if you want to receive a WM_LBUTTONUP message in a certain window (or in Terms of MFC in a certain CWnd derived class) you have to ensure that exactly this CWnd derived class has been called SetCapture (an no other window called SetCapture later).

    In your code snippet it seems to me that you're trying to handle the WM_LBUTTONDOWN message twice: In your given PreTranslateMessage function and in the OnMouseDown of the static class.

    Please debug your app, set a breakpoint at the line where you're calling SetCapture() and ensure that this is called really (I don't think so).

    Hope this helps

    PS: Please use code tags when showing code.

    With regards
    Programartist
    Ingo Bochmann

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)