Skip to content

Instantly share code, notes, and snippets.

@dantio
Last active March 26, 2016 21:55
Show Gist options
  • Save dantio/1088bca80f5a1f449f15 to your computer and use it in GitHub Desktop.
Save dantio/1088bca80f5a1f449f15 to your computer and use it in GitHub Desktop.
void GetDesktopResolution(int& width, int& height, int& posX, int& posY)
{
int index = 0;
DISPLAY_DEVICE dd;
dd.cb = sizeof(DISPLAY_DEVICE);
bool secondDisplay = false;
while (EnumDisplayDevices(NULL, index++, &dd, 0))
{
if (dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) {
DEVMODE mode;
mode.dmSize = sizeof(mode);
EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &mode);
width = mode.dmPelsWidth;
height = mode.dmPelsHeight;
posX = mode.dmPosition.x;
posY = mode.dmPosition.y;
if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) printf("* ");
cout << "Device Name: " << dd.DeviceName << "; Device String: " << dd.DeviceString << endl;
cout << "5: Width: " << width << "; height: " << height << '\n';
}
}
}
void GetDesktopResolution1(int& width, int& height)
{
RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect(hDesktop, &desktop);
width = desktop.right;
height = desktop.bottom;
cout << "1: Width: " << width << "; height: " << height << '\n';
}
void GetDesktopResolution2(int& width, int& height)
{
HMONITOR hMon = MonitorFromPoint({ 0, 0 }, MONITOR_DEFAULTTOPRIMARY);
MONITORINFO monInfo;
monInfo.cbSize = sizeof monInfo;
GetMonitorInfo(hMon, &monInfo);
width = monInfo.rcMonitor.right - monInfo.rcMonitor.left;
height = monInfo.rcMonitor.bottom - monInfo.rcMonitor.top;
cout << "2: Width: " << width << "; height: " << height << '\n';
}
void GetDesktopResolution3(int& width, int& height)
{
width = GetSystemMetrics(SM_CXSCREEN);
height = GetSystemMetrics(SM_CYSCREEN);
cout << "3: Width: " << width << "; height: " << height << '\n';
}
void GetDesktopResolution4(int& width, int& height)
{
RECT rc;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0);
width = rc.right - rc.left;
height = rc.bottom - rc.top;
cout << "4: Width: " << width << "; height: " << height << '\n';
}
// Only function that returns correct resolution for primary monitor
void GetDesktopResolution5(int& width, int& height)
{
DEVMODE mode;
mode.dmSize = sizeof(mode);
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &mode);
width = mode.dmPelsWidth;
height = mode.dmPelsHeight;
cout << "5: Width: " << width << "; height: " << height << '\n';
}
struct MonitorInfo {
unsigned monitor = 0;
unsigned primary = 0;
unsigned index = 0;
unsigned width;
unsigned height;
string displayName;
};
static BOOL CALLBACK
MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
MonitorInfo& info = *(MonitorInfo*) dwData;
MONITORINFOEX mi;
memset(&mi, 0, sizeof(MONITORINFOEX));
mi.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(hMonitor, &mi);
string displayName = (const char*)mi.szDevice;
if (displayName.find(R"(\\.\DISPLAYV)") == 0) return TRUE; //ignore pseudo-monitors
if (mi.dwFlags & MONITORINFOF_PRIMARY) info.primary = info.index;
if (info.monitor == info.index) {
info.width = lprcMonitor->right - lprcMonitor->left;
info.height = lprcMonitor->bottom - lprcMonitor->top;
info.displayName = displayName;
}
info.index++;
return TRUE;
}
void GetDesktopResolution6(unsigned monitor) {
MonitorInfo info;
info.monitor = monitor;
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&info);
cout << "Name:" << info.displayName << "; Width: " << info.width << "; height " << info.height << endl;
}
void ListDisplayDevices()
{
int index = 0;
DISPLAY_DEVICE dd;
dd.cb = sizeof(DISPLAY_DEVICE);
while (EnumDisplayDevices(NULL, index++, &dd, 0))
{
if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) printf("* ");
cout << "Device Name: " << dd.DeviceName << "; Device String: " << dd.DeviceString << endl;
}
}
void ListDisplaySettings(int index)
{
DISPLAY_DEVICE dd;
dd.cb = sizeof(DISPLAY_DEVICE);
if (!EnumDisplayDevices(NULL, index, &dd, 0))
{
cerr << "EnumDisplayDevices failed:" << GetLastError() << endl;
return;
}
DISPLAY_DEVICE monitor;
monitor.cb = sizeof(DISPLAY_DEVICE);
if (!EnumDisplayDevices(dd.DeviceName, index, &monitor, 0))
{
pcerr << "EnumDisplayDevices failed:" << GetLastError() << endl;
return;
}
DEVMODE dm;
dm.dmSize = sizeof(DEVMODE);
if (!EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm))
{
cerr << "EnumDisplaySettings failed" << GetLastError() << endl;
return;
}
cout << "Device name:" << dd.DeviceName << endl;
cout << "Monitor name:" << monitor.DeviceID << endl;
cout << "Refresh rate, in hertz:" << dm.dmDisplayFrequency << endl;
cout << "Color depth: %d\n" << dm.dmBitsPerPel << endl;
cout << "Screen resolution, in pixels:" << dm.dmPelsWidth << " x " << dm.dmPelsHeight << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment