opencv4+中cvGetWindowHandle()的替代方案
Alternative of cvGetWindowHandle() in opencv4+
我在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
相关文章:
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- 计算每个节点的树高,帮助我解释这个代码解决方案
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- visual c++,如何获取解决方案目录中的代码
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- C++Matching Brackets 2解决方案不起作用
- 在 ubuntu3 上C++ goto 定义有什么解决方案吗16.04?
- C++11 中不同类型的对象的 std::array 的替代方案
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- 我的固定时间步长与增量时间和插值的解决方案是错误的吗?
- 无法在问题解决方案中执行输出逻辑
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 从预序遍历构造 bst 的 c++ 和 python 解决方案之间的区别
- 在一个解决方案中针对第三方静态库 (Creo) 的不同版本(版本)进行构建
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- N-queen问题:无法弄清楚为什么我的解决方案不起作用
- 从排序数组中删除重复项,具有不同代码方式的相同解决方案具有不同的输出