Advertisement
Manu404

ScreenMgmt

May 21st, 2011
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5.  
  6. namespace ScreenMgmtLibrary
  7. {
  8.     #region Structures
  9.     [StructLayout(LayoutKind.Sequential)]
  10.     public struct DISPLAY_DEVICE
  11.     {
  12.         public int cb;
  13.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  14.         public string DeviceName;
  15.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  16.         public string DeviceString;
  17.         public int StateFlags;
  18.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  19.         public string DeviceID;
  20.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  21.         public string DeviceKey;
  22.  
  23.         public DISPLAY_DEVICE(int flags)
  24.         {
  25.             cb = 0;
  26.             StateFlags = flags;
  27.             DeviceName = new string((char)32, 32);
  28.             DeviceString = new string((char)32, 128);
  29.             DeviceID = new string((char)32, 128);
  30.             DeviceKey = new string((char)32, 128);
  31.             cb = Marshal.SizeOf(this);
  32.         }
  33.     }
  34.  
  35.     [StructLayout(LayoutKind.Sequential)]
  36.     public struct DEVMODE
  37.     {
  38.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  39.         public string dmDeviceName;
  40.         public short dmSpecVersion;
  41.         public short dmDriverVersion;
  42.         public short dmSize;
  43.         public short dmDriverExtra;
  44.         public int dmFields;
  45.         public short dmOrientation;
  46.         public short dmPaperSize;
  47.         public short dmPaperLength;
  48.         public short dmPaperWidth;
  49.         public short dmScale;
  50.         public short dmCopies;
  51.         public short dmDefaultSource;
  52.         public short dmPrintQuality;
  53.         public short dmColor;
  54.         public short dmDuplex;
  55.         public short dmYResolution;
  56.         public short dmTTOption;
  57.         public short dmCollate;
  58.         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  59.         public string dmFormName;
  60.         public short dmUnusedPadding;
  61.         public short dmBitsPerPel;
  62.         public int dmPelsWidth;
  63.         public int dmPelsHeight;
  64.         public int dmDisplayFlags;
  65.         public int dmDisplayFrequency;
  66.     }
  67.     #endregion
  68.  
  69.     public static class Screenmgmt
  70.     {
  71.         #region DLLImports
  72.         [DllImport("User32.dll")]
  73.         private static extern bool EnumDisplayDevices(IntPtr lpDevice, int iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);
  74.  
  75.         [DllImport("User32.dll")]
  76.         private static extern bool EnumDisplaySettings(string devName, int modeNum, ref DEVMODE devMode);
  77.  
  78.         [DllImport("user32.dll")]
  79.         private static extern int ChangeDisplaySettings(ref DEVMODE devMode, int flags);
  80.         #endregion
  81.  
  82.         /// <summary>
  83.         /// Change the resolution of <param name="devNum"> by <param name="modeNum">
  84.         /// </summary>
  85.         static public void ChangeResolution(int devNum, int modeNum)
  86.         {
  87.             DEVMODE d = GetDevmode(devNum, modeNum);
  88.             if (d.dmBitsPerPel != 0 & d.dmPelsWidth != 0 & d.dmPelsHeight != 0)
  89.                 ChangeDisplaySettings(ref d, 0);
  90.         }
  91.  
  92.         #region Modes
  93.         /// <summary>
  94.         /// Return a list of available modes
  95.         /// </summary>
  96.         static public List<string> EnumModes(int devNum)
  97.         {
  98.             List<string> modes = new List<string>();
  99.             string devName = GetDeviceName(devNum);
  100.             DEVMODE devMode = new DEVMODE();
  101.             int modeNum = 0;
  102.             bool result = true;
  103.             do
  104.             {
  105.                 result = EnumDisplaySettings(devName, modeNum, ref devMode);
  106.                 if (result)
  107.                 {
  108.                     string item = DevmodeToString(devMode);
  109.                     modes.Add(item);
  110.                 }
  111.                 modeNum++;
  112.             } while (result);
  113.             return modes;
  114.         }
  115.  
  116.         /// <summary>
  117.         /// Get the actual mode of the device <param name="devNum">
  118.         /// </summary>
  119.         static public DEVMODE GetDevmode(int devNum, int modeNum)
  120.         {
  121.             DEVMODE devMode = new DEVMODE();
  122.             string devName = GetDeviceName(devNum);
  123.             EnumDisplaySettings(devName, modeNum, ref devMode);
  124.             return devMode;
  125.         }
  126.  
  127.         /// <summary>
  128.         /// Convert to a human readable format a devmode, for eg : to ask for the user to choose one
  129.         /// </summary>
  130.         static public string DevmodeToString(DEVMODE devMode)
  131.         {
  132.             return devMode.dmPelsWidth.ToString() + " x " + devMode.dmPelsHeight.ToString() + ", "
  133.                 + devMode.dmBitsPerPel.ToString() + " bits, "
  134.                 + devMode.dmDisplayFrequency.ToString() + " Hz";
  135.         }
  136.         #endregion
  137.  
  138.         #region Devices
  139.         /// <summary>
  140.         /// Return a list of string of all the available devices
  141.         /// </summary>
  142.         static public List<string> EnumDevices()
  143.         {
  144.             List<string> devices = new List<string>();
  145.  
  146.             DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
  147.  
  148.             int devNum = 0;
  149.             bool result;
  150.             do
  151.             {
  152.                 result = EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0);
  153.                 if (result)
  154.                 {
  155.                     string item = DeviceToString(d, devNum);
  156.                     devices.Add(item);
  157.                 }
  158.                 devNum++;
  159.             } while (result);
  160.             return devices;
  161.         }
  162.  
  163.         /// <summary>
  164.         /// Convert a display device to a human readable format
  165.         /// </summary>
  166.         static public string DeviceToString(DISPLAY_DEVICE d, int devNum)
  167.         {
  168.             string r = devNum.ToString() + ". " + d.DeviceString.Trim();
  169.             if ((d.StateFlags & 4) != 0) r += " - MASTER";
  170.             return r;
  171.         }
  172.  
  173.         /// <summary>
  174.         /// Get the name of a device passed in argument
  175.         /// </summary>
  176.         static public string GetDeviceName(int devNum)
  177.         {
  178.             DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
  179.             bool result = EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0);
  180.             return (result ? d.DeviceName.Trim() : "#error#");
  181.         }
  182.  
  183.         /// <summary>
  184.         /// Get the name of the Main Device
  185.         /// </summary>
  186.         static public bool MainDevice(int devNum)
  187.         {
  188.             DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
  189.             if (EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0))
  190.                 return ((d.StateFlags & 4) != 0);
  191.             return false;
  192.         }
  193.         #endregion
  194.  
  195.         #region Search
  196.         /// <summary>
  197.         /// Return index of actuals modes
  198.         /// </summary>
  199.         static public List<int> SearchActualResolutionModes()
  200.         {
  201.             List<int> r = new List<int>();
  202.             int i = 0;
  203.             while (MainDevice(i) == false) i++;
  204.             r.Add(i);
  205.  
  206.             DEVMODE current = GetDevmode(i, -1);
  207.             r.Add(EnumModes(r.First()).IndexOf(DevmodeToString(current)));
  208.  
  209.             return r;
  210.         }
  211.  
  212.         /// <summary>
  213.         /// Return index for the mode of the device passed on argument
  214.         /// </summary>
  215.         static public int SearchSettingsString(string toFind, int devNum)
  216.         {
  217.             List<string> modes = Screenmgmt.EnumModes(devNum);
  218.             int i = 0;
  219.             foreach (string s in modes)
  220.             {
  221.                 if (s == toFind)
  222.                     return i;
  223.                 else
  224.                     i++;
  225.             }
  226.             return -1;
  227.         }
  228.  
  229.         /// <summary>
  230.         /// Return devmode of the mode for the device passed on argument
  231.         /// </summary>
  232.         static public DEVMODE SearchSettingsStruct(string toFind, int devNum)
  233.         {
  234.             string devName = GetDeviceName(devNum);
  235.             DEVMODE devMode = new DEVMODE();
  236.             int modeNum = 0;
  237.             bool result = true;
  238.             do
  239.             {
  240.                 result = EnumDisplaySettings(devName, modeNum, ref devMode);
  241.                 if (result)
  242.                 {
  243.                     string item = DevmodeToString(devMode);
  244.                     if (item == toFind)
  245.                         return devMode;
  246.                 }
  247.                 modeNum++;
  248.             } while (result);
  249.             return devMode;
  250.         }
  251.         #endregion
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement