Sign in to follow this  
mateo

How would I change the Desktop Resolution?

Recommended Posts

mateo    100
Does anybody know how to change the Desktop resolution from within a program? I need to change it from 32 bits of color to 16 bits of color (or even 8 bits of color).

Share this post


Link to post
Share on other sites
Xyphyx    142
You could utilize the DEVMODE structure (WIN).
DEVMODE dm;
ZeroMemory(&dm, sizeof(DEVMODE));
dm.dmSize =sizeof(dm);
dm.dmPelsWidth = WindowWidth;
dm.dmPelsHeight = WindowHeight;
dm.dmBitsPerPel = 16; /*<-16 bits per pixel*/
dm.dmFlags = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

if(ChangeDisplaySettings(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
<t>/*Failed to change display mode
}

and to revert the mode after the app has completed...
ChangeDisplaySettings(NULL, 0);

Share this post


Link to post
Share on other sites
Evil Steve    2017
This does beg the question: Why?

If I had an app that changed the desktop settings every time it ran, it'd only get to run once before it was uninstalled.

Share this post


Link to post
Share on other sites
Enselic    829
Quote:
Original post by mateo
Does anybody know how to change the Desktop resolution from within a program? I need to change it from 32 bits of color to 16 bits of color (or even 8 bits of color).

I remember I wanted to know this as well when I began programming, but a much better method is to use a graphics API, like DirectX or OpenGL. It will require some time to get into, but if you start with the very basic applications (for instance from http://www.codesampler.com) you will soon get into it. If you have any specific questions about the tutorials there, feel free to ask here.

If you use a "normal" fullscreen Windows application with changed desktop resolution/bit deepth, you will mess up the layout of the icons on the users desktop for instance, and that is quite annoying.

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by Enselic
If you use a "normal" fullscreen Windows application with changed desktop resolution/bit deepth, you will mess up the layout of the icons on the users desktop for instance, and that is quite annoying.
Not if you do it properly [wink]
My Pong Clone goes into a fullscreen mode, using Windows GDI, and it doesn't mess with the desktop icons when you exit.
Although, I think it might be broken just now. Hrm...

Share this post


Link to post
Share on other sites
mateo    100
Thanks to everyone for your help, But I'm having no luck yet. Forgot to mention that I'm using Windows XP-SP2 and DirectX9. Any other suggestions?

Evil Steve:
I'm writing a directdraw library, and one of the features is directdraw windowed mode. So I need a way to change the desktop display properties so that the graphics are drawn correctly no matter what resolution is used for the directdraw application. And as you have stated, a way to change it back.

Enselic:
I don't think the Icons will get messed up. I've used the Gen's Sega Genesis emulator and am very sure it changes my desktop resolution from 32-bit to 16-bit, and it does it with no side effects when restoring the original 32-bit desktop resolution.

Evil Steve: Exactly!

Share this post


Link to post
Share on other sites
blarn    128
You should probably make your code work around the screen resolution (for at least common resolutions), not change the screen resolution to work around your code. It'd be crazy annoying if a application just changed the resolution on you in windowed mode.

If you were worried about bits per pixel stuff, these links might help (I haven't read em though, they just looked helpful!)

http://www.gamedev.net/reference/articles/article538.asp

http://www.geocities.com/foetsch/locking/locking.htm

Share this post


Link to post
Share on other sites
John_23    200
Well I'm learning OpenGL and I thought this might help, it's from a simple OpenGL example.



// the main windows entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX windowClass; // window class
HWND hwnd; // window handle
MSG msg; // message
bool done; // flag saying when our app is complete
DWORD dwExstyle; // Window Extended style
DWORD dwstyle; // Window style
RECT windowRect;

// temp var's
int width = 800;
int height = 600;
int bits = 32;

//fullScreen = TRUE;

windowRect.left=(long)0; // Set Left Value To 0
windowRect.right=(long)width; // Set Right Value To Requested Width
windowRect.top=(long)0; // Set Top Value To 0
windowRect.bottom=(long)height; // Set Bottom Value To Requested Height

// fill out the window class structure
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // default icon
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); // default arrow
windowClass.hbrBackground = NULL; // don't need background
windowClass.lpszMenuName = NULL; // no menu
windowClass.lpszClassName = "MyClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); // windows logo small icon

// register the windows class
if (!RegisterClassEx(&windowClass))
return 0;

if (fullScreen) // fullscreen?
{
DEVMODE dmScreenSettings; // device mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
dmScreenSettings.dmSize = sizeof(dmScreenSettings);
dmScreenSettings.dmPelsWidth = width; // screen width
dmScreenSettings.dmPelsHeight = height; // screen height
dmScreenSettings.dmBitsPerPel = bits; // bits per pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

//
if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
// setting display mode failed, switch to windowed
MessageBox(NULL, "Display mode failed", NULL, MB_OK);
fullScreen=FALSE;
}
}

if (fullScreen) // Are We Still In Fullscreen Mode?
{
dwExstyle=WS_EX_APPWINDOW; // Window Extended style
dwstyle=WS_POPUP; // Windows style
ShowCursor(FALSE); // Hide Mouse Pointer
}
else
{
dwExstyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended style
dwstyle=WS_OVERLAPPEDWINDOW; // Windows style
}

AdjustWindowRectEx(&windowRect, dwstyle, FALSE, dwExstyle); // Adjust Window To True Requested Size

// class registered, so now create our window
hwnd = CreateWindowEx(NULL, // extended style
"MyClass", // class name
"OpenGL Robot", // app name
dwstyle | WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
0, 0, // x,y coordinate
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top, // width, height
NULL, // handle to parent
NULL, // handle to menu
hInstance, // application instance
NULL); // no extra params

// check if window creation failed (hwnd would equal NULL)
if (!hwnd)
return 0;

ShowWindow(hwnd, SW_SHOW); // display the window
UpdateWindow(hwnd); // update the window

done = false; // intialize the loop condition variable

// main message loop
while (!done)
{
PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);

if (msg.message == WM_QUIT) // do we receive a WM_QUIT message?
{
done = true; // if so, time to quit the application
}
else
{
Render();

TranslateMessage(&msg); // translate and dispatch to event queue
DispatchMessage(&msg);
}
}

if (fullScreen)
{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
}

return msg.wParam;
}




Near the top you should notice the width height and bits variables. Hope this helps :)

John

Share this post


Link to post
Share on other sites
Enselic    829
Quote:
Original post by Evil Steve
Not if you do it properly [wink]
My Pong Clone goes into a fullscreen mode, using Windows GDI, and it doesn't mess with the desktop icons when you exit.
Quote:
Original post by mateo
I don't think the Icons will get messed up. I've used the Gen's Sega Genesis emulator and am very sure it changes my desktop resolution from 32-bit to 16-bit, and it does it with no side effects when restoring the original 32-bit desktop resolution.

Before we go on here, I think I should define 'mess up' :^)

Consider a desktop resolution at 1024x768 with the Recycle Bin at the bottom-right. Now run a game that changes the desktop1 resolution to 800x600. The Recycle bin will be moved to the bottom right of the 800x600 screen to remain reachable. However, when the game resets the resolution to 1024x768, the Recycle Bin remains where it is, i.e. it is moved from where it was before the game was started. That's what I call a 'mess up'.

I haven't experienced the problem in a while though, so it might be fixed, but I doubt it has, since when I manually set the res to 800x600, and then press 'No' in the 'Do you see this?'-dialog causing it to reset to 1024x768, it messes up the icons.

1mateo: the Gen's Sega Genesis emulator probably doesn't change the desktop resolution when it is in fullscreen, it probably rather controls the GPU directly by using some graphics API.

Share this post


Link to post
Share on other sites
_0D_0A    130
Quote:
I haven't experienced the problem in a while though, so it might be fixed, but I doubt it has, since when I manually set the res to 800x600, and then press 'No' in the 'Do you see this?'-dialog causing it to reset to 1024x768, it messes up the icons.


AFAIK (I'm not certain though) no erroneuos placing of the desktop icons will occur if they are not drawn while the lower resolution is set. I might be totally wrong, but I think that is the usual behaviour nowadays.

Share this post


Link to post
Share on other sites
mateo    100
All right guys, thanks! I finally got it working. I was doing this:
dmScreenSettings.dmDisplaySettings=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

instead of this:
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

Enselic: Let me correct my self here, you may be right after all, I think they still do get messed up if the resolution changes. The Gens Sega Genesis emulator just changes the bitdepth and not the resolution. Actually, thats what I needed too, just a bitdepth change, to prevent the icon screwups.

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