3

如何在 XNA 中获得支持的屏幕分辨率?就像您在 Windows 中更改屏幕分辨率时一样,它不会为您提供所有可能选择的列表,而是只为您提供其中的一些选择。

4

2 回答 2

5

只需使用这个:

foreach (DisplayMode mode in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes) {
    //mode.whatever (and use any of avaliable information)
}

但它会给你很少的重复,因为它也考虑到刷新率,所以你可能会包括那个 aswel,或者做一些过滤。

于 2012-09-19T04:19:53.350 回答
4

我不是最新的 XNA,但我不认为有一个快速和简单的功能。有一种使用旧 WinForms API 的方法,但我个人不想在其他应用程序中链接它,最简单的方法是使用本机函数。

首先,定义将使用的本机结构:

[StructLayout(LayoutKind.Sequential)]
internal struct DEVMODE
{
    private const int CCHDEVICENAME = 0x20;
    private const int CCHFORMNAME = 0x20;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
    public string dmDeviceName;
    public short dmSpecVersion;
    public short dmDriverVersion;
    public short dmSize;
    public short dmDriverExtra;
    public int dmFields;
    public int dmPositionX;
    public int dmPositionY;
    public int dmDisplayOrientation;
    public int dmDisplayFixedOutput;
    public short dmColor;
    public short dmDuplex;
    public short dmYResolution;
    public short dmTTOption;
    public short dmCollate;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
    public string dmFormName;

    public short dmLogPixels;
    public int dmBitsPerPel;
    public int dmPelsWidth;
    public int dmPelsHeight;
    public int dmDisplayFlags;
    public int dmDisplayFrequency;
    public int dmICMMethod;
    public int dmICMIntent;
    public int dmMediaType;
    public int dmDitherType;
    public int dmReserved1;
    public int dmReserved2;
    public int dmPanningWidth;
    public int dmPanningHeight;
}

我们还需要定义我们将使用的两个本机函数:

[DllImport("user32.dll")]
private static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);

[DllImport("user32.dll")]
private static extern int GetSystemMetrics(int nIndex);

最后,我们列出所有屏幕分辨率的函数和一个获取当前屏幕分辨率的函数:

public static List<string> GetScreenResolutions()
{
    var resolutions = new List<string>();

    try
    {
        var devMode = new DEVMODE();
        int i = 0;

        while (EnumDisplaySettings(null, i, ref devMode))
        {
            resolutions.Add(string.Format("{0}x{1}", devMode.dmPelsWidth, devMode.dmPelsHeight));
            i++;
        }

        resolutions = resolutions.Distinct(StringComparer.InvariantCulture).ToList();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Could not get screen resolutions.");
    }

    return resolutions;
}

public static string GetCurrentScreenResolution()
{
    int width = GetSystemMetrics(0x00);
    int height = GetSystemMetrics(0x01);

    return string.Format("{0}x{1}", width, height);
}
于 2012-04-06T08:49:37.237 回答