【问题标题】:Direct2D Desktop DPI mulitple monitorsDirect2D 桌面 DPI 多显示器
【发布时间】:2022-01-10 05:36:17
【问题描述】:

当我将窗口拖动到不同的监视器时,我正在尝试重新创建一些 Direct2D 图形。现在我正在尝试调整字体大小。当承载 DirectX 图形的容器调整大小时,将调用此代码。

        public override FontResource CreateFont(Font drawingFont)
        {
            if (drawingFont == null)
                throw new ArgumentNullException(nameof(drawingFont));
            try
            {
                _d2dFactory.ReloadSystemMetrics(); 
                float dpi = _d2dFactory.DesktopDpi.Height; //assume that DPI is same for height and width
                var newFont = new FontResouce11(this, _writeFactory, drawingFont.FontFamily.Name, drawingFont.SizeInPoints, drawingFont.Height, drawingFont.Bold, dpi);
                Fonts.Add(newFont);
                return newFont;
            }
            catch (SharpDXException ex)
            {
                ExceptionWrapper.Wrap(ex);
                throw;
            } // Rethrow if the wrapper doesnt throw.
        }
        internal FontResouce11(DeviceEngine11 d3dEngine, SharpDX.DirectWrite.Factory factory, string familyName, float fontSize, int fontHeight, bool isBold, float deviceDpi)
        {
            if (factory == null)
                throw new ArgumentNullException(nameof(factory));
            if (familyName == null)
                throw new ArgumentNullException(nameof(familyName));
            _writeFactory = factory;
            _d3dEngine = d3dEngine;
            FontWeight weight;
            if (isBold)
            {
                weight = FontWeight.Bold;
            }
            else
            {
                weight = FontWeight.Normal;
            }

            float fontSizeDips = fontSize / 72.0F * deviceDpi;

            // Create a format using the font size and weight.
            _format = new TextFormat(_writeFactory, familyName, weight, SharpDX.DirectWrite.FontStyle.Normal, fontSizeDips);
            _height = fontHeight;
        }

如您所见,我从 D2D 工厂获取 DesktopDPI,如此处所述 https://docs.microsoft.com/en-us/windows/win32/directwrite/how-to-ensure-that-your-application-displays-properly-on-high-dpi-displays#step-2-declare-that-the-application-is-dpi-aware 并传递它以正确调整字体大小。

但问题是,即使在调用ReloadSystemMetrics() 之后,每个显示器的 DPI 也不会改变。

它继续使用启动应用程序的显示器的 DPI。

我想获取当前显示器的 DPI。

【问题讨论】:

    标签: .net directx directx-11 direct2d sharpdx


    【解决方案1】:

    在 Windows 10 上,有更精细的“DPI awareness”概念。

    见这篇文章:DPI and Device-Independent Pixels。它有这个注释:

    在 Windows 10 版本 1903 上,ID2D1Factory::GetDesktopDpi 已弃用 建议是 DisplayInformation::LogicalDpi for Windows 商店应用程序或GetDpiForWindow 用于桌面应用程序。

    虽然不推荐,但是 可以使用以编程方式设置默认 DPI 感知 SetProcessDpiAwarenessContext。一旦一个窗口(一个 HWND)已经 在您的流程中创建,不再更改 DPI 感知模式 支持的。如果要设置进程默认 DPI 感知模式 以编程方式,您必须在任何 HWND 之前调用相应的 API 已创建。

    【讨论】:

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