0
votes

my app is mfc CDialog based. CImageView is inherited from CScrollView. my images are 1600x 1000. my client area is smaller than image size. I like to get the pixel coordinate that I left click on or move mouse on.

void CImageView::OnLButtonDown(UINT nFlags, CPoint pt)
{
CString s;
s.Format("%d %d", pt.x,pt.y);
AfxMessageBox(s);
}

If I scroll the vertical and horizontal bars all the way to the bottom and right respectively, i can view the pixel in most bottom right corner but its coordinate (x,y) is not correct, it is smaller than the 1600x1000. How can i get the correct pixel coordinate if scroll bars are moved?. It seems as the scroll bars get moved, the 1,1 pixel location moves as well.

1

1 Answers

1
votes

Use GetScrollPosition (MSDN here) to retrieve the xy coordinates of the top left of the client area according to the scroll bar positions, then add the xy offset you get from the mouse click.

void CImageView::OnLButtonDown(UINT nFlags, CPoint pt)
{
    CPoint ptImage = GetScrollPosition() + pt;
    CString s;
    s.Format("%d %d", ptImage.x, ptImage.y);
    AfxMessageBox(s);
}