Zen1000
Zen1000

Reputation: 157

Is there a way to get the native resolution in c++ & winapi?

Is there a way to get the recommended resolution in c++ & winapi?

I get only the current resolution by using GetMonitorInfo, EnumDisplaySettings, EnumDisplayMonitors or GetSystemMetrics.

Edit: After some research i found this example code Example code for displaying an app on a portrait device.

Even if it is not about the native resolution it shows how to get it. For that have a look at the functions HRESULT GetPathInfo(_In_ PCWSTR pszDeviceName, _Out_ DISPLAYCONFIG_PATH_INFO* pPathInfo) and bool IsNativeOrientationPortrait(const LUID AdapterLuid, const UINT32 TargetId).

The code works so far but I do not know how it behaves under windows 8.1 and previous versions. Have not made any further tests.

Upvotes: 4

Views: 1748

Answers (1)

Strive Sun
Strive Sun

Reputation: 6289

Actually, you can do a GetSupportedFullScreenResolutions to get the list of resolutions the monitor support and usually the last index is the best(highest) resolution for the monitor.

Like this:

#include <Windows.h>
#include <iostream>
using namespace std;

int main()
{
    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;
    }

    return 0;
}

EDIT:

You can get the resolution of the graphics card and compare the maximum resolution obtained with the screen resolution. In general, the smallest of the two is the best resolution.

#include <Windows.h>
#include <iostream>
using namespace std;

int main()
{
    UINT32 PathArraySize = 0;
    UINT32 ModeArraySize = 0;
    DISPLAYCONFIG_PATH_INFO* PathArray;
    DISPLAYCONFIG_MODE_INFO* ModeArray;
    DISPLAYCONFIG_TOPOLOGY_ID CurrentTopology;
    //Directly query the resolution of the graphics card and get the physical resolution all the time.
    GetDisplayConfigBufferSizes(QDC_ALL_PATHS, &PathArraySize, &ModeArraySize);
    PathArray = (DISPLAYCONFIG_PATH_INFO*)malloc(PathArraySize * sizeof(DISPLAYCONFIG_PATH_INFO));
    memset(PathArray, 0, PathArraySize * sizeof(DISPLAYCONFIG_PATH_INFO));
    ModeArray = (DISPLAYCONFIG_MODE_INFO*)malloc(ModeArraySize * sizeof(DISPLAYCONFIG_MODE_INFO));
    memset(ModeArray, 0, ModeArraySize * sizeof(DISPLAYCONFIG_MODE_INFO));
    LONG ret = QueryDisplayConfig(QDC_DATABASE_CURRENT, &PathArraySize, PathArray, &ModeArraySize, ModeArray, &CurrentTopology);

    int x_DisplayConfigScreen = ModeArray->targetMode.targetVideoSignalInfo.activeSize.cx;
    int y_DisplayConfigScreen = ModeArray->targetMode.targetVideoSignalInfo.activeSize.cy;

    return 0;
}

Upvotes: 3

Related Questions