Changing a window's style

I'm having the hardest time trying to get this to work. I'm doing some tutorials with OpenCV, and it's all going great, however I ran into some issues with how it implements windows.

There's no way for me to change the default window style... or so I thought. They at least give you a function that returns a polymorphic window handle in the form of a void pointer (which can then be cast as an hWnd). It's called cvGetWindowHandle and it's featured in the first line of the middle block of code below.

If someone can tell me what I'm doing wrong, it would be much appreciated! I'm trying to remove the icon, minimize, maximize and close buttons. Getting the one's complement of WS_SYSMENU in the style chunk ought to do it, right?

Here's some code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <Windows.h>
#include "highgui.h"

int main( int argc, char** argv )
{
	IplImage* img = cvLoadImage("limecat.jpg");		//load image from file
	cvNamedWindow("example1", CV_WINDOW_AUTOSIZE );		//generate window

///////////////////////////the section of code in question/////////////////////////////////////////
	HWND hw = (HWND)cvGetWindowHandle("example1");
	LONG nNewStyle = GetWindowLong(hw, GWL_STYLE) & ~WS_SYSMENU;
	SetWindowLong(hw,GWL_STYLE,nNewStyle);
	SetWindowPos(hw,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_DRAWFRAME);
///////////////////////////////////////////////////////////////////////////////////////////////////

	cvShowImage("example1", img );				//show image in window
	cvWaitKey(0);						//wait for keypress
	cvReleaseImage( &img );					//free image pointer
	cvDestroyWindow("example1");				//destroy window
}
Last edited on
Don't know if it makes any difference, especially because I don't know if you are compiling for 64-bit, but here goes anyway: You must use GetWindowLongPtr() in order for your code to work on both 32-bit and 64-bit compilations. Maybe the change helps.
Thanks for the reply! Actually, I was compiling for 32-bit, so that's not the issue. Although, that's a good tip for someone who may read this later on in a search.

I finally found out what was wrong! I can't believe I couldn't find anyone online with the answer to this. Many people seem to want to be able to do this, but there's no documentation about why OpenCV behaves this way. It turns out that the function "cvNamedWindow" generates more than one window handle. I verified this with a tool called WinExplorer. When I was calling "cvGetWindowHandle", it was getting the wrong handle. Maybe I was using this function for the wrong purpose? Who knows? Who cares?

Instead of using "cvGetWindowHandle", I used a more direct approach. I used the Windows API function "FindWindow".

The syntax of this function is FindWindow(const char* className, const char* windowName). The class name that OpenCV uses for any USABLE window generated with "cvNamedWindow" is "Main HighGUI class". You could leave this argument null, but you may be modifying windows that have nothing to do with your program that just happened to be called something like "example1", so it's not recommended.

The section of code for changing the window's style now looks like this:
1
2
3
4
HWND hw = FindWindow("Main HighGUI class", "example1");
LONG nNewStyle = GetWindowLong(hw, GWL_STYLE) & ~WS_SYSMENU;
SetWindowLong(hw,GWL_STYLE,nNewStyle);
SetWindowPos(hw,0,0,0,0,0,SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_DRAWFRAME);
Last edited on
If you had checked all return values from win32 api's called you had figured out what's wrong much sooner.
Topic archived. No new replies allowed.