Changing the style of a Win32 window

Started by
4 comments, last by cgibson 20 years, 1 month ago
Hi, This post is in question to how to change the style of a window without destroying the old window and creating a new one. I understand that the ::SetWindowLong(...) function is to be used. Below is the basic code that I am trying to get to work.

void ChangeToWindowedMode( HWND hWnd_ ) {
    ::SetWindowLong( hWnd_, GWL_STYLE, WS_OVERLAPPED );
    ::ShowWindow( hWnd_, SW_SHOW );
    ::UpdateWindow( hWnd_ );
}// end ChangeToWindowedMode()

void ChangeToFullscreenMode( HWND hWnd_ ) {
    ::SetWindowLong( hWnd_, GWL_STYLE, WS_POPUP );
    ::ShowWindow( hWnd_, SW_SHOW );
    ::UpdateWindow( hWnd_ );
}// end ChangeToFullscreenMode()
[\code]

Okay, here is my problem.  I begin the application by creating a window with a title bar and border.  When the user presses the F1 key, it should toggle between fullscreen and windowed mode ( a window with WS_POPUP for fullscreen and WS_OVERLAPPED for windowed ).  I know the toggle isn''t the problem because I have it writing to a log file to let me know the mode the application is currently in.  

When the user presses the F1 key for the first time, it successfully changes the window''s style to WS_POPUP, but anytime the user presses it after the first time, the window''s style doesn''t change.  

When I setup the application to initially create the window with the WS_POPUP style, no change occurred.  Even upon the first press of the F1 key resulted in no change.  Does something different have to be performed in order to change styles from the WS_POPUP style?  I have search MSDN for some sort of insight, but nothing has proven successful.

I have even replaced the ::ShowWindow( ... ) and ::UpdateWindow( ... ) calls with the ::SetWindowPos( ... ) function and still get the same undesireable results.

I am currently running this code on WinXP.  I am not sure if the OS has anything to do with it.  Let me know if there is any additional information needed to make this work.

I appreciate any assistance in helping me solve my problem.  I don''t know what else to do.  

Thank you in advance.   
Advertisement
To change the style and a lot of other things call SetWindowLongPtr. Look it up in MSDN.
VLAjarn: Cause it (linux) NEVER crashesMrPr0Grmer: lol ur wrongMrPr0Grmer: RedHat crashesVLAjarn: I'm saying good builds of linux
It still does the same thing.
Are you sure that your application is processing the keyboard event for the F1 key when you''re window style is WS_POPUP?
Im not really sure whats wrong
I decided to modify the code

void ChangeWindowStyle( HWND hWnd, bool bWindowed ){  DWORD dwStyle = 0; // The window style  // Set the window style  if( bWindowed )    dwStyle = WS_OVERLAPPEDWINDOW|WS_CLIPSIBLINGS|WS_CLIPCHILDREN;  else    dwStyle = WS_POPUP|WS_CLIPSIBLINGS|WS_CLIPCHILDREN;  // Update the window  SetWindowLong( hWnd, GWL_STYLE, dwStyle );  ShowWindow( hWnd, SW_SHOW );  SetForegroundWindow( hWnd );}
I even tried a test that was unsuccessful as well. Here is the test code.

#include < windows.h >bool bFullscreen = false;LRESULT CALLBACK WindowProc( HWND hWnd_, UINT msg_, WPARAM w_, LPARAM l_ ) {	switch( msg_ ) {	case WM_KEYDOWN:		{			switch( w_ ) {			case VK_ESCAPE:				::SendMessage( hWnd_, WM_CLOSE, 0, 0 );				break;			case VK_SPACE:				{					bFullscreen = !bFullscreen;					DWORD dwStyles = 0;					if( bFullscreen ) {						dwStyles = WS_POPUP;						::SetWindowLong( hWnd_, GWL_STYLE, dwStyles );						::ShowWindow( hWnd_, SW_SHOW );					}					else {						dwStyles = WS_OVERLAPPED;						::SetWindowLong( hWnd_, GWL_STYLE, dwStyles );						::ShowWindow( hWnd_, SW_SHOW );					}				}break;			}		}break;	case WM_CLOSE:		::SendMessage( hWnd_, WM_DESTROY, 0, 0 );		break;	case WM_DESTROY:		::PostQuitMessage( 0 );		break;		default:		return ::DefWindowProc( hWnd_, msg_, w_, l_ );	}	return 0;}int WINAPI WinMain( HINSTANCE hInst_, HINSTANCE hp_, LPSTR lpcmd_, int nShow_ ) {	WNDCLASS wc;	wc.cbClsExtra = 0;	wc.cbWndExtra = 0;	wc.hbrBackground = ( HBRUSH )::GetStockObject( BLACK_BRUSH );	wc.hCursor = ::LoadCursor( NULL, IDC_ARROW );	wc.hIcon = ::LoadIcon( NULL, IDI_APPLICATION );	wc.hInstance = hInst_;	wc.lpfnWndProc = WindowProc;	wc.lpszClassName = "TEST";	wc.lpszMenuName = NULL;	wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;	if( 0 == ::RegisterClass( &wc ) ) {		return 1;	}	HWND hWnd = ::CreateWindow( "TEST", "TEST", WS_OVERLAPPED, 50, 50, 800, 600, NULL, NULL, hInst_, NULL );	if( NULL == hWnd ) {		return 1;	}	::ShowWindow( hWnd, SW_SHOW );	::UpdateWindow( hWnd );	MSG msg;	while( 1 ) {		if( 0 == PeekMessage( &msg, hWnd, 0, 0, PM_REMOVE ) ) {			if( WM_QUIT == msg.message ) {				break;			}			::TranslateMessage( &msg );			::DispatchMessage( &msg );		}	}	return msg.wParam;}


Tell me if you see something strange here. And thank you to all that have responded so far. I really appreciate your assistance.

This topic is closed to new replies.

Advertisement