网上看了部分代码,但是觉得或多或少有些坐标位置上的错误,特别明显是体现在GetWindowRect这个API上,MSDN中说他返回的坐标是以屏幕左上角为基点。所以在对话框中必须先转换成基于客户端的坐标,方法是调用ScreenToClient,然后才能够调用CvvImage::DrawToHDC函数。具体步骤如下
代码
typedef struct
{
CDlg * pDlg;
}PARAMETER, *PPARAMETER;
void ShowImage(IplImage * img, void * p)
{
PPARAMETER param = (PPARAMETER)p;
CDlg * pDigitDlg = param->pDlg;
//Opencv自带类
CvvImage cimg;
cimg.CopyOf(img);
CDC *pDC = pDigitDlg->GetDC();
HDC hDC= pDC->GetSafeHdc();
RECT rect;
//获得picture控件的屏幕坐标,IDC_STATIC_PICTURE是图片控件的ID
pDigitDlg->GetDlgItem(IDC_STATIC_PICTURE)->GetWindowRect(&rect);
POINT point;
point.x = rect.left;
point.y = rect.top;
//调整屏幕坐标到客户端坐标
ScreenToClient(pDigitDlg->m_hWnd,&point);
RECT rect2 = rect;
rect.top = point.y;
rect.bottom = point.y + (rect2.bottom - rect2.top);
rect.left = point.x;
rect.right = point.x + (rect2.right - rect2.left);
//CvvImage::DrawToHDC第二参数需要相对客户端的坐标
cimg.DrawToHDC(hDC,&rect);
pDigitDlg->ReleaseDC(pDC);
//无需显示重绘
//pDigitDlg->InvalidateRect(&rect,TRUE);
//pDigitDlg->UpdateWindow();
}
{
CDlg * pDlg;
}PARAMETER, *PPARAMETER;
void ShowImage(IplImage * img, void * p)
{
PPARAMETER param = (PPARAMETER)p;
CDlg * pDigitDlg = param->pDlg;
//Opencv自带类
CvvImage cimg;
cimg.CopyOf(img);
CDC *pDC = pDigitDlg->GetDC();
HDC hDC= pDC->GetSafeHdc();
RECT rect;
//获得picture控件的屏幕坐标,IDC_STATIC_PICTURE是图片控件的ID
pDigitDlg->GetDlgItem(IDC_STATIC_PICTURE)->GetWindowRect(&rect);
POINT point;
point.x = rect.left;
point.y = rect.top;
//调整屏幕坐标到客户端坐标
ScreenToClient(pDigitDlg->m_hWnd,&point);
RECT rect2 = rect;
rect.top = point.y;
rect.bottom = point.y + (rect2.bottom - rect2.top);
rect.left = point.x;
rect.right = point.x + (rect2.right - rect2.left);
//CvvImage::DrawToHDC第二参数需要相对客户端的坐标
cimg.DrawToHDC(hDC,&rect);
pDigitDlg->ReleaseDC(pDC);
//无需显示重绘
//pDigitDlg->InvalidateRect(&rect,TRUE);
//pDigitDlg->UpdateWindow();
}