Thursday, April 11, 2013

Force opencv image window to always on top

Recently I needed to make an opencv(Open Source Computer Vision) image window always on top of any other windows. I followed the answers in Stackoverflow but couldn't make it really work. The solution proposed by it was to use setWindowPos API to make the image window TOPMOST.

I have to say today is my lucky day. I finally found if we set the parent of that image window to TOPMOST, then it works like a charm.
        HWND cvHwnd = (HWND )cvGetWindowHandle("window");
        HWND cvParent = ::GetParent(cvHwnd);
        ::ShowWindow(cvParent, SW_MINIMIZE);
        ::ShowWindow(cvParent, SW_RESTORE);
        ::SetWindowPos(cvParent, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);