OpenCv将图像窗口嵌入到MFC Picture Control控件上,resizeWindow调整窗口大小和控件大小一致,WINDOW_NORMAL设置图像自适应窗口
int width, height;
CRect picRect;
GetDlgItem(IDC_DISPLAY_STATIC)->GetClientRect(&picRect);
width = picRect.right;
height = picRect.bottom;
namedWindow("MyPicture", WINDOW_NORMAL);
resizeWindow("MyPicture",width,height);
HWND hWnd = (HWND)cvGetWindowHandle("MyPicture");
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, GetDlgItem(IDC_DISPLAY_STATIC)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);
char *fn="E:\\img\\18-11-52O.bmp";
Mat image=imread(fn);
if(!image.data)
MessageBox(_T("图像加载失败!"));
imshow("MyPicture",image);
显示到Qt控件,(参考)
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 打开摄像头
bool ok = mvCapture.open(0);
namedWindow("view", WINDOW_AUTOSIZE);
HWND Whnd;
Whnd=(HWND)cvGetWindowHandle("view");
if (Whnd)
{
HWND parentHwnd;
parentHwnd = (HWND)GetWindowLong(Whnd,GWL_HWNDPARENT);
if(parentHwnd)
{
ShowWindow(parentHwnd,SW_HIDE);//隐藏
}
SetWindowLong(parentHwnd,GWL_STYLE,/*WS_CLIPCHILDREN|*/WS_CLIPSIBLINGS|WS_CHILDWINDOW);//隐藏标题栏
SetWindowLong(parentHwnd,GWL_EXSTYLE,WS_EX_TOPMOST);//在最前面
if(parentHwnd)
{
ShowWindow(parentHwnd,SW_SHOW);//显示
}
}
QObject::connect(&dataTimer, SIGNAL(timeout()), this, SLOT(OnTimer()));
dataTimer.start(100);
//QWidget* a = QWidget::find((WId)hWnd1);
//ui->gridLayout->addWidget(a);
}
MainWindow::~MainWindow()
{
delete ui;
}
// 显示定时器
void MainWindow::OnTimer()
{
Mat srcImage;
mvCapture>>srcImage;
imshow("view", srcImage);
}