如何在 Windows 10 处检测辅助显示器的本机屏幕分辨率或比例因子
How to detect native screen resolution or scaling factor of the secondary monitor at Windows 10
有两个监视器:次要的在主要的左边。决议:
Primary: 2560*1440 scaling 100%
Secondary: 1920*1200 scaling 150%
在程序启动时它会 EnumDisplayMonitors
给出以下 RECT:
Primary: 0,0,2560,1440
Secondary: -1920,0,-640,800
我也试过代码:
int width, height;
RECT rect = { -1920, 0, -640, 800 };
SystemParametersInfoA(SPI_SETWORKAREA, 0, &rect, 0);
width = GetSystemMetrics(SM_CXSCREEN);
height = GetSystemMetrics(SM_CYSCREEN);
但 width
和 height
始终具有主显示器的尺寸。
如何检测辅助显示器的原始分辨率 1920*1200
或比例因子 150%
?
这也不起作用,给出 1280*800
:
BOOL CALLBACK EnumMonitorCallback(HMONITOR hMon, HDC hdc, LPRECT rect, LPARAM param)
{
MONITORINFOEXA mi;
mi.cbSize = sizeof(mi);
GetMonitorInfoA(hMon, &mi);
HDC dc = CreateDCA("DISPLAY", mi.szDevice, NULL, NULL);
int width = GetDeviceCaps(dc, HORZRES);
int height = GetDeviceCaps(dc, VERTRES);
DeleteDC(dc);
return TRUE;
}
GetSystemMetrics
calls for SM_CXSCREEN
or SM_CYSCREEN
will return the resolution of the primary display. To get the resolution for secondary monitors, you need to use GetDeviceCaps
or the Multiple Display Monitors API.
您的应用程序应该indicate High DPI awareness so that OS does not try to lie about resolutions trying to mimic legacy environment. Then DXGI output enumeration gets you请求的数据。
您可以使用 this blog post 底部的工具快速检查。我有两台 3840x2160 的显示器,第一台缩放比例为 175%,第二台缩放比例为 150%。请注意下面打印输出中的 "desktop coordinates" 和 "monitor DPI":
#### Output: \.\DISPLAY4
* Desktop Coordinates: (0, 0) - (3840, 2160); 3840 x 2160
* Attached To Desktop: 1
* Rotation: DXGI_MODE_ROTATION_IDENTITY
* Monitor: 0x000100B3
* Physical Monitors: LG ULTRA HD(DisplayPort) (0x00000000)
* Bits Per Color: 10
* Color Space: DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709
* Primaries, White Point: R { 0.652, 0.335 }, G { 0.305, 0.637 }, B { 0.148, 0.062 }; { 0.313, 0.329 }
* Luminance: Min 0.500, Max 270.000, Max Full Frame 270.000
* Hardware Composition Support: DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAG_FULLSCREEN | DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAG_CURSOR_STRETCHED
* Monitor DPI, MDT_EFFECTIVE_DPI: 168, 168 ; System DPI 168
* Monitor DPI, MDT_ANGULAR_DPI: 161, 160
* Monitor DPI, MDT_RAW_DPI: 162, 161
…
#### Output: \.\DISPLAY5
* Desktop Coordinates: (3840, 0) - (7680, 2160); 3840 x 2160
* Attached To Desktop: 1
* Rotation: DXGI_MODE_ROTATION_IDENTITY
* Monitor: 0x000200B1
* Physical Monitors: LG ULTRA HD(DisplayPort) (0x00000000)
* Bits Per Color: 10
* Color Space: DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709
* Primaries, White Point: R { 0.652, 0.335 }, G { 0.305, 0.637 }, B { 0.148, 0.062 }; { 0.313, 0.329 }
* Luminance: Min 0.500, Max 270.000, Max Full Frame 270.000
* Hardware Composition Support: DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAG_FULLSCREEN | DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAG_CURSOR_STRETCHED
* Monitor DPI, MDT_EFFECTIVE_DPI: 144, 144 ; System DPI 168
* Monitor DPI, MDT_ANGULAR_DPI: 161, 160
* Monitor DPI, MDT_RAW_DPI: 162, 161
使用GetScaleFactorForMonitor函数获取比例因子。
有两个监视器:次要的在主要的左边。决议:
Primary: 2560*1440 scaling 100%
Secondary: 1920*1200 scaling 150%
在程序启动时它会 EnumDisplayMonitors
给出以下 RECT:
Primary: 0,0,2560,1440
Secondary: -1920,0,-640,800
我也试过代码:
int width, height;
RECT rect = { -1920, 0, -640, 800 };
SystemParametersInfoA(SPI_SETWORKAREA, 0, &rect, 0);
width = GetSystemMetrics(SM_CXSCREEN);
height = GetSystemMetrics(SM_CYSCREEN);
但 width
和 height
始终具有主显示器的尺寸。
如何检测辅助显示器的原始分辨率 1920*1200
或比例因子 150%
?
这也不起作用,给出 1280*800
:
BOOL CALLBACK EnumMonitorCallback(HMONITOR hMon, HDC hdc, LPRECT rect, LPARAM param)
{
MONITORINFOEXA mi;
mi.cbSize = sizeof(mi);
GetMonitorInfoA(hMon, &mi);
HDC dc = CreateDCA("DISPLAY", mi.szDevice, NULL, NULL);
int width = GetDeviceCaps(dc, HORZRES);
int height = GetDeviceCaps(dc, VERTRES);
DeleteDC(dc);
return TRUE;
}
GetSystemMetrics
calls for SM_CXSCREEN
or SM_CYSCREEN
will return the resolution of the primary display. To get the resolution for secondary monitors, you need to use GetDeviceCaps
or the Multiple Display Monitors API.
您的应用程序应该indicate High DPI awareness so that OS does not try to lie about resolutions trying to mimic legacy environment. Then DXGI output enumeration gets you请求的数据。
您可以使用 this blog post 底部的工具快速检查。我有两台 3840x2160 的显示器,第一台缩放比例为 175%,第二台缩放比例为 150%。请注意下面打印输出中的 "desktop coordinates" 和 "monitor DPI":
#### Output: \.\DISPLAY4
* Desktop Coordinates: (0, 0) - (3840, 2160); 3840 x 2160
* Attached To Desktop: 1
* Rotation: DXGI_MODE_ROTATION_IDENTITY
* Monitor: 0x000100B3
* Physical Monitors: LG ULTRA HD(DisplayPort) (0x00000000)
* Bits Per Color: 10
* Color Space: DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709
* Primaries, White Point: R { 0.652, 0.335 }, G { 0.305, 0.637 }, B { 0.148, 0.062 }; { 0.313, 0.329 }
* Luminance: Min 0.500, Max 270.000, Max Full Frame 270.000
* Hardware Composition Support: DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAG_FULLSCREEN | DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAG_CURSOR_STRETCHED
* Monitor DPI, MDT_EFFECTIVE_DPI: 168, 168 ; System DPI 168
* Monitor DPI, MDT_ANGULAR_DPI: 161, 160
* Monitor DPI, MDT_RAW_DPI: 162, 161
…
#### Output: \.\DISPLAY5
* Desktop Coordinates: (3840, 0) - (7680, 2160); 3840 x 2160
* Attached To Desktop: 1
* Rotation: DXGI_MODE_ROTATION_IDENTITY
* Monitor: 0x000200B1
* Physical Monitors: LG ULTRA HD(DisplayPort) (0x00000000)
* Bits Per Color: 10
* Color Space: DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709
* Primaries, White Point: R { 0.652, 0.335 }, G { 0.305, 0.637 }, B { 0.148, 0.062 }; { 0.313, 0.329 }
* Luminance: Min 0.500, Max 270.000, Max Full Frame 270.000
* Hardware Composition Support: DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAG_FULLSCREEN | DXGI_HARDWARE_COMPOSITION_SUPPORT_FLAG_CURSOR_STRETCHED
* Monitor DPI, MDT_EFFECTIVE_DPI: 144, 144 ; System DPI 168
* Monitor DPI, MDT_ANGULAR_DPI: 161, 160
* Monitor DPI, MDT_RAW_DPI: 162, 161
使用GetScaleFactorForMonitor函数获取比例因子。