始终获得主监视器DPI

时间:2018-06-12 11:28:36

标签: c# .net dpi multiple-monitors

我需要获得外接显示器的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值始终来自主显示屏后,我可以看到显示器的显示器手柄正确。

1 个答案:

答案 0 :(得分:0)

首先将您的应用设置为dpi感知,然后尝试检查输出

uint m5 = GetDpiForMonitor(mhandle,MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI,dpiX,dpiY);