Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. Microsoft.Win32.SystemEvents.DisplaySettingsChanged += new EventHandler(this.SystemEvents_DisplaySettingsChanged);
  2.  
  3. /// <summary>
  4. /// This class deals with monitors.
  5. /// </summary>
  6. internal static class Monitors
  7. {
  8. private static List<Monitors.Screen> Screens = null;
  9.  
  10. internal static List<Monitors.Screen> GetScreens()
  11. {
  12. Monitors.Screens = new List<Monitors.Screen>();
  13.  
  14. var handler = new NativeMethods.DisplayDevicesMethods.EnumMonitorsDelegate(Monitors.MonitorEnumProc);
  15. NativeMethods.DisplayDevicesMethods.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, handler, IntPtr.Zero); // should be sequential
  16.  
  17. return Monitors.Screens;
  18. }
  19.  
  20. private static bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdcMonitor, NativeMethods.DisplayDevicesMethods.RECT rect, IntPtr dwData)
  21. {
  22. NativeMethods.DisplayDevicesMethods.MONITORINFO mi = new NativeMethods.DisplayDevicesMethods.MONITORINFO();
  23.  
  24. if (NativeMethods.DisplayDevicesMethods.GetMonitorInfo(hMonitor, mi))
  25. {
  26. Monitors.Screens.Add(new Monitors.Screen(
  27. (mi.dwFlags & 1) == 1, // 1 = primary monitor
  28. mi.rcMonitor.Left,
  29. mi.rcMonitor.Top,
  30. Math.Abs(mi.rcMonitor.Right - mi.rcMonitor.Left),
  31. Math.Abs(mi.rcMonitor.Bottom - mi.rcMonitor.Top)));
  32. }
  33.  
  34. return true;
  35. }
  36.  
  37. /// <summary>
  38. /// Represents a display device on a single system.
  39. /// </summary>
  40. internal sealed class Screen
  41. {
  42. /// <summary>
  43. /// Initializes a new instance of the Screen class.
  44. /// </summary>
  45. /// <param name="primary">A value indicating whether the display is the primary screen.</param>
  46. /// <param name="x">The display's top corner X value.</param>
  47. /// <param name="y">The display's top corner Y value.</param>
  48. /// <param name="w">The width of the display.</param>
  49. /// <param name="h">The height of the display.</param>
  50. internal Screen(bool primary, int x, int y, int w, int h)
  51. {
  52. this.IsPrimary = primary;
  53. this.TopX = x;
  54. this.TopY = y;
  55. this.Width = w;
  56. this.Height = h;
  57. }
  58.  
  59. /// <summary>
  60. /// Gets a value indicating whether the display device is the primary monitor.
  61. /// </summary>
  62. internal bool IsPrimary { get; private set; }
  63.  
  64. /// <summary>
  65. /// Gets the display's top corner X value.
  66. /// </summary>
  67. internal int TopX { get; private set; }
  68.  
  69. /// <summary>
  70. /// Gets the display's top corner Y value.
  71. /// </summary>
  72. internal int TopY { get; private set; }
  73.  
  74. /// <summary>
  75. /// Gets the width of the display.
  76. /// </summary>
  77. internal int Width { get; private set; }
  78.  
  79. /// <summary>
  80. /// Gets the height of the display.
  81. /// </summary>
  82. internal int Height { get; private set; }
  83. }
  84. }
  85.  
  86. internal static class NativeMethods
  87. {
  88. /// <summary>
  89. /// Methods for retrieving display devices.
  90. /// </summary>
  91. internal static class DisplayDevicesMethods
  92. {
  93. internal delegate bool EnumMonitorsDelegate(IntPtr hMonitor, IntPtr hdcMonitor, NativeMethods.DisplayDevicesMethods.RECT rect, IntPtr dwData);
  94.  
  95. [DllImport("user32.dll")]
  96. [return: MarshalAs(UnmanagedType.Bool)]
  97. internal static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, EnumMonitorsDelegate lpfnEnum, IntPtr dwData);
  98.  
  99. /// <summary>
  100. /// Retrieves information about a display monitor.
  101. /// </summary>
  102. /// <param name="hmonitor">A handle to the display monitor of interest.</param>
  103. /// <param name="info">A pointer to a MONITORINFO or MONITORINFOEX structure that receives information about the specified display monitor.</param>
  104. /// <returns>If the function succeeds, the return value is nonzero.</returns>
  105. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  106. [return: MarshalAs(UnmanagedType.Bool)]
  107. internal static extern bool GetMonitorInfo(IntPtr hmonitor, [In, Out] NativeMethods.DisplayDevicesMethods.MONITORINFO info);
  108.  
  109. /// <summary>
  110. /// The RECT structure defines the coordinates of the upper-left and lower-right corners of a rectangle.
  111. /// </summary>
  112. [StructLayout(LayoutKind.Sequential)]
  113. internal struct RECT
  114. {
  115. public int Left;
  116. public int Top;
  117. public int Right;
  118. public int Bottom;
  119. }
  120.  
  121. /// <summary>
  122. /// The MONITORINFO structure contains information about a display monitor.
  123. /// </summary>
  124. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 4)]
  125. internal class MONITORINFO
  126. {
  127. internal int cbSize = Marshal.SizeOf(typeof(NativeMethods.DisplayDevicesMethods.MONITORINFO));
  128. internal NativeMethods.DisplayDevicesMethods.RECT rcMonitor = new NativeMethods.DisplayDevicesMethods.RECT();
  129. internal NativeMethods.DisplayDevicesMethods.RECT rcWork = new NativeMethods.DisplayDevicesMethods.RECT();
  130. internal int dwFlags;
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement