【问题标题】:Always getting primary monitor DPI始终获取主显示器 DPI
【发布时间】:2018-06-12 11:28:35
【问题描述】:

我需要获取外接显示器的 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 值总是来自主显示器。

【问题讨论】:

  • 请通过屏幕显示您的循环。
  • foreach(Screen.AllScreens 中的变量 i)
  • 如果这是你的循环,AllScreens[i] 将无法工作/编译,因为AllScreens 的索引是int
  • 实际上,我检查了每台显示器的 left 和 top 的值,它们是正确的。
  • 是的,我确实将其转换为 int。

标签: c# .net dpi multiple-monitors


【解决方案1】:

在研究同一主题时,我偶然发现了您的问题: 根据微软文档:

GetDpiForMonitor 不支持 DPI,如果 调用线程是每个监视器的 DPI 感知。对于 DPI 感知版本 这个API,见GetDpiForWindow

来源:docs.microsoft.com

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多