2009-04-13 85 views
7

我發現很好的例子使用C++(http://www.codeproject.com/KB/tips/resswitch.aspx),但不是在C#中。如何使用C#列出可用的視頻模式?

有人可以幫忙嗎?

編輯: 該列表中選擇視頻模式的確切功能:

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

但sincerelly我無法理解的是C++代碼。我在哪裏可以找到「thismode」功能?

+0

我客人,你沒有太多的經驗與VC + +? thismode是CVideoMode的一個實例。要解決您的問題,您可以嘗試EnumDisplaySettings/EnumDisplaySettingsEx。他們是Win32 API,所以你不能直接調用。請參閱我的答案更多詳細信息:) – Vimvq1987 2009-04-13 17:19:05

+0

我知道EnumDisplaySettings/EnumDisplaySettingsEx,但我不知道如何實現...唯一的代碼,我發現是在C++中,但我不能decypher ... – 2009-04-13 17:59:40

+0

我只是用C#編寫一個小程序。希望這個幫助! – Vimvq1987 2009-04-14 00:26:36

回答

13

如果你的意思視頻模式可用的分辨率,嘗試調用EnumDisplaySettingsEx

細節可以在這裏找到:

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

小程序,列出了可用的分辨率:

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

完美!謝謝! – 2009-04-14 03:27:36

相關問題