ChangeDisplaySettings Problems

Started by
3 comments, last by nraiyani 18 years, 2 months ago
I was reading the article on wrapping windows (http://www.gamedev.net/reference/articles/article1810.asp ), and thought that might be a good exercise to try. And i sort of got it working, till the point where i wanted the wrapper to change resolutions. Code Follows, I would appreciate if someone can point out the mistake i am making here. becuase i've tried that code before with out using class and namespace, and it should work but it doesn't. So i must be making a mistake somewhere.

#include <windows.h>
namespace Core
{
	class clsTest
	{
	protected:
		DEVMODE dmDispSet;
		DEVMODE dmOriDispSet;

		bool ChangeResolution (int w, int h, int b)
		{
			EnumDisplaySettings( NULL, ENUM_CURRENT_SETTINGS, &dmOriDispSet );
			
			SecureZeroMemory(&dmDispSet, sizeof(DEVMODE));
			dmDispSet.dmPelsHeight = h;
			dmDispSet.dmPelsWidth = w;
			dmDispSet.dmBitsPerPel = b;
			dmDispSet.dmDisplayFrequency = dmOriDispSet.dmDisplayFrequency;
			dmDispSet.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;

			if (ChangeDisplaySettings(&dmDispSet, CDS_TEST) == DISP_CHANGE_SUCCESSFUL)
			{
				ChangeDisplaySettings(&dmDispSet, 0);
				return true;
			}
			return false;
		};

		void ResetResolution()
		{
			ChangeDisplaySettings( &dmOriDispSet, 0 );
		};
	public:
		void DoRes()
		{
			ChangeResolution(800, 600, 32);
		};

		void UndoRes()
		{
			ResetResolution();
		};
	};
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
using namespace Core;


int WINAPI WinMain(HINSTANCE hInstance, 
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, 
				   int nShowCmd)
{
	MSG uMsg;
	HWND g_hWnd;
	WNDCLASSEX winClass;

	clsTest test;

	memset(&uMsg, 0, sizeof(uMsg));

	winClass.lpszClassName = "Framework";
	winClass.cbSize = sizeof(WNDCLASSEX);
	winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	winClass.lpfnWndProc = WindowProc;
	winClass.hInstance = hInstance;
	winClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	winClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	winClass.lpszMenuName = NULL;
	winClass.cbClsExtra = 0;
	winClass.cbWndExtra = 0;

	if (!RegisterClassEx(&winClass))
		return E_FAIL;

	test.DoRes();

	g_hWnd = CreateWindowEx(NULL,
							"Framework", 
							"Framework Res change test", 
							WS_POPUP | WS_VISIBLE, 
							0, 0, 800, 600,
							NULL, NULL, hInstance, 
							NULL);

	if (g_hWnd == NULL)
		return E_FAIL;


	ShowWindow (g_hWnd, nShowCmd);
	UpdateWindow (g_hWnd);

	while (uMsg.message != WM_QUIT)
	{
		if (PeekMessage(&uMsg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&uMsg);
			DispatchMessage(&uMsg);
		}

	}

	test.UndoRes();
	//ChangeResolution(1280, 1024, 32);

	return 0;
}


Oh, just in case its important, I am using Visual C++ Express with Platform SDK
42
Advertisement
So the resolution isnt changing? It might be that ChangeDisplaySettings() is failing. You're not checking return values to notice.
Actually i did check the return value and it always returns DISP_CHANGE_SUCCESSFUL
but it doesn't change the Resolution.

I am thinking it might because of the Dualview i have set up. and for some reason it doesn't like it. although it should return DISP_CHANGE_BADDUALVIEW in which case, and it doesn't do that either.


I tried it with Dual View disabled, and it still doesn't work.
42
Your code lacks of this (very important) line:

dmDispSet.dmSize = sizeof (dmDispSet) ;
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
Wow, i can't believe i didn't see that.

Thanks
42

This topic is closed to new replies.

Advertisement