在XNA中获得支持的屏幕分辨率?

时间:2012-04-06 05:02:49

标签: c# .net xna

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

2 个答案:

答案 0 :(得分:5)

请使用:

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

但是它会给你很少的重复,因为它也会影响refrash率,所以你可能会包括aswel,或者做一些过滤。

答案 1 :(得分:3)

我不是最新的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);
}