No window frame when going from full screen back to windowed mode

Started by
9 comments, last by howie_007 11 years, 3 months ago

I have read through tons of post here on this subject and looked at numerous code examples and for the life of me, I can't get this to work correctly. As far as I can tell, I'm not doing anything different then what I have been seeing.

I start out in windowed mode and then change it to full screen mode and then change it back to windowed mode. Even though I'm resetting the styles via "SetWindowLong" and then calling "SetWindowPos", I'm left with a window with no window frame.

I've tested this ever way I can think of. Any idea of what I might be missing?

I define the styles in the constructor as class members...


CXWindow::CXWindow() :
...
windowedStyle(WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX),
windowedExStyle(WS_EX_APPWINDOW|WS_EX_WINDOWEDGE),
fullScreenStyle(WS_POPUP|WS_VISIBLE),
fullScreenExStyle(WS_EX_TOPMOST)

This is how I create the window.


void CXWindow::CreateXWindow( const HINSTANCE hInst )
{
    uint dwExStyle;
    uint dwStyle;

	RECT rect;

	// Set the rect
    SetRect( &rect, 0, 0, (int)CSettings::Instance().GetSize().w, (int)CSettings::Instance().GetSize().h );

    if( CSettings::Instance().GetFullScreen() )
    {
        dwExStyle = fullScreenExStyle;
        dwStyle = fullScreenStyle;
    }
	else
    {       
        // Setup the styles
        dwExStyle = windowedExStyle;
        dwStyle = windowedStyle;   
    }
    
    // Create the window
    hWnd = CreateWindowEx( dwExStyle,
						   CSettings::Instance().GetGameWndClassName().c_str(),
						   "",
                           dwStyle,
						   rect.left,
						   rect.top,
                           rect.right,
						   rect.bottom,
                           NULL,
						   NULL,
						   hInst,
						   NULL );

	if( hWnd == NULL )
		throw NExcept::CCriticalException("Create Window Error",
			boost::str( boost::format("There was an error creating the game window.\n\n%s\nLine: %s") % __FUNCTION__ % __LINE__ ));

	if( !CSettings::Instance().GetFullScreen() )
	{
		// Adjust the rect so that the client size of the window is the exact size of the needed resolution
		AdjustWindowRectEx( &rect, GetWindowStyle(hWnd), GetMenu(hWnd) != NULL, GetWindowExStyle(hWnd) );

		// Position the window in the center of the screen
		SetWindowPos( hWnd,
					  HWND_NOTOPMOST,
					  (GetSystemMetrics( SM_CXSCREEN ) - (rect.right-rect.left)) / 2, 
					  (GetSystemMetrics( SM_CYSCREEN ) - (rect.bottom-rect.top)) / 2,
					  rect.right - rect.left,
					  rect.bottom - rect.top,
					  SWP_NOACTIVATE );
	}

}   // CreateXWindow

I call this prior to resetting the DirectX device. I've even recalled it after the device was reset.


void CXWindow::ResetWindowStyle()
{
	if( CSettings::Instance().WasScreenModeChanged() )
	{
		uint dwExStyle = windowedExStyle;
		uint dwStyle = windowedStyle;

		if( CSettings::Instance().GetFullScreen() )
		{
			dwExStyle = fullScreenExStyle;
			dwStyle = fullScreenStyle;
		}

		// Change the windows styles
		SetWindowLong( hWnd, GWL_STYLE, dwStyle);
		SetWindowLong( hWnd, GWL_EXSTYLE, dwExStyle);
	}

}	// ResetWindowStyle

This is what I call after the device reset. One odd thing to note is that in this function, "AdjustWindowRectEx" does not change the rect like it does when the window was first created. The rect remains unchanged.


