我需要获取外接显示器的dpi,但是我总是可以获取主显示器的dpi。我也做了清单设置。我使用以下代码来获取它。
定义
public static void GetPerMonitorDPIEx(ref double dx, ref double dy, int x,
int y)
{
UInt32 dpiX = 0;
UInt32 dpiY = 0;
System.Drawing.Point pt = new System.Drawing.Point(x, y);
bool m = SetProcessDpiAwarenessContext((int)PROCESS_DPI_AWARENESS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
var mhandle = MonitorFromPoint( pt, Utils.DEFAULT_TO_NEAREST);
uint m5 = GetDpiForMonitor(mhandle, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out dpiX, out dpiY);
dx = (double)dpiX / 96;
dy = (double)dpiY / 96;
}
通话
GetPerMonitorDPIEx(ref dpiX, ref dpiY, System.Windows.Forms.Screen.AllScreens[i].Bounds.Left + 1, System.Windows.Forms.Screen.AllScreens[i].Bounds.Top +1);
我可以看到两个屏幕的显示器手柄都正确,因为dpi值始终来自主显示器。
首先将您的应用程序设置为dpi感知,然后尝试检查输出
uint m5 = GetDpiForMonitor(mhandle,MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI,out dpiX,out dpiY);