9

I am writing a small program to let me switch my resolution back and forth because my projector cannot handle the same resolution as my screen. I already know how to set the screen resolution using the windows API. As well as read the current resolution using the windows API or the QT4 toolkit. My problem is I want a menu of all of the different resolutions supported by the screen and graphics card. This program will be distributed so I need the program to actually communicate to the graphics card to find out what it supports. The only API I want to use is the windows API, or the QT4 toolkit, but I don't think QT4 does that unless you are using the graphics widgets in odd ways.

I am pretty sure this is possible with the WINDOWS API. I just don't know how to do it.

Oh and please cut me some slack, I am familiar with QT4 and C++ but I am typically a Linux programmer, I am writing this for someone else. The only thing I have ever done with the windows API is make a message box, set the background, and used system variables. So please explain the process simply. Please don't just post a link to the msdn, I hate their documentation, and I hate Microsoft. I use windows maybe twice a year.

The Dude
  • 310
  • 7
  • 15
  • Any reason you're restricted yourself to the Win32 API? The DirectX API encapsulates this functionality so that you don't have to deal with different graphics-card vendors. – Moo-Juice Nov 03 '10 at 16:17
  • 7
    "Please don't just post a link to the msdn, I hate their documentation, and I hate Microsoft." Really!? MSDN documentation is generally fantastic. – James Nov 03 '10 at 17:06

2 Answers2

18

The following should probably work for you in the general case

DEVMODE dm = { 0 };
dm.dmSize = sizeof(dm);
for( int iModeNum = 0; EnumDisplaySettings( NULL, iModeNum, &dm ) != 0; iModeNum++ ) {
  cout << "Mode #" << iModeNum << " = " << dm.dmPelsWidth << "x" << dm.dmPelsHeight << endl;
  }

This should print out all the supported resolutions on the current display that the .exe is running on. Assuming you're not dealing with a multi-display graphics card this should work. Otherwise you'd have to use EnumDisplayDevices loop over each display.

Once you figure out what resolution you want you can use 'ChangeDisplaySettingsEx' to change the display to the mode you want.

Using DirectX is possible but I wouldn't recommend it as the code is alot more complicated (having to initialize DirectX and using COM pointers) unless you plan to actually use DirectX for more than just determining display resolutions.

MerickOWA
  • 7,453
  • 1
  • 35
  • 56
  • this is exactly what I wanted, I didnt understand that each mode number was each support res, and that 0 was current, thank you – The Dude Nov 03 '10 at 16:42
  • 1
    isnt it interesting that I can look at some example code and understand it better than reading the info off the msdn – The Dude Nov 03 '10 at 17:13
  • @TheDude Everyone has a different learning style. Yours and mine seems to be 'learn by example'. – deed02392 Apr 01 '14 at 14:26
  • I also used this code example, but for some reason i get the same resolution multiple times. For example : mode0, mode1...modemode17 are all showing me 320x200, when another issue is that if I enter the "resolution bar" , the "lowest" my computer allows is 800x600 – user1386966 Jul 23 '14 at 14:01
2

EnumDisplaySettings :)

From MSDN:

"To obtain the current display settings, pass the ENUM_CURRENT_SETTINGS constant in the iModeNum parameter to the EnumDisplaySettings API, as illustrated by the following C++ code."

DEVMODE dm;
// initialize the DEVMODE structure
ZeroMemory(&dm, sizeof(dm));
dm.dmSize = sizeof(dm);
if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
{
// inspect the DEVMODE structure to obtain details
// about the display settings such as
//  - Orientation
//  - Width and Height
//  - Frequency
//  - etc.
}
YWE
  • 2,849
  • 28
  • 42
  • 2
    It's funny that the only word in your answer is a link to MSDN, which the questioner explicitly stated he's not interested in... – Frédéric Hamidi Nov 03 '10 at 16:17
  • @Frédéric Hamidi. I added an example. – YWE Nov 03 '10 at 16:26
  • I don't see where "The Dude" said that at all. He said the "Windows API", and the link points to a function in GDI, which is certianly a part of the Windows API. This seems to me to be exactly what was asked for. – T.E.D. Nov 03 '10 at 16:26
  • I already know how to get the current screen info. I want to get all of the supported resolutions. I didn't think of directx, can you post an example or link please – The Dude Nov 03 '10 at 16:29
  • @T.E.D., questioner says `Please don't just post a link to the msdn, I hate their documentation, and I hate Microsoft`, which I think is clear enough :) – Frédéric Hamidi Nov 03 '10 at 16:31
  • Ah, I see it now. Wonder how I missed that. Must have been reading Slashdot for so long I skip right over anti-MS rants. :-) – T.E.D. Nov 03 '10 at 18:00