C++ Finding out what resolutions are supported by the graphics card

The Dude picture The Dude · Nov 3, 2010 · Viewed 8.3k times · Source

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.

Answer

MerickOWA picture MerickOWA · Nov 3, 2010

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.