2009-04-13 2 views
7

Ich habe schöne Beispiele gefunden mit C++ (http://www.codeproject.com/KB/tips/resswitch.aspx), aber nicht in C#.Wie listen Sie verfügbare Videomodi mit C# auf?

Kann jemand bitte helfen?

Edit: Die genaue Funktion, die die Videomodi Liste ist:

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

Aber sincerelly kann ich verstehen nicht, dass C++ Code. Wo finde ich das "thismode" Funktion?

+0

I Gast, dass Sie nicht viel Erfahrung mit VC++ haben? thismode ist eine Instanz von CVideoMode. Um Ihr Problem zu lösen, können Sie EnumDisplaySettings/EnumDisplaySettingsEx ausprobieren. Sie sind Win32 API, so dass Sie nicht direkt aufrufen können. Siehe meine Antwort für weitere Details :) – Vimvq1987

+0

Ich kenne EnumDisplaySettings/EnumDisplaySettingsEx, aber ich weiß nicht, wie zu implementieren ... Der einzige Code, den ich fand, war in C++, aber ich kann nicht entschlüsseln ... –

+0

Ich habe gerade habe ein kleines Programm in C# geschrieben. Ich hoffe das hilft! – Vimvq1987

Antwort

13

Wenn Sie Videomodi bedeuten verfügbaren Auflösungen sind, versuchen aufzurufen EnumDisplaySettingsEx

Details finden Sie hier:

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

kleines Programm, das verfügbaren Auflösungen aufgeführt:

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

    } 

} 
+0

Perfekt! Danke! –