我正在开发一个在Windows环境中的Visual Studio 2017上显示实时LiDAR点云的MFC应用程序。
现在所有的显示功能都运行得很好,我按如下方式实现了它们:
使用资源编辑器CDialog
向我的IDC_PICTURE
对话框添加了一个静态图片元素。
在我的类的头文件中定义了以下内容:
CStatic m_Picture;
CRect m_rectangle;
将静态图片(IDC_PICTURE
)与CStatic属性(m_picture
)相关联,如下所示:
void MyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PICTURE, m_Picture);
//other static lists and indicators
}
通过将CRect
元素与其相关联来获取图片尺寸和坐标,我按照以下步骤操作:
在我的OnInitDialog()
中,我将m_picture与m_rectangle
相关联,然后在单独的变量中获取维度,如下所示:
m_Picture.GetWindowRect(m_rectangle);
PicWidth = m_rectangle.Width();
PicHeight = m_rectangle.Height();
然后,为了显示点云,我编写了一个名为DrawData
的函数,它具有以下正文:
int MyDlg::DrawData(void)
{
CDC* pDC = m_Picture.GetDC();
CDC memDC;
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC, PicWidth, PicHeight);
//iterate over the points in my point cloud vector
// use memDC.setpixel() method to display the points one by one
//then:
pDC->StretchBlt(0, 0, PicWidth, PicHeight, &memDC, 0, 0, PicWidth, PicHeight, SRCCOPY);
bmp.DeleteObject();
memDC.DeleteDC();
ReleaseDC(pDC);
}
直到这里一切都很好。我的问题在于以下内容。
现在我需要仅在矩形内部(IDC_PICTURE)显示鼠标光标的坐标,并根据矩形的坐标系(不是整个窗口)显示。
所以,我通过执行以下操作在我的代码中集成了OnMouseMove()
函数:
BEGIN_MESSAGE_MAP(CalibrationDLG, CDialog)
ON_WM_PAINT()
ON_WM_MOUSEMOVE() // added this to take mouse movements into account
//OTHER BUTTON MESSAGES..
END_MESSAGE_MAP()
函数的主体如下所示:
void CalibrationDLG::OnMouseMove(UINT nFlags, CPoint point)
{
CDC* dc;
dc = GetDC();
CString str;
CPoint ptUpLeft = m_rect_calib.TopLeft(); // get the coordinates of the top left edge of the rectangle
CPoint ptDownRight = m_rect_calib.BottomRight(); // get the coordinates of the bottom right edge of the rectangle
if (point.x >= ptUpLeft.x && point.x <= ptUpLeft.x+ m_rect_calib.Width() && point.y >= ptUpLeft.y && point.y <= ptUpLeft.y+ m_rect_calib.Height())
{
str.Format(_T("x: %d y: %d"), point.x, point.y);
dc->TextOut(10, 10, str);
ReleaseDC(dc);
CDialog::OnMouseMove(nFlags, point);
}
}
我的问题是我得到的坐标不正确。 甚至在我的条件中定义的区域的限制:
if (point.x >= ptUpLeft.x &&
point.x <= ptUpLeft.x + m_rect_calib.Width() &&
point.y >= ptUpLeft.y &&
point.y <= ptUpLeft.y + m_rect_calib.Height())
似乎没有限制我正在寻找的区域。 它比实际IDC_PICTURE
表面小。
有谁知道我在这里缺少什么? 如何将鼠标坐标转换为仅相对于IDC_PICTURE区域? 谢谢
答案 0 :(得分:3)
坐标是相对于任何处理鼠标移动事件的客户区域,在您的情况下是对话框。您希望它们相对于图片控件的客户区域。您可以通过识别它们具有共同的屏幕坐标来在它们之间进行转换。
// transform from this dialog's coordinates to screen coordinates
this->ClientToScreen(&point);
// transform from screen coordinates to picture control coordinates
m_picture.ScreenToClient(&point);
如果不是处理整个对话框的鼠标移动,而是从CStatic
派生自己的图片类并处理它的OnMouseMove
,您可以取消所有这些转换。然后,您收到的点将位于图片控件的坐标中。