void CXWindow::ResetWindowSize()
{
RECT rect;

	// Set the rect
    SetRect( &rect, 0, 0, (int)CSettings::Instance().GetSizeChange().w, (int)CSettings::Instance().GetSizeChange().h );

	if( CSettings::Instance().WasResolutionChanged() || CSettings::Instance().WasScreenModeChanged() )
	{
		if( CSettings::Instance().WasResolutionChanged() )
		{
			// Recalculate the default size and ratio
			CSettings::Instance().CalcRatio( CSettings::Instance().GetSizeChange() );

			// Recalculate the projection matrixes
			CalcProjMatrix( CSettings::Instance().GetSizeChange(),
							CSettings::Instance().GetDefaultSize() );
		}

		if( !CSettings::Instance().WasScreenModeChanged() )
		{
			// Adjust the rect so that the client size of the window is the exact size of the needed resolution
			AdjustWindowRectEx( &rect, GetWindowStyle(hWnd), GetMenu(hWnd) != NULL, GetWindowExStyle(hWnd) );

			// Position the window in the center of the screen
			SetWindowPos( hWnd,
						  HWND_TOP,
						  (GetSystemMetrics( SM_CXSCREEN ) - (rect.right-rect.left)) / 2, 
						  (GetSystemMetrics( SM_CYSCREEN ) - (rect.bottom-rect.top)) / 2,
						  rect.right - rect.left,
						  rect.bottom - rect.top,
						  SWP_FRAMECHANGED|SWP_SHOWWINDOW );
		}
	}

}	// ResetWindow
Advertisement

I see that you're resetting the window size, but did you reset the device as well?

There is no call to D3D Reset (or even your own custom implementation of Clear and CreateDevice).

Check this out.

Thanks for the link but that example only deals with resizing the window. Everything I'm currently doing works fine except going from full screen to window mode.

Here are my reset functions.


void CGameTemplate::HandleDeviceReset( CSize<float> & res, bool vSync, bool windowed )
{
	// Free the shader(s)
	CShader::Instance().Free();

	// All things created by the device that is not managed need 
	// to be Released before the device can be reset
	CXWindow::Instance().HandleDeviceReset( res, vSync, windowed );

	// Reload and Enumerate through all the shaders to set their default values
	CShader::Instance().LoadFromXML( string("data/shaders/startup_shader_effects.cfg") );
	CShader::Instance().LoadFromXML( string("data/shaders/shader_effects.cfg") );
	CShader::Instance().EnumerateShaderInit();

}	// HandleDeviceReset

void CXWindow::HandleDeviceReset( CSize<float> & res, bool vSync, bool windowed )
{
    // function for releasing device created members for device
    // lost reset. The reset will error if these are not released
    ReleaseDeviceCreatedMembers();

    // Set the resolution
    dxpp.BackBufferWidth  = res.w;
    dxpp.BackBufferHeight = res.h;

    // Set the windowed/full screen mode
    dxpp.Windowed = windowed;

    // Set the vsync
    dxpp.PresentationInterval = vSync ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE;

    // All things created by the device that is not managed
    // need to be Released before the device can be reset
    if( spDXDevice->Reset( &dxpp ) == D3DERR_INVALIDCALL )
        throw NExcept::CCriticalException("DirectX Reset Error",
            boost::str( boost::format("Call to Reset() failed with D3DERR_INVALIDCALL!\n\n%s\nLine: %s") % __FUNCTION__ % __LINE__ ));

    // Buffers and render states that need to be reset/recreated during a device reset
    ResetXDevice();

}    // HandleDeviceReset

Here's the function for creating the DirectX device.


