Guest User

Untitled

a guest
Jul 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows;
  4. using System.Windows.Interop;
  5.  
  6. namespace CRT
  7. {
  8. /// <summary>
  9. /// Interaction logic for Window1.xaml
  10. /// </summary>
  11. public partial class Window1 : Window
  12. {
  13. public class NativeMethods
  14.  
  15. {
  16. [DllImport("user32.dll", EntryPoint = "MonitorFromWindow", SetLastError = true)]
  17. public static extern IntPtr MonitorFromWindow(
  18. [In] IntPtr hwnd, uint dwFlags);
  19.  
  20. [DllImport("dxva2.dll", EntryPoint = "GetNumberOfPhysicalMonitorsFromHMONITOR", SetLastError = true)]
  21. [return: MarshalAs(UnmanagedType.Bool)]
  22. public static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(
  23. IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);
  24.  
  25. [DllImport("dxva2.dll", EntryPoint = "GetPhysicalMonitorsFromHMONITOR", SetLastError = true)]
  26. [return: MarshalAs(UnmanagedType.Bool)]
  27. public static extern bool GetPhysicalMonitorsFromHMONITOR(
  28. IntPtr hMonitor,
  29. uint dwPhysicalMonitorArraySize,
  30. [Out] NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray);
  31.  
  32. [DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitors", SetLastError = true)]
  33. [return: MarshalAs(UnmanagedType.Bool)]
  34. public static extern bool DestroyPhysicalMonitors(
  35. uint dwPhysicalMonitorArraySize, [Out] NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray);
  36.  
  37. [DllImport("dxva2.dll", EntryPoint = "GetMonitorTechnologyType", SetLastError = true)]
  38. [return: MarshalAs(UnmanagedType.Bool)]
  39. public static extern bool GetMonitorTechnologyType(
  40. IntPtr hMonitor, ref NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE pdtyDisplayTechnologyType);
  41.  
  42. [DllImport("dxva2.dll", EntryPoint = "GetMonitorCapabilities", SetLastError = true)]
  43. [return: MarshalAs(UnmanagedType.Bool)]
  44. public static extern bool GetMonitorCapabilities(
  45. IntPtr hMonitor, ref uint pdwMonitorCapabilities, ref uint pdwSupportedColorTemperatures);
  46. }
  47.  
  48. public class NativeConstants
  49. {
  50. public const int MONITOR_DEFAULTTOPRIMARY = 1;
  51.  
  52. public const int MONITOR_DEFAULTTONEAREST = 2;
  53.  
  54. public const int MONITOR_DEFAULTTONULL = 0;
  55. }
  56.  
  57. public class NativeStructures
  58. {
  59. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  60. public struct PHYSICAL_MONITOR
  61. {
  62. public IntPtr hPhysicalMonitor;
  63.  
  64. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string szPhysicalMonitorDescription;
  65. }
  66.  
  67. public enum MC_DISPLAY_TECHNOLOGY_TYPE
  68. {
  69. MC_SHADOW_MASK_CATHODE_RAY_TUBE,
  70.  
  71. MC_APERTURE_GRILL_CATHODE_RAY_TUBE,
  72.  
  73. MC_THIN_FILM_TRANSISTOR,
  74.  
  75. MC_LIQUID_CRYSTAL_ON_SILICON,
  76.  
  77. MC_PLASMA,
  78.  
  79. MC_ORGANIC_LIGHT_EMITTING_DIODE,
  80.  
  81. MC_ELECTROLUMINESCENT,
  82.  
  83. MC_MICROELECTROMECHANICAL,
  84.  
  85. MC_FIELD_EMISSION_DEVICE,
  86. }
  87. }
  88.  
  89. public Window1() { InitializeComponent(); }
  90.  
  91. private void Button_Click(object sender, RoutedEventArgs e)
  92. {
  93. WindowInteropHelper helper = new WindowInteropHelper(this);
  94.  
  95. IntPtr hMonitor = NativeMethods.MonitorFromWindow(helper.Handle, NativeConstants.MONITOR_DEFAULTTOPRIMARY);
  96. int lastWin32Error = Marshal.GetLastWin32Error();
  97.  
  98. uint pdwNumberOfPhysicalMonitors = 0u;
  99. bool numberOfPhysicalMonitorsFromHmonitor = NativeMethods.GetNumberOfPhysicalMonitorsFromHMONITOR(
  100. hMonitor, ref pdwNumberOfPhysicalMonitors);
  101. lastWin32Error = Marshal.GetLastWin32Error();
  102.  
  103. NativeStructures.PHYSICAL_MONITOR[] pPhysicalMonitorArray =
  104. new NativeStructures.PHYSICAL_MONITOR[pdwNumberOfPhysicalMonitors];
  105. bool physicalMonitorsFromHmonitor = NativeMethods.GetPhysicalMonitorsFromHMONITOR(
  106. hMonitor, pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray);
  107. lastWin32Error = Marshal.GetLastWin32Error();
  108.  
  109. uint pdwMonitorCapabilities = 0u;
  110. uint pdwSupportedColorTemperatures = 0u;
  111. var monitorCapabilities = NativeMethods.GetMonitorCapabilities(
  112. pPhysicalMonitorArray[0].hPhysicalMonitor, ref pdwMonitorCapabilities, ref pdwSupportedColorTemperatures);
  113. lastWin32Error = Marshal.GetLastWin32Error();
  114.  
  115. NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE type =
  116. NativeStructures.MC_DISPLAY_TECHNOLOGY_TYPE.MC_SHADOW_MASK_CATHODE_RAY_TUBE;
  117. var monitorTechnologyType = NativeMethods.GetMonitorTechnologyType(
  118. pPhysicalMonitorArray[0].hPhysicalMonitor, ref type);
  119. lastWin32Error = Marshal.GetLastWin32Error();
  120.  
  121. var destroyPhysicalMonitors = NativeMethods.DestroyPhysicalMonitors(
  122. pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray);
  123. lastWin32Error = Marshal.GetLastWin32Error();
  124.  
  125. this.lbl.Content = type;
  126. }
  127. }
  128. }
Add Comment
Please, Sign In to add comment