opencv4+中cvGetWindowHandle()的替代方案

Alternative of cvGetWindowHandle() in opencv4+

本文关键字:方案 cvGetWindowHandle opencv4+      更新时间:2023-10-16

我在MFC应用程序中使用Opencv3.4.8。我使用cvGetWindowHandle((来获取窗口的句柄,如下所示;

namedWindow(windowname, WINDOW_AUTOSIZE);
HWND hWnd = (HWND) cvGetWindowHandle(windowname.c_str());
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, GetDlgItem(IDC_PICTURE)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);
CWnd* pWnd = new CWnd();
pWnd->CWnd::Attach(hParent);

现在我想升级到Opencv4+,而Opencv4+中没有cvGetWindowHandle((。我想知道Opencv4+中是否有替代功能,如果没有,我如何在Opencv4+中完成同样的任务。

#3091。getWindowProperty(window_name,WND_PROP_AUTOSIZE(>=0应替换cvGetWindowHandle((

只包括

#include <opencv2/highgui/highgui_c.h>

然后u可以进一步使用cvGetWindowHandle((。

问候DiebBlue

对于那些仍在MFC对话框中使用Opencv的人,有一个不同的解决方案

FindWindows返回父窗口句柄,MFC与子窗口一起工作,因此您需要FindWindow和FindWindowEx。

MFC和Opencv4+的新源代码

namedWindow(windowname, WINDOW_AUTOSIZE);
////// This will work on opencv 4.X //////
HWND hParent = (HWND)FindWindow(NULL, windowname.c_str());
HWND hWnd = (HWND)FindWindowEx(hParent, NULL, L"HighGUI class", NULL);
::SetParent(hWnd, GetDlgItem(IDC_PICTURE)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);
CWnd* pWnd = new CWnd();
pWnd->CWnd::Attach(hParent);

也许您仍然有麻烦,因为字符串到LPCWSTR的转换失败,hParent返回NULL。有很多方法可以将字符串转换为LPCWSTR,但由于您使用的是MFC,请尝试

namedWindow(windowname, WINDOW_AUTOSIZE);
////// This will work on opencv 4.X //////
CString CstrWindowname = windowname.data();

HWND hParent = (HWND)FindWindow(NULL, CstrWindowname);
HWND hWnd = (HWND)FindWindowEx(hParent, NULL, L"HighGUI class", NULL);
::SetParent(hWnd, GetDlgItem(IDC_PICTURE)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);
CWnd* pWnd = new CWnd();
pWnd->CWnd::Attach(hParent);

新代码应该取代这个旧代码

namedWindow(windowname, WINDOW_AUTOSIZE);
///// OLD version. Used on opencv 3.X on MFC Dialog Box /////
HWND hWnd = (HWND) cvGetWindowHandle(windowname.c_str());    
HWND hParent = ::GetParent(hWnd);
::SetParent(hWnd, GetDlgItem(IDC_PICTURE)->m_hWnd);
::ShowWindow(hParent, SW_HIDE);
CWnd* pWnd = new CWnd();
pWnd->CWnd::Attach(hParent);

试试,

我在应用程序中使用带有QT的OpenCV4.5.0。QtSdk的版本是Qt5.14.2。
"imshow"窗口嵌入到我的应用程序中的QWidget中

#include <opencv2/highgui/highgui_c.h>
...
/**
* @brief MainWindow::EmbedCvWindow
* @param pWnd : a handle of parent window
* @param strWndName: name of opencv`s window 
* @param w: widget`s width
* @param h: widget`s height
*/
void MainWindow::EmbedCvWindow( HWND pWnd, QString strWndName, int w, int h )
{
// create a window named 'strWndName'. OpenCVSdk is compiled with Qt,So the parameter is set to 'CV_GUI_NORMAL', it has no status bar and  no tool bar
cvNamedWindow(strWndName.toStdString().c_str(), CV_GUI_NORMAL);// flag is only for Qt
//The cvWindow of opencv with qt is a subclass of QWidget,so it can`t get right result by nomal method.     
//HWND hWnd =  (HWND)cvGetWindowHandle(strWndName.toStdString().c_str());

//first way: get the handle of independent window 'imshow' 
//HWND hWnd =  (HWND)(((QWidget*)cvGetWindowHandle(strWndName.toStdString().c_str()))->winId());

//second way:get the handle of independent window 'imshow' by winAPI 'FindWindow'
HWND hWnd =::FindWindow(NULL,strWndName.toStdWString().c_str()); 
// get the handle of parent window 'imshow'
HWND hParent = ::GetParent((HWND)hWnd);

// The client area of 'imshow' is embedded in a QWidget in my application.
::SetParent(hWnd, pWnd);
// hide status bar and tool bar,
::SetWindowLong(hWnd,GWL_STYLE,WS_CLIPCHILDREN|WS_CLIPSIBLINGS|WS_CHILDWINDOW|WS_VISIBLE);
::SetWindowLong(hWnd,GWL_EXSTYLE,WS_EX_LEFT|WS_EX_RTLREADING/*|WS_EX_TOPMOST*/);
//hide the old parent window of 'imshow'
::ShowWindow(hParent, SW_HIDE);
//resize width and height of client area 
cvResizeWindow(strWndName.toStdString().c_str(), w,h);

//set postion of client area. (very importent)
cvMoveWindow(strWndName.toStdString().c_str(),0,0); 
}

namedWindow ("SrcView", WINDOW_AUTOSIZE);
HWND hWnd = (HWND)cvGetWindowHandle ("SrcView");
HWND hParent = (HWND)FindWindow (NULL, L"SrcView");
HWND hOrgParent = ::SetParent (hWnd, GetDlgItem (IDC_STATIC_SRC_VIEW)->m_hWnd);
::ShowWindow (hOrgParent, SW_HIDE); // This row is important, if you comment this row, an extra window will be showed)

这适用于我的MFC应用程序,无论它是使用OpenCV 3.1还是OpenCV 4.5

相关文章: