Sign in to follow this  
A_B

win32 reset window

Recommended Posts

A_B    122
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
Black Knight    769
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
A_B    122
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
Black Knight    769
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this