Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

winapi - How to list available video modes using C#?

I've found nice examples using C++ (http://www.codeproject.com/KB/tips/resswitch.aspx), but not in C#.

Can someone help, please?

Edit: The exact function that list the video modes is:

BOOL CVideoModes::GetAvailableVideoModes(CAvailableVideoModes& modes)
{
  modes.SetSize(0, 5);
  int i=0;
  DEVMODE dm;

  while (EnumDisplaySettings(NULL, i, &dm))
  {
    CVideoMode thismode(dm.dmBitsPerPel, dm.dmPelsWidth, 
                        dm.dmPelsHeight, dm.dmDisplayFrequency);
    modes.SetAtGrow(i, thismode);
    ++i;
  }

  modes.FreeExtra();

  return (i>0);
}

But sincerelly I cannot understand that C++ code. Where I can find that "thismode" function?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you mean video modes are available resolutions, try to invoke EnumDisplaySettingsEx

details can be found here:

http://msdn.microsoft.com/en-us/library/dd162612(VS.85).aspx

small program that lists available resolutions:

using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ListResolutions
{

    class Program
    {
        [DllImport("user32.dll")]
        public static extern bool EnumDisplaySettings(
              string deviceName, int modeNum, ref DEVMODE devMode);
        const int ENUM_CURRENT_SETTINGS = -1;

        const int ENUM_REGISTRY_SETTINGS = -2;

        [StructLayout(LayoutKind.Sequential)]
        public 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 ScreenOrientation 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;

        }

        static void Main(string[] args)
        {            
                DEVMODE vDevMode = new DEVMODE();
                int i = 0;
                while (EnumDisplaySettings(null, i, ref vDevMode))
                {
                    Console.WriteLine("Width:{0} Height:{1} Color:{2} Frequency:{3}",
                                            vDevMode.dmPelsWidth,
                                            vDevMode.dmPelsHeight,
                                            1 << vDevMode.dmBitsPerPel,         vDevMode.dmDisplayFrequency
                                        );
                i++;
                }
        }

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...