void CXWindow::CreateXDevice()
{
    D3DDISPLAYMODE dxdm;
    HRESULT hresult;
    D3DFORMAT depthFormat[] = {D3DFMT_D32, D3DFMT_D24X8, D3DFMT_D16, (D3DFORMAT)0};
    D3DFORMAT selectedDepthFormat;
    int counter = 0;

	// Set the initial buffer clear mask
	if( CSettings::Instance().GetClearTargetBuffer() )
		bufferClearMask = D3DCLEAR_TARGET;

	// Recored the max and min z distances
	maximumZDist = CSettings::Instance().GetMaxZdist();
	minimumZDist = CSettings::Instance().GetMinZdist();
	viewAngle = D3DXToRadian( CSettings::Instance().GetViewAngle() );

	// Calc the aspect ratio
	float aspectRatio = CSettings::Instance().GetSize().w / 
						CSettings::Instance().GetSize().h;

	// Calc the square percentage
    squarePercentage = CSettings::Instance().GetSize().h /
					   CSettings::Instance().GetSize().w;

	// calc the y ratio to the frustrum
    frustrumYRatio = squarePercentage / aspectRatio;

    // Create the DirectX 9 instance
    spDXInstance.reset( Direct3DCreate9( D3D_SDK_VERSION ) );

    if( spDXInstance.isNull() )
		throw NExcept::CCriticalException("DirectX Init Error",
			boost::str( boost::format("Error creating an instance of DirectX9.\n\n%s\nLine: %s") % __FUNCTION__ % __LINE__ ));

    // Get the caps to see if we can do hardware vertex processing
    if( FAILED( spDXInstance->GetDeviceCaps( D3DADAPTER_DEFAULT,
                                             D3DDEVTYPE_HAL,
                                             &d3dCaps ) ) )
		throw NExcept::CCriticalException("DirectX Init Error",
			boost::str( boost::format("Error getting device capabilities of video card.\n\n%s\nLine: %s") % __FUNCTION__ % __LINE__ ));

    // Get info about the video card
    if( FAILED( spDXInstance->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &dxdm ) ) )
		throw NExcept::CCriticalException("DirectX Init Error",
			boost::str( boost::format("Error getting adapter display mode of video card.\n\n%s\nLine: %s") % __FUNCTION__ % __LINE__ ));

    // Check for the best z buffer format
    do
    {
        selectedDepthFormat = depthFormat[counter];

        hresult = spDXInstance->CheckDeviceFormat( D3DADAPTER_DEFAULT,
                                                   D3DDEVTYPE_HAL,
                                                   dxdm.Format,
                                                   D3DUSAGE_DEPTHSTENCIL,
                                                   D3DRTYPE_SURFACE,
                                                   depthFormat[counter++] );
    }
    while( hresult != D3D_OK && depthFormat[counter] );

    // Tell them we couldn't secure a depth buffer
    if( hresult != D3D_OK )
		throw NExcept::CCriticalException("DirectX Init Error",
			boost::str( boost::format("Video card does not support depth buffering.\n\n%s\nLine: %s") % __FUNCTION__ % __LINE__ ));

    // clear out the structure
    memset( &dxpp, 0, sizeof(dxpp) );

	// Full screen or windowed mode
    dxpp.Windowed = !CSettings::Instance().GetFullScreen();

	// Fill out the remaining items of the structure
    dxpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
    dxpp.BackBufferFormat       = dxdm.Format;
    dxpp.BackBufferWidth        = (int)CSettings::Instance().GetSize().w;
    dxpp.BackBufferHeight       = (int)CSettings::Instance().GetSize().h;
	dxpp.hDeviceWindow          = hWnd;
	dxpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;

	if( CSettings::Instance().GetTripleBuffering() )
		dxpp.BackBufferCount = 2;

	if( CSettings::Instance().GetVSync() )
		dxpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;

	// Do we need the depth stencil? Z buffer and stencil are both created at the same time in hardware
	if( CSettings::Instance().GetCreateDepthStencilBuffer() )
	{
		dxpp.EnableAutoDepthStencil = true;
		dxpp.AutoDepthStencilFormat = selectedDepthFormat;

		// check for stencil buffer support
		if( spDXInstance->CheckDepthStencilMatch( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, dxpp.BackBufferFormat, dxpp.BackBufferFormat, D3DFMT_D24FS8 ) != D3D_OK )
		{
			if( spDXInstance->CheckDepthStencilMatch( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, dxpp.BackBufferFormat, dxpp.BackBufferFormat, D3DFMT_D24S8 ) != D3D_OK )
			{
				if( spDXInstance->CheckDepthStencilMatch( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, dxpp.BackBufferFormat, dxpp.BackBufferFormat, D3DFMT_D24X4S4 ) != D3D_OK )
				{
					if( spDXInstance->CheckDepthStencilMatch( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, dxpp.BackBufferFormat, dxpp.BackBufferFormat, D3DFMT_D24FS8 ) != D3D_OK )
					{
						throw NExcept::CCriticalException("DirectX Init Error",
							boost::str( boost::format("Video card does not support hardware stencil buffer.\n\n%s\nLine: %s") % __FUNCTION__ % __LINE__ ));
					}
					else // support for a d15s1 buffer
					{
						dxpp.AutoDepthStencilFormat = D3DFMT_D15S1;
					}
				}
				else // support for a d24x4s4 buffer
				{
					dxpp.AutoDepthStencilFormat = D3DFMT_D24X4S4;
				}
			}
			else // support for a d24s8 buffer
			{
				dxpp.AutoDepthStencilFormat = D3DFMT_D24S8;
			}
		}
		else //support for a d24fs8 buffer
		{
			dxpp.AutoDepthStencilFormat = D3DFMT_D24FS8;
		}
	}

	DWORD dwBehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

    if( d3dCaps.VertexProcessingCaps != 0 )    
        dwBehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;

    // Create the device
	LPDIRECT3DDEVICE9 pDXDev;
    if( FAILED( hresult = spDXInstance->CreateDevice( D3DADAPTER_DEFAULT,
                                         D3DDEVTYPE_HAL,
                                         hWnd,
                                         dwBehaviorFlags | D3DCREATE_MULTITHREADED,
                                         &dxpp, 
                                         &pDXDev ) ) )
	{
		DisplayError( hresult );
	}

	spDXDevice.reset(pDXDev);

	CalcProjMatrix( CSettings::Instance().GetSize(),
						CSettings::Instance().GetDefaultSize() );
	
    // Buffers and render states that need to be reset/recreated during a device rest
    ResetXDevice();

}   // CreateXDevice
To enable/disable the frame around the windows, you should set the hbrBackground value in your windowclass object. I.e set it to (HBRUSH)COLOR_WINDOW to disable it

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Thanks but unfortunately, that didn't solve my problem.

