Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- namespace ScreenMgmtLibrary
- {
- #region Structures
- [StructLayout(LayoutKind.Sequential)]
- public struct DISPLAY_DEVICE
- {
- public int cb;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
- public string DeviceName;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
- public string DeviceString;
- public int StateFlags;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
- public string DeviceID;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
- public string DeviceKey;
- public DISPLAY_DEVICE(int flags)
- {
- cb = 0;
- StateFlags = flags;
- DeviceName = new string((char)32, 32);
- DeviceString = new string((char)32, 128);
- DeviceID = new string((char)32, 128);
- DeviceKey = new string((char)32, 128);
- cb = Marshal.SizeOf(this);
- }
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct DEVMODE
- {
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
- public string dmDeviceName;
- public short dmSpecVersion;
- public short dmDriverVersion;
- public short dmSize;
- public short dmDriverExtra;
- public int dmFields;
- public short dmOrientation;
- public short dmPaperSize;
- public short dmPaperLength;
- public short dmPaperWidth;
- public short dmScale;
- public short dmCopies;
- public short dmDefaultSource;
- public short dmPrintQuality;
- public short dmColor;
- public short dmDuplex;
- public short dmYResolution;
- public short dmTTOption;
- public short dmCollate;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
- public string dmFormName;
- public short dmUnusedPadding;
- public short dmBitsPerPel;
- public int dmPelsWidth;
- public int dmPelsHeight;
- public int dmDisplayFlags;
- public int dmDisplayFrequency;
- }
- #endregion
- public static class Screenmgmt
- {
- #region DLLImports
- [DllImport("User32.dll")]
- private static extern bool EnumDisplayDevices(IntPtr lpDevice, int iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, int dwFlags);
- [DllImport("User32.dll")]
- private static extern bool EnumDisplaySettings(string devName, int modeNum, ref DEVMODE devMode);
- [DllImport("user32.dll")]
- private static extern int ChangeDisplaySettings(ref DEVMODE devMode, int flags);
- #endregion
- /// <summary>
- /// Change the resolution of <param name="devNum"> by <param name="modeNum">
- /// </summary>
- static public void ChangeResolution(int devNum, int modeNum)
- {
- DEVMODE d = GetDevmode(devNum, modeNum);
- if (d.dmBitsPerPel != 0 & d.dmPelsWidth != 0 & d.dmPelsHeight != 0)
- ChangeDisplaySettings(ref d, 0);
- }
- #region Modes
- /// <summary>
- /// Return a list of available modes
- /// </summary>
- static public List<string> EnumModes(int devNum)
- {
- List<string> modes = new List<string>();
- string devName = GetDeviceName(devNum);
- DEVMODE devMode = new DEVMODE();
- int modeNum = 0;
- bool result = true;
- do
- {
- result = EnumDisplaySettings(devName, modeNum, ref devMode);
- if (result)
- {
- string item = DevmodeToString(devMode);
- modes.Add(item);
- }
- modeNum++;
- } while (result);
- return modes;
- }
- /// <summary>
- /// Get the actual mode of the device <param name="devNum">
- /// </summary>
- static public DEVMODE GetDevmode(int devNum, int modeNum)
- {
- DEVMODE devMode = new DEVMODE();
- string devName = GetDeviceName(devNum);
- EnumDisplaySettings(devName, modeNum, ref devMode);
- return devMode;
- }
- /// <summary>
- /// Convert to a human readable format a devmode, for eg : to ask for the user to choose one
- /// </summary>
- static public string DevmodeToString(DEVMODE devMode)
- {
- return devMode.dmPelsWidth.ToString() + " x " + devMode.dmPelsHeight.ToString() + ", "
- + devMode.dmBitsPerPel.ToString() + " bits, "
- + devMode.dmDisplayFrequency.ToString() + " Hz";
- }
- #endregion
- #region Devices
- /// <summary>
- /// Return a list of string of all the available devices
- /// </summary>
- static public List<string> EnumDevices()
- {
- List<string> devices = new List<string>();
- DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
- int devNum = 0;
- bool result;
- do
- {
- result = EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0);
- if (result)
- {
- string item = DeviceToString(d, devNum);
- devices.Add(item);
- }
- devNum++;
- } while (result);
- return devices;
- }
- /// <summary>
- /// Convert a display device to a human readable format
- /// </summary>
- static public string DeviceToString(DISPLAY_DEVICE d, int devNum)
- {
- string r = devNum.ToString() + ". " + d.DeviceString.Trim();
- if ((d.StateFlags & 4) != 0) r += " - MASTER";
- return r;
- }
- /// <summary>
- /// Get the name of a device passed in argument
- /// </summary>
- static public string GetDeviceName(int devNum)
- {
- DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
- bool result = EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0);
- return (result ? d.DeviceName.Trim() : "#error#");
- }
- /// <summary>
- /// Get the name of the Main Device
- /// </summary>
- static public bool MainDevice(int devNum)
- {
- DISPLAY_DEVICE d = new DISPLAY_DEVICE(0);
- if (EnumDisplayDevices(IntPtr.Zero, devNum, ref d, 0))
- return ((d.StateFlags & 4) != 0);
- return false;
- }
- #endregion
- #region Search
- /// <summary>
- /// Return index of actuals modes
- /// </summary>
- static public List<int> SearchActualResolutionModes()
- {
- List<int> r = new List<int>();
- int i = 0;
- while (MainDevice(i) == false) i++;
- r.Add(i);
- DEVMODE current = GetDevmode(i, -1);
- r.Add(EnumModes(r.First()).IndexOf(DevmodeToString(current)));
- return r;
- }
- /// <summary>
- /// Return index for the mode of the device passed on argument
- /// </summary>
- static public int SearchSettingsString(string toFind, int devNum)
- {
- List<string> modes = Screenmgmt.EnumModes(devNum);
- int i = 0;
- foreach (string s in modes)
- {
- if (s == toFind)
- return i;
- else
- i++;
- }
- return -1;
- }
- /// <summary>
- /// Return devmode of the mode for the device passed on argument
- /// </summary>
- static public DEVMODE SearchSettingsStruct(string toFind, int devNum)
- {
- string devName = GetDeviceName(devNum);
- DEVMODE devMode = new DEVMODE();
- int modeNum = 0;
- bool result = true;
- do
- {
- result = EnumDisplaySettings(devName, modeNum, ref devMode);
- if (result)
- {
- string item = DevmodeToString(devMode);
- if (item == toFind)
- return devMode;
- }
- modeNum++;
- } while (result);
- return devMode;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement