cvGetWindowHandle vs Windows FindWindow

Using Windows 10, Visual Studio 2017, and OpenCV 4.5, C++ MFC

I know that cvGetWindowHandle is supposed to be deprecated in OCV 4.5 although it is still available via highgui_c.h. I have been modifying by previous code that used cvGetWindowHandle to use the Windows API call FindWindow(). Most of the time I used the handle to a named window to set the cursor for that window but in this case I used it to set the parent. After successfully creating the namedwindow “video” and imshow and waitKey the old code was

HWND hWndVideo = (HWND)cvGetWindowHandle(“video”);
HWND hndPreviousParent = ::SetParent(hWndVideo, m_hWnd);

In this case hndPreviousParent was returned as a handle.

But changing the code to deprecate the cvGetWindowHandle to this code

HWND hWndVideo = (HWND)FindWindow(NULL, _T(“video”));
HWND hndPreviousParent = ::SetParent(hWndVideo, m_hWnd);

The hWndVideo is returned as a handle as before (although different) but the hndPreviousParent is returned as NULL meaning failure. I find this odd since FindWindow and SetParent are both Windows API calls and yet FindWindow is not getting a proper handle to an OCV namedwindow.

So for now I have to continue using cvGetWindowHandle but does anyone know why FindWindow does not work with OCV namedwindow or what the underlying code is to cvGetWindowHandle that can be used directly?

Thanks, Ed

i don’t think you can access that.
looking at the src, it is calling icvFindWindowByName, which looks up the handle from an internal map (does not call FindWindow() at all)

no, it means there was no previous parent window (toplevel)
see small print there, you might have to fiddle some other bits as well to set a parent

Yes, I found some code in the …\opencv\sources\modules\highgui\src\window_w32.cpp but, without studying it more closely and tracking the threads, I couldn’t tell off the bat what it was using other than going through the names of all open windows and looking for a match. I really don’t want to re-invent the wheel at this point.

According the MS docs for SetParent they state " If the function fails, the return value is NULL: per

Thanks for the fast response.


sorry, my bad, i mixed it up with hWndNewParent

Looking at the docs for FindWindow I notice that it states "Retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. " (emphasis mine). I think that is the problem. Apparanty the solution would be to use EnumWindows and do it like cvGetWindowHandle does it. As long as OCV keeps the highgui_c.h I guess I will continue to use this method. I hope they keep it in OCV 5,0

Thanks again.


it is still there
just don’t bet any money that it stays ;(