Direct3D Window->Bounds.Width/Height 与实际分辨率不同

Direct3D Window->Bounds.Width/Height differs from real resolution

我在执行 this tutorial 时注意到 Direct3D 有一个奇怪的行为。

我从 Window 对象获得的尺寸与 windows 的配置分辨率不同。这里我设置的是1920*1080,Winows对象的宽高是1371*771.

CoreWindow^ Window = CoreWindow::GetForCurrentThread();
// set the viewport
D3D11_VIEWPORT viewport = { 0 };

viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = Window->Bounds.Width; //should be 1920, actually is 1371
viewport.Height = Window->Bounds.Height;  //should be 1080, actually is 771

我正在 Alienware 14 上开发,也许这会导致这个问题,但我还找不到任何答案。

CoreWindow 大小、指针位置等不是以像素表示。它们以 设备独立像素 (DIPS) 表示。要转换 to/from 像素,您需要使用每英寸点数 (DPI) 值。

inline int ConvertDipsToPixels(float dips) const
{
    return int(dips * m_DPI / 96.f + 0.5f);
}

inline float ConvertPixelsToDips(int pixels) const
{
    return (float(pixels) * 96.f / m_DPI);
}

m_DPI comes from DisplayInformation::GetForCurrentView()->LogicalDpi and you get the DpiChanged event when and if it changes.

有关详细信息,请参阅 DPI and Device-Independent Pixels

您应该查看 GitHub 上的 Direct3D UWP 游戏模板,并查看 Main.cpp 中的处理方式。