Home > Net >  Identify laptop display vs external monitor programmatically in C
Identify laptop display vs external monitor programmatically in C

Time:05-02

I'm IT for my company and multiple of our user know very little of computers. We have multiple drop-in stations with docks and external monitors. So users experience display issues when moving to a different station. Monitors resize, duplicate or change frequencies.

I made this easy to use one click tool to resize their monitors in a console C exe. The issue I run into is identifying whether the display is a laptop display or an external monitor. This is important because all of our external monitors are 1920 x 1080 but some of our laptops are 1920 x 1200.

main.cpp:

#include <Windows.h>
#include <string>
#include <iostream>
int main()
{

    DEVMODE devmode;
    
    SetDisplayConfig(0, NULL, 0, NULL, SDC_TOPOLOGY_EXTEND | SDC_APPLY);
    

    //long result = ChangeDisplaySettings(&devmode, 0);

    DISPLAY_DEVICE displayDevice;
    displayDevice.cb = sizeof(displayDevice);
    int deviceIndex = 0;
    while (EnumDisplayDevices(0, deviceIndex, &displayDevice, 0))
    {
        std::wstring deviceName = displayDevice.DeviceName;
        int monitorIndex = 0;

        while (EnumDisplayDevices(deviceName.c_str(), monitorIndex, &displayDevice, 0))
        {            
            devmode.dmPelsWidth = 1920;
            devmode.dmPelsHeight = 1080;
            devmode.dmDisplayFrequency = 60;
            devmode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
            devmode.dmSize = sizeof(DEVMODE);

            long result = ChangeDisplaySettingsEx(deviceName.c_str(), &devmode, NULL, NULL, 0);
              monitorIndex;
        }
          deviceIndex;
    }
    return 0;
}

CodePudding user response:

Perhaps you are looking for the DISPLAY_DEVICE_REMOVABLE flag in the displayDevice.StateFlags field?

Value Meaning
DISPLAY_DEVICE_REMOVABLE The device is removable; it cannot be the primary display.

https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-display_devicea

  • Related