0
votes

I'm trying to get coordinate of the Rect (Picture control) but It's a bit glitchy.

So here're the process that I've done.

1st. made a picture control

2nd. I've earned WindowRect through GetWindowRect

// myDialogDlg.cpp

CRect m_rcDisp // (is acually in myDialogDlg.h)

BOOL myDialogDlg::OnInitDialog()
{
// IDC_PIC1 == ID of the (static) picture control
GetDlgItem(IDC_PIC1)->GetWindowRect(m_rcDisp);

...
}

3rd. I've made OnMouseMove event, and used PtInRect to make some action while mouse pointer is inside the picture control.

// myDialogDlg.cpp
void myDialogDlg::OnMouseMove(UINT nFlags, CPoint point)
{
    CString debug;
    {
        if (m_rcDisp.PtInRect(point))
        {
// my event starts
            OutputDebugString(_T("here"));
            if (m_CamTrig == CAMERA_TRIG_SW)
            {
                m_CurSor.x = point.x;
                m_CurSor.y = point.y;
                InvalidateRect(m_rcDisp, NULL);
            }
// my event ends
        }
    }
    CDialogEx::OnMouseMove(nFlags, point);
}

and... where actually PtInRect works is about here inside the red box

enter image description here

:( Hope I get the best answer. thx!

1
Looks like you're mixing up coordinates for WindowRect and ClientRect ... but it's hard to be more specific with the limited code you've shown. Think about adding a bit more context and rather more clarity. - Adrian Mole
@AdrianMole if you mean by clientrect, is OnMouseMove's CPoint based on ClientRect? What I'm trying to base on is WindowRect - 21stDivision
The solution may be as simple as calling ScreenToClient(&m_rcDisp); in your OnInitDialog function. - Adrian Mole
But probably a bit trickier. What happens, for example, if you move your dialog after the rectangle has been set in OnInitDialog? - Adrian Mole
@AdrianMole Well, I might not gonna need more answer. ScreenToClient(&m_rcDisp); worked. I've also tried GetClientRect(&m_rcDisp) but I don't get why that didnt work. Thanks a lot though! - 21stDivision

1 Answers

1
votes

GetWindowRect returns the window dimensions in screen coordinates. WM_MOUSEMOVE reports the mouse position in client coordinates. ScreenToClient1 can be used to translate from screen coordinates to client coordinates, making the picture control's window rectangle coordinates and hit testing function agree on a common origin.

This needs to be done whenever the picture control is moved relative to its parent dialog. If the picture control is never moved you only need to adjust the rectangle once.

This answer links to the Windows API documentation, since it's generally more informative and better maintained than the respective MFC entries. You can call either one from MFC code, though it's usually more convenient to just call into the CWnd members.


1Use MapWindowPoints instead if you plan on supporting RTL and LTR layouts. See Window Layout and Mirroring for guidance.