I can't seem to find any function which will allow me to obtain the Windows screen resolution and/or set it in C++.
I would like to be able to change it, then change it back.

Thanx

Dani AI

Generated

If you only need to read the current desktop size, call GetSystemMetrics. For the primary display: SM_CXSCREEN and SM_CYSCREEN. On multi-monitor systems, the virtual desktop bounds are SM_XVIRTUALSCREEN/YVIRTUALSCREEN and SM_CXVIRTUALSCREEN/CYVIRTUALSCREEN. Do not use GetWindowRect(GetDesktopWindow(), ...) to infer resolution; that returns the primary monitor rectangle and ignores additional monitors. (learn.microsoft.com)

To switch resolution and then restore it (what and were aiming for), save the current mode with EnumDisplaySettings, set only the fields you intend to change, test the mode, apply, then restore. Also link against User32.lib; that resolves the LNK2019/2028 errors hit because ChangeDisplaySettings* lives in User32. (learn.microsoft.com)

#include <Windows.h>
#pragma comment(lib, "User32.lib")

// Read current primary display size
int width  = GetSystemMetrics(SM_CXSCREEN);
int height = GetSystemMetrics(SM_CYSCREEN);

// Change to 800x600 and restore
void SwitchTemporarily()
{
    DEVMODE dmOrig = {}; dmOrig.dmSize = sizeof(dmOrig);
    if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dmOrig)) return;

    DEVMODE dmNew = dmOrig;
    dmNew.dmPelsWidth  = 800;
    dmNew.dmPelsHeight = 600;
    dmNew.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL | DM_DISPLAYFREQUENCY;

    if (ChangeDisplaySettings(&dmNew, CDS_TEST) == DISP_CHANGE_SUCCESSFUL)
    {
        ChangeDisplaySettings(&dmNew, 0);     // apply (non-persistent)
        // ... your work here ...
        ChangeDisplaySettings(&dmOrig, 0);    // restore
    }
}

Notes:

  • Use CDS_TEST first; apply only if the driver reports success. Avoid CDS_UPDATEREGISTRY unless you truly want to persist the change. (learn.microsoft.com)
  • To target a specific monitor, enumerate adapters with EnumDisplayDevices and call ChangeDisplaySettingsEx for that device. (learn.microsoft.com)
  • On Windows 8+ you generally cannot set modes below 32 bpp. (learn.microsoft.com)

If you just want system info, WMI (as suggested) works, but the Win32 APIs above are simpler and purpose-built.

Recommended Answers

All 7 Replies

Thanx. Now I just have to figure out how to use it. I will, eventually.

I'm also trying to do the same thing and i'm running on windows xp. i kind of understand the links you gave RFBourquin except i'm pretty new to C++, only using it in a class for the past 6 weeks. I'm having trouble understanding how to use the commands from http://msdn.microsoft.com/en-us/library/dd183413.aspx. i was hoping someone could give an example of using the commands from that page and change the resolution to 800 x 600 as an example please. thank you.

(First, declare the device mode class)
DEVMODE NewDevice;

(The members of this class you will need)
NewDevice.dmSize = sizeof(DEVMODE);
NewDevice.dmPelsWidth = 1024;
NewDevice.dmPelsHeight = 768;


(In the main program)
ChangeDisplaySettings(&NewDevice, 0);

You may want to have a look at WMI (windows management instrumentation). It provides an extensive API for accessing your systems information.

I get the following errors please help

Error 1 error LNK2028: unresolved token (0A0000AD) "extern "C" long __stdcall ChangeDisplaySettingsW(struct _devicemodeW *,unsigned long)" (?ChangeDisplaySettingsW@@$$J18YGJPAU_devicemodeW@@K@Z) referenced in function "private: void __clrcall UI::Form1::About_Click(class System::Object ^,class System::EventArgs ^)" (?About_Click@Form1@UI@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z) C:\Users\7sedam7\documents\visual studio 2010\Projects\UI\UI\UI.obj


Error 2 error LNK2019: unresolved external symbol "extern "C" long __stdcall ChangeDisplaySettingsW(struct _devicemodeW *,unsigned long)" (?ChangeDisplaySettingsW@@$$J18YGJPAU_devicemodeW@@K@Z) referenced in function "private: void __clrcall UI::Form1::About_Click(class System::Object ^,class System::EventArgs ^)" (?About_Click@Form1@UI@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z) C:\Users\7sedam7\documents\visual studio 2010\Projects\UI\UI\UI.obj

Error 1 error LNK2028: unresolved token (0A0000AD)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.