我正在开发一个MFC应用程序,在windows环境下,在Visual 2017上显示实时LiDAR点云。
现在,所有的显示功能都运行得很好,我已经按如下方式实现了它们:
使用资源编辑器,将一个静态图片元素添加到我的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区域?,谢谢
发布于 2018-03-26 14:14:29
坐标相对于任何处理鼠标移动事件的工作区,在您的例子中是对话框。您希望它们相对于图片控件的客户区域。您可以通过识别它们具有共同的屏幕坐标来在它们之间进行转换。
// 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,则可以取消所有这些转换。然后,您收到的点将已经在图片控件的坐标中。
https://stackoverflow.com/questions/49492872
复制相似问题