Here's my window registration function.


void CGameWnd::RegisterWnd( HINSTANCE hInstance,
			winCallbackFuncPtrType pWinCallback )
{
    WNDCLASSEX w;

    w.cbSize = sizeof( WNDCLASSEX );
    w.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC|CS_BYTEALIGNCLIENT;
    w.lpfnWndProc = pWinCallback;
    w.cbClsExtra = 0;
    w.cbWndExtra = 0;
    w.hInstance = hInstance;
    w.hIcon = LoadIcon( hInstance, "AppIcon" );
    w.hIconSm = NULL;
    w.hCursor = LoadCursor( NULL, IDC_ARROW );
    w.hbrBackground = NULL;
    w.lpszMenuName = NULL;
	w.lpszClassName = CSettings::Instance().GetGameWndClassName().c_str();

    if( RegisterClassEx(&w) == 0 )
		throw NExcept::CCriticalException("Register Class Window", "There was an error registering the class window.");

}   // Starter_RegisterWnd 

That's too bad.
I don't know if setting it to NULL achieves any of the 2 goals you try to achieve.

What I do for windowed is not set it all (and that works) and when I'm in fullscreen I use (HBRUSH)COLOR_WINDOW. Maybe setting it to NULL is different then the default value

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

I was able to come up with a solution.

My solution is not to use "SetWindowLong" because once you set a style that removes the window frame for full screen, you can't get the frame back by reapplying the style when switching back to windowed mode.

So my solution is to create a window with the desired window frame and when in full screen mode, eat the WM_NCPAINT message. This keeps the windows caption from flickering while in full screen mode.

It's not an elegant solution...

If you change the frame with SetWindowLong you have to call SetWindowPos with SWP_FRAMECHANGED afterwards, so the changes get properly applied.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

If you change the frame with SetWindowLong you have to call SetWindowPos with SWP_FRAMECHANGED afterwards, so the changes get properly applied.

You would think. I tried that along with many other tests to no avail.

It works fine using SetWindowLong and SetWindowPos when going from window mode to full screen mode. It doesn't work when going from full screen mode back to window mode.

Are you setting the new window style *after* resetting the device? All you need to do after resetting the device is call SetWindowLong() to set the new window style, and then SetWindowPos() with the SWP_FRAMECHANGED flag to flush the changes.

This topic is closed to new replies.

Advertisement