OpenCV读取图像显示在MFC窗口上
1. 在MFC上创建一个图片控件,自己修改ID,比如IDC_PICTURE,并且添加对应的变量 CStatic m_PictureControl
2. 在打开图片的按钮触发事件中添加如下代码:
namedWindow("ImageShow", CV_WINDOW_KEEPRATIO); // 用OpenCV创建一个窗口
CRect cWindowRect;
m_PictureControl.GetClientRect(&cWindowRect); // 获取控件窗口大小
int nWindowWidth = cWindowRect.Width();
int nWindowHeight = cWindowRect.Height();
resizeWindow("ImageShow", nWindowWidth, nWindowHeight);
hPictureWindow = (HWND)cvGetWindowHandle("ImageShow"); // 获取OpenCV窗口的句柄
hParentWindow = ::GetParent(hPictureWindow);
::SetParent(hPictureWindow, GetDlgItem(IDC_PICTURE)->m_hWnd); // 关联OpenCV窗口和MFC的控件窗口
::ShowWindow(hParentWindow, SW_HIDE);
GetDlgItem(IDC_PICTURE)->ShowWindow(0); // 开始不显示图片控件
CFileDialog dlg(TRUE, _T("*,