CommunistPancake
CommunistPancake

Reputation: 605

Get the supported screen resolutions in XNA?

How can I get the supported screen resolutions in XNA? Like when you change the screen resolution in windows, and instead of giving you a list of all the possible choices, it gives you just a few of them.

Upvotes: 3

Views: 3011

Answers (2)

NewProger
NewProger

Reputation: 3075

Just use this:

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

But it will give you few duplicates, because it also takes into accound refrash rate, so you might include that aswel, or do some filtering.

Upvotes: 6

Jensen
Jensen

Reputation: 3538

I'm not that up-to-date with XNA, but I didn't think there was a quick and easy function. There is a way using the old WinForms API, but as I personally don't want to link against that in other applications the easiest way is to use the native functions.

First, define the native struct that will 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 the two native functions 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);

And last, our functions to 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);
}

Upvotes: 3

Related Questions