The SDK functions SetCapture and ReleaseCapture are a bit finicky but the sample code I have here should work. In the code below, OnLButtonDown calls SetCapture to start mouse capture and OnLButtonUp calls ReleaseCapture to stop the capture. Then the mouse coordinates are obtained from the MSG structure for the WM_LBUTTONUP message.
void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point) {
HCURSOR hCursor=::LoadCursor(NULL, (LPCTSTR)IDC_CROSS);
ShowWindow(SW_HIDE);
if (hCursor)
m_hOldCursor = SetCursor(hCursor);
else
MessageBox("No cursor");
SetCapture();
}
void CMainFrame::OnLButtonUp(UINT nFlags, CPoint point) {
if (IsVisible()) { // if not hidden then we are not capturing
CFrameWnd::OnLButtonUp(nFlags, point);
return;
}
ReleaseCapture();
if (m_hOldCursor)
SetCursor(m_hOldCursor);
m_hOldCursor = NULL;
const MSG *pMsg = CWnd::GetCurrentMessage();
// The mouse is at pMsg->pt.x, pMsg->pt.y
ShowWindow(SW_SHOW);
}
In my CMainFrame class I have:
HCURSOR m_hOldCursor;
See my Visual C++ Programmer Stuff page for more C++ stuff.