Sign in to follow this  
yuval

win32 proper fullscreen

Recommended Posts

yuval    100
Hello, I am trying to write a wrapper to win32. All is going well excepts when going into fullscreen mode. I have two monitors and I can see the mouse moving in my second monitor when I am in fullscreen mode. Also when I press tab then my program loses focus and I can see the taskbar the other windows. I have seen many games that go into fullscreen mode without those problems. My code (mostly from NEHE):
int Init(char* sTitle, unsigned int uiWidth, unsigned int uiHeight,
	 unsigned int uiBPP, bool bFullscreen)
{
	WNDCLASSEX wc;
	HINSTANCE hInstance = (HINSTANCE)GetWindowLong(GetConsoleWindow(), GWL_HINSTANCE);
	DWORD dwExstyle;						// Window Extended style
	DWORD dwstyle;							// Window style

	//Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;	//NULL
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);	//(HBRUSH)(COLOR_WINDOW+1), (HBRUSH)COLOR_WINDOW;
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
	//Setup window style
	if(bFullscreen)	//Fullscreen mode
	{
		ToggleCursor(false);//
		m_bFullscreen = bFullscreen;
		DEVMODE dmScreenSettings;					// Device Mode
		memset(&dmScreenSettings,0,sizeof(dmScreenSettings));		// Makes Sure Memory's Cleared
		dmScreenSettings.dmSize=sizeof(dmScreenSettings);		// Size Of The Devmode Structure
		dmScreenSettings.dmPelsWidth	= uiWidth;			// Selected Screen Width
		dmScreenSettings.dmPelsHeight	= uiHeight;			// Selected Screen Height
		dmScreenSettings.dmBitsPerPel	= uiBPP;			// Selected Bits Per Pixel
		dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
		// Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
		if(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
		{
			MessageBox(NULL, "Fullscreen mode failed", "Error!", MB_ICONEXCLAMATION | MB_OK);
			return 0;
		}
		dwExstyle = WS_EX_APPWINDOW;					// Window Extended style
		dwstyle = WS_POPUP;						// Windows style
	}
	else	//Windowed mode
	{
		dwExstyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;			// Window Extended style
		dwstyle = WS_OVERLAPPEDWINDOW;					// Windows style
	}
	//Adjust Window To True Requested Size
	RECT WindowRect;						// Grabs Rectangle Upper Left / Lower Right Values
	WindowRect.left=(long)0;				// Set Left Value To 0
	WindowRect.right=(long)uiWidth;			// Set Right Value To Requested Width
	WindowRect.top=(long)0;					// Set Top Value To 0
	WindowRect.bottom=(long)uiHeight;		// Set Bottom Value To Requested Height
	AdjustWindowRectEx(&WindowRect, dwstyle, FALSE, dwExstyle);
	//Creating the Window
    if(!(g_hwnd = CreateWindowEx(
		dwExstyle,					// Extended style For The Window (WS_EX_CLIENTEDGE, WS_EX_TRANSPARENT)
		g_szClassName,				// Class Name
		sTitle,	// Window Title
		WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwstyle,	//Window style (WS_OVERLAPPEDWINDOW, WS_POPUPWINDOW, WS_EX_TOPMOST | WS_POPUP)
        0, 0,			//Window top left position (CW_USEDEFAULT, CW_USEDEFAULT)
		uiWidth, uiHeight,		//Window Width, Height ((640, 480), (1920, 1080))
        NULL, NULL, hInstance, NULL)))
	{
		MessageBox(NULL, "Window Creation Error.","ERROR", MB_OK|MB_ICONEXCLAMATION);
		return false;	
	}
    ShowWindow(g_hwnd, SW_SHOWNORMAL);	//0
    UpdateWindow(g_hwnd);
	SetFocus(g_hwnd);
	//The Message Loop
	/*
	MSG Msg;
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
	*/
	return true;
}

[Edited by - yuval on February 9, 2010 3:48:34 PM]

Share this post


Link to post
Share on other sites
Erik Rufelt    5901
Use the WS_EX_TOPMOST style to put the window above the task-bar. In order to keep the mouse from leaving your application, use the ClipCursor Function. You might need to handle this every time your app loses/gains focus, to work properly with alt-tabbing. You probably also want to use the CloseWindow Function when the user alt-tabs, to minimize your window if in fullscreen mode. See the WM_ACTIVATEAPP Notification for detecting this.

Also, when posting large amounts of code, use [source].code.[/source] tags to put it in a text-box. This will keep the tab formatting and syntax highlight to make it easier to read. Like this:

int main() {
return 0;
}

Share this post


Link to post
Share on other sites
yuval    100
Quote:
Original post by Erik Rufelt
Use the WS_EX_TOPMOST style to put the window above the task-bar. In order to keep the mouse from leaving your application, use the ClipCursor Function. You might need to handle this every time your app loses/gains focus, to work properly with alt-tabbing. You probably also want to use the CloseWindow Function when the user alt-tabs, to minimize your window if in fullscreen mode. See the WM_ACTIVATEAPP Notification for detecting this.

Also, when posting large amounts of code, use [source].code.[/source] tags to put it in a text-box. This will keep the tab formatting and syntax highlight to make it easier to read. Like this:
*** Source Snippet Removed ***


It works great now! Thank you.
(I also fixed the code so other people can read it)

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this