Get Supported Screen Resolutions in XNA?

How do I get the screen resolutions in XNA? For example, when you change the screen resolution in windows and instead of giving you a list of all the possible options, it only gives you a few of them.

+3


source to share


2 answers


Just use this:

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

      



But it will give you some duplicates because it also takes into account the speed of the redial, so you can turn that applause on or do some filtering.

+5


source


I'm not that modern XNA, but I didn't think there was a quick and easy feature. There is a way to use the old WinForms API, but since I personally don't want to reference it in other applications, the easiest way is to use native functions.

First, define your own construct to be used:

[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;
}

      

We also need to define two functions of our own that we will use:



[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);

      

Finally, our functions list all screen resolutions and one to get the current screen resolution:

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);
}

      

+3


source







All Articles