论坛上有很多贴子在讨论Windows Store App屏幕分辨率的问题,一般的想法是通过获取当前控制窗口的长宽,这样的做法的确可以在全屏模式下获取屏幕分辨率,但如果是处于非全屏状态,则获取不到当前屏幕的分辨率,只能获取当前窗口的长宽。

下图所示为获取的窗口长宽。

不过我们还有一种更为可靠的方式获取当前屏幕的大小: 通过使用DX获取当前设备的长宽

下图为所获取的桌面长宽:

 通过以下代码来实现:

MyLib::MyLib()
{
// This flag adds support for surfaces with a different color channel ordering than the API default.
// It is recommended usage, and is required for compatibility with Direct2D.
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;

#if defined(_DEBUG)
// If the project is in a debug build, enable debugging via SDK Layers with this flag.
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

// This array defines the set of DirectX hardware feature levels this app will support.
// Note the ordering should be preserved.
// Don't forget to declare your application's minimum required feature level in its
// description. All applications are assumed to support 9.1 unless otherwise stated.
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
};

// Create the DX11 API device object, and get a corresponding context.
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
DX::ThrowIfFailed(
D3D11CreateDevice(
nullptr, // specify null to use the default adapter
D3D_DRIVER_TYPE_HARDWARE,
0, // leave as 0 unless software device
creationFlags, // optionally set debug and Direct2D compatibility flags
featureLevels, // list of feature levels this app can support
ARRAYSIZE(featureLevels), // number of entries in above list
D3D11_SDK_VERSION, // always set this to D3D11_SDK_VERSION for Metro style apps
&device, // returns the Direct3D device created
&m_featureLevel, // returns feature level of device created
&context // returns the device immediate context
)
);

// Get the DirectX11.1 device by QI off the DirectX11 one.
DX::ThrowIfFailed(
device.As(&m_d3dDevice)
);
}


Point MyLib::ScreenResolution::get()
{
// Obtain the underlying DXGI device of the Direct3D11.1 device.
ComPtr<IDXGIDevice> dxgiDevice;
DX::ThrowIfFailed(
m_d3dDevice.As(&dxgiDevice)
);

// Identify the physical adapter (GPU or card) this device is running on.
ComPtr<IDXGIAdapter> dxgiAdapter;
DX::ThrowIfFailed(
dxgiDevice->GetAdapter(&dxgiAdapter)
);

IDXGIOutput * pOutput;
if (dxgiAdapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND)
{
DXGI_OUTPUT_DESC desc;
pOutput->GetDesc(&desc);
return Point(desc.DesktopCoordinates.right, desc.DesktopCoordinates.bottom);
}

return Point(0, 0);
}

 另外,我将示例上传到OneDrive中方便大家查看:http://1drv.ms/1gKw9Fa