【发布时间】: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