Jump to content
  • Advertisement
Sign in to follow this  
A_B

win32 reset window

This topic is 3035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to allow a user to change settings of a window in win32 What i did is i have a struct with several settings that can be changed. I first destroy everything with this function:
void CWindow::KillWindow()
{
	if (winvars.windowed == false)
	{
		ChangeDisplaySettings(NULL, 0);
		ShowCursor(true);
	}

	wglMakeCurrent( NULL, NULL );
	wglDeleteContext( hRc );
	ReleaseDC( hWnd, hDc );
	
	DestroyWindow(hWnd);
	hWnd = NULL;
	
	UnregisterClass(L"WindowClass", GetModuleHandle(NULL)); 
}

Right after I rebuild the entire thing with this function:
void CWindow::MakeWindow()
{
	HINSTANCE hInstance = GetModuleHandle(NULL);
	
	//WINDOW CLASS
	WNDCLASSEX wc;
	
	ZeroMemory(&wc, sizeof(WNDCLASSEX));
	
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC)WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.lpszClassName = L"WindowClass";
	
	RegisterClassEx(&wc);
	
	if (winvars.windowed == false)
	{
		//DISPLAY SETTINGS
		DEVMODE DispSettings;
		
		ZeroMemory(&DispSettings, sizeof(DEVMODE));
		DispSettings.dmSize = sizeof(DEVMODE);
		DispSettings.dmPelsWidth = winvars.width;
		DispSettings.dmPelsHeight = winvars.height;
		DispSettings.dmBitsPerPel = winvars.bits;
		DispSettings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
		
		ChangeDisplaySettings(&DispSettings, CDS_FULLSCREEN);
	}
		
	if (winvars.windowed == false)
	{
		winvars.style = WS_POPUP;
		ShowCursor(false);
		winvars.cursor = false;
	}
	else
	{
		winvars.style = WS_OVERLAPPEDWINDOW;
		ShowCursor(true);
		winvars.cursor = true;
	}
	
	RECT rect;
	rect.left = winvars.posX; rect.top = winvars.posY;
	rect.right = winvars.width + winvars.posX; rect.bottom = winvars.height + winvars.posY;
	
	AdjustWindowRectEx(&rect, winvars.style, false, 0);
	
	
	hWnd = CreateWindowEx(	NULL,
					L"WindowClass",
					winvars.title,
					winvars.style,
					winvars.posX, winvars.posY,
					winvars.width, winvars.height,
					NULL,
					NULL,
					hInstance,
					NULL);
	
	//PIXEL FORMAT
	PIXELFORMATDESCRIPTOR pfd;
	
	ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));
	pfd.nSize = sizeof (PIXELFORMATDESCRIPTOR);
	
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
			  PFD_DOUBLEBUFFER;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = winvars.bits;
	pfd.cDepthBits = 16;
	pfd.iLayerType = PFD_MAIN_PLANE;
	
	hDc = GetDC (hWnd);
	int iFormat = ChoosePixelFormat( hDc, &pfd );
	SetPixelFormat( hDc, iFormat, &pfd );
	
	
	
		
	//OPENGL RENDER CONTEXT
	hRc = wglCreateContext(hDc);
	wglMakeCurrent(hDc, hRc);
	
	ShowWindow(hWnd, SW_SHOW);
}

What happens though is that the program just exits. Anyone have an idea how it sould be done? Thanks Alex

Share this post


Link to post
Share on other sites
Advertisement
Instead of destroying/recreating the window you can use SetWindowLongPtr to change the properties of a window.

Your program is closing most likely because DestroyWindow sends a WM_DESTROY message and your WndProc is treating this as a indication to end the application.

Share this post


Link to post
Share on other sites
Thanks Black Knight,
I couldn't really figure out what the last parapeter of SetWindowLongPtr was upposed to be or how to use it is I just changed my winow procedure so that the message to exit the application is WM_CLOSE instead of WM_DESTROY. It works now, But could I ever run into trouble by doing it this way?

Thanks

Share this post


Link to post
Share on other sites
The last parameter to SetWindowLongPtr depends on what the second parameter is.
For example to change a window style you can do :

SetWindowLongPtr(windowHandle,GWL_style,WS_OVERLAPPEDWINDOW);

Also from MSDN:
"Certain window data is cached, so changes you make using SetWindowLongPtr will not take effect until you call the SetWindowPos function."

So you might need to call SetWindowPos to make the changes take effect immediately.

There shouldn't be any problems caused by destroying/recreating the window other than the window will probably disappear and come back again.Doing it with SetWindowLongPtr should be smoother.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!