-
May 19th, 2008, 07:24 AM
#1
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.
-
May 19th, 2008, 08:21 AM
#2
Re: How to detect mouse event outside dialog on SDI
It might work if you capture the mouse. See SetCapture for details
Har Har
-
May 19th, 2008, 03:40 PM
#3
Re: How to detect mouse event outside dialog on SDI
Yep, SetCapture/ReleaseCapture is what you want...
Viggy
-
May 20th, 2008, 04:58 AM
#4
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.
-
May 20th, 2008, 05:42 AM
#5
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
-
May 20th, 2008, 06:20 AM
#6
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
-
May 20th, 2008, 06:47 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|