public static void DestroyPhysicalMonitors(PHYSICAL_MONITOR[] physicalMonitorArray) { if (!DestroyPhysicalMonitors((uint)physicalMonitorArray.Length, physicalMonitorArray)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
public static void SetMonitorBrightness(PHYSICAL_MONITOR physicalMonitor, double brightness, uint dwMinimumBrightness, uint dwMaximumBrightness) { if (!SetMonitorBrightness(physicalMonitor.hPhysicalMonitor, (uint)(dwMinimumBrightness + (dwMaximumBrightness - dwMinimumBrightness) * brightness))) { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
public static void GetDevices(Action <List <PHYSICAL_MONITOR> > handleCallback) { var handles = new List <PHYSICAL_MONITOR>(); // Iterate monitors and retrieve their physical monitor instances EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, delegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData) { var supported = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, out uint arrSize); if (!supported) { return(true); } var arr = new PHYSICAL_MONITOR[arrSize]; supported = GetPhysicalMonitorsFromHMONITOR(hMonitor, arrSize, arr); if (!supported) { return(true); } handles.AddRange(arr); return(true); }, IntPtr.Zero); // Callback handleCallback(handles); // Cleanup DestroyPhysicalMonitors((uint)handles.Count, handles.ToArray()); }
private static PHYSICAL_MONITOR[] GetPhysicalMonitors(IntPtr displayMonitorPtr) { Dvxa2.GetNumberOfPhysicalMonitorsFromHMONITOR(displayMonitorPtr, out uint nrPhysicalMonitors); var physMonitors = new PHYSICAL_MONITOR[nrPhysicalMonitors]; Dvxa2.GetPhysicalMonitorsFromHMONITOR(displayMonitorPtr, nrPhysicalMonitors, physMonitors); return(physMonitors); }
public static double GetMonitorBrightness(PHYSICAL_MONITOR physicalMonitor) { if (!GetMonitorBrightness(physicalMonitor.hPhysicalMonitor, out uint dwMinimumBrightness, out uint dwCurrentBrightness, out uint dwMaximumBrightness)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return((double)(dwCurrentBrightness - dwMinimumBrightness) / (double)(dwMaximumBrightness - dwMinimumBrightness)); }
public static IEnumerable <PhysicalItem> EnumeratePhysicalMonitors(IntPtr monitorHandle) { if (!GetNumberOfPhysicalMonitorsFromHMONITOR( monitorHandle, out uint count)) { Debug.WriteLine($"Failed to get the number of physical monitors. ({Error.CreateMessage()})"); yield break; } if (count == 0) { yield break; } var physicalMonitors = new PHYSICAL_MONITOR[count]; try { if (!GetPhysicalMonitorsFromHMONITOR( monitorHandle, count, physicalMonitors)) { Debug.WriteLine($"Failed to get an array of physical monitors. ({Error.CreateMessage()})"); yield break; } int monitorIndex = 0; foreach (var physicalMonitor in physicalMonitors) { var handle = new SafePhysicalMonitorHandle(physicalMonitor.hPhysicalMonitor); bool isSupported = GetMonitorCapabilities( handle, out MC_CAPS caps, out MC_SUPPORTED_COLOR_TEMPERATURE temperature) && caps.HasFlag(MC_CAPS.MC_CAPS_BRIGHTNESS); //Debug.WriteLine($"Handle: {physicalMonitor.hPhysicalMonitor}"); //Debug.WriteLine($"Description: {physicalMonitor.szPhysicalMonitorDescription}"); //Debug.WriteLine($"IsSupported: {isSupported}"); if (isSupported) { yield return(new PhysicalItem( description: physicalMonitor.szPhysicalMonitorDescription, handle: handle, monitorIndex: monitorIndex)); } monitorIndex++; } } finally { // The physical monitor handles should be destroyed at a later stage. } }
public static double GetMonitorContrast(PHYSICAL_MONITOR physicalMonitor) { uint dwMinimumContrast, dwCurrentContrast, dwMaximumContrast; if (!GetMonitorContrast(physicalMonitor.hPhysicalMonitor, out dwMinimumContrast, out dwCurrentContrast, out dwMaximumContrast)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return (double)(dwCurrentContrast - dwMinimumContrast) / (double)(dwMaximumContrast - dwMinimumContrast); }
public static double GetMonitorBrightness(PHYSICAL_MONITOR physicalMonitor) { uint dwMinimumBrightness, dwCurrentBrightness, dwMaximumBrightness; if (!GetMonitorBrightness(physicalMonitor.hPhysicalMonitor, out dwMinimumBrightness, out dwCurrentBrightness, out dwMaximumBrightness)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return (double)(dwCurrentBrightness - dwMinimumBrightness) / (double)(dwMaximumBrightness - dwMinimumBrightness); }
public static uint GetMonitorCapabilities(PHYSICAL_MONITOR physicalMonitor) { uint dwMonitorCapabilities, dwSupportedColorTemperatures; if (!GetMonitorCapabilities(physicalMonitor.hPhysicalMonitor, out dwMonitorCapabilities, out dwSupportedColorTemperatures)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return(dwMonitorCapabilities); }
public static double GetMonitorContrast(PHYSICAL_MONITOR physicalMonitor) { uint dwMinimumContrast, dwCurrentContrast, dwMaximumContrast; if (!GetMonitorContrast(physicalMonitor.hPhysicalMonitor, out dwMinimumContrast, out dwCurrentContrast, out dwMaximumContrast)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return((double)(dwCurrentContrast - dwMinimumContrast) / (double)(dwMaximumContrast - dwMinimumContrast)); }
private double GetBrightness(PHYSICAL_MONITOR pHYSICAL_MONITOR) { uint dwMinimumBrightness, dwCurrentBrightness, dwMaximumBrightness; if (!GetMonitorBrightness(pHYSICAL_MONITOR.hPhysicalMonitor, out dwMinimumBrightness, out dwCurrentBrightness, out dwMaximumBrightness)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return((double)(dwCurrentBrightness - dwMinimumBrightness) / (double)(dwMaximumBrightness - dwMinimumBrightness)); }
public static IEnumerable <PhysicalItem> EnumeratePhysicalMonitors(IntPtr monitorHandle) { if (!GetNumberOfPhysicalMonitorsFromHMONITOR( monitorHandle, out uint count)) { Debug.WriteLine($"Failed to get the number of physical monitors. "); yield break; } if (count == 0) { yield break; } var physicalMonitors = new PHYSICAL_MONITOR[count]; try { if (!GetPhysicalMonitorsFromHMONITOR( monitorHandle, count, physicalMonitors)) { Debug.WriteLine($"Failed to get an array of physical monitors. "); yield break; } int monitorIndex = 0; foreach (var physicalMonitor in physicalMonitors) { var handle = new SafePhysicalMonitorHandle(physicalMonitor.hPhysicalMonitor); bool isHighLevelSupported = GetMonitorCapabilities( handle, out MC_CAPS caps, out _) && caps.HasFlag(MC_CAPS.MC_CAPS_BRIGHTNESS); yield return(new PhysicalItem( description: physicalMonitor.szPhysicalMonitorDescription, monitorIndex: monitorIndex, handle: handle, isHighLevelSupported: isHighLevelSupported)); monitorIndex++; } } finally { // The physical monitor handles should be destroyed at a later stage. } }
public static void SetMonitorEnable(IntPtr windowHandle, bool enable) { uint pdwNumberOfPhysicalMonitors = 0; IntPtr hMonitor = MonitorFromWindow(windowHandle, 1); bool numberOfPhysicalMonitorsFromHmonitor = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, ref pdwNumberOfPhysicalMonitors); var pPhysicalMonitorArray = new PHYSICAL_MONITOR[pdwNumberOfPhysicalMonitors]; GetPhysicalMonitorsFromHMONITOR(hMonitor, pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray); SetVCPFeature(pPhysicalMonitorArray[0].hPhysicalMonitor, PowerMode, enable ? PowerOn : PowerOff); }
public static void SetMonitorBrightness(PHYSICAL_MONITOR physicalMonitor, double brightness) { uint dwMinimumBrightness, dwCurrentBrightness, dwMaximumBrightness; if (!GetMonitorBrightness(physicalMonitor.hPhysicalMonitor, out dwMinimumBrightness, out dwCurrentBrightness, out dwMaximumBrightness)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (!SetMonitorBrightness(physicalMonitor.hPhysicalMonitor, (uint)(dwMinimumBrightness + (dwMaximumBrightness - dwMinimumBrightness) * brightness))) { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
public static void Main(string[] args) { var hMonitorList = new List <IntPtr>(); var monitorList = new List <PHYSICAL_MONITOR>(); var gcHandle = GCHandle.Alloc(hMonitorList); NativeInterop.MonitorEnumProc lpfnEnum = delegate(IntPtr hMonitor, IntPtr hDCMonitor, ref RECT lprcMonitor, IntPtr dwData) { var list = (List <IntPtr>)GCHandle.FromIntPtr(dwData).Target; list.Add(hMonitor); return(true); }; if (NativeInterop.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, lpfnEnum, GCHandle.ToIntPtr(gcHandle))) { foreach (var hMonitor in hMonitorList) { var numberOfMonitors = uint.MinValue; if (NativeInterop.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, ref numberOfMonitors)) { var monitors = new PHYSICAL_MONITOR[numberOfMonitors]; if (NativeInterop.GetPhysicalMonitorsFromHMONITOR(hMonitor, numberOfMonitors, monitors)) { foreach (var monitor in monitors) { monitorList.Add(monitor); } } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } } } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } foreach (var monitor in monitorList) { var brightness = NativeInterop.GetMonitorBrightness(monitor); //NativeInterop.SetMonitorBrightness(monitor, (brightness - 0.5)); Console.WriteLine(monitor.MonitorDescription); } NativeInterop.DestroyPhysicalMonitors((uint)monitorList.Count, monitorList.ToArray()); }
public static void SetMonitorContrast(PHYSICAL_MONITOR physicalMonitor, double contrast) { uint dwMinimumContrast, dwCurrentContrast, dwMaximumContrast; if (!GetMonitorContrast(physicalMonitor.hPhysicalMonitor, out dwMinimumContrast, out dwCurrentContrast, out dwMaximumContrast)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (!SetMonitorContrast(physicalMonitor.hPhysicalMonitor, (uint)(dwMinimumContrast + (dwMaximumContrast - dwMinimumContrast) * contrast))) { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
public static IEnumerable <PhysicalItem> EnumeratePhysicalMonitors(IntPtr monitorHandle, bool verbose = false) { if (!GetNumberOfPhysicalMonitorsFromHMONITOR( monitorHandle, out uint count)) { Debug.WriteLine($"Failed to get the number of physical monitors. {Error.GetMessage()}"); yield break; } if (count == 0) { yield break; } var physicalMonitors = new PHYSICAL_MONITOR[count]; try { if (!GetPhysicalMonitorsFromHMONITOR( monitorHandle, count, physicalMonitors)) { Debug.WriteLine($"Failed to get an array of physical monitors. {Error.GetMessage()}"); yield break; } int monitorIndex = 0; foreach (var physicalMonitor in physicalMonitors) { var handle = new SafePhysicalMonitorHandle(physicalMonitor.hPhysicalMonitor); //Debug.WriteLine($"Description: {physicalMonitor.szPhysicalMonitorDescription}"); //Debug.WriteLine($"Handle: {physicalMonitor.hPhysicalMonitor}"); yield return(new PhysicalItem( description: physicalMonitor.szPhysicalMonitorDescription, monitorIndex: monitorIndex, handle: handle, capability: GetMonitorCapability(handle, verbose))); monitorIndex++; } } finally { // The physical monitor handles should be destroyed at a later stage. } }
public static PHYSICAL_MONITOR[] GetPhysicalMonitors(IntPtr hMonitor) { uint dwNumberOfPhysicalMonitors; if (!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, out dwNumberOfPhysicalMonitors)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } PHYSICAL_MONITOR[] physicalMonitorArray = new PHYSICAL_MONITOR[dwNumberOfPhysicalMonitors]; if (!GetPhysicalMonitorsFromHMONITOR(hMonitor, dwNumberOfPhysicalMonitors, physicalMonitorArray)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return physicalMonitorArray; }
public static MonitorExtremums GetMonitorExtremums(PHYSICAL_MONITOR physicalMonitor) { if (!GetMonitorBrightness(physicalMonitor.hPhysicalMonitor, out uint dwMinimumBrightness, out uint dwCurrentBrightness, out uint dwMaximumBrightness)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return(new MonitorExtremums { Min = dwMinimumBrightness, Max = dwMaximumBrightness, Current = dwCurrentBrightness }); }
private void BTNConnectMonitor_Click(object sender, RoutedEventArgs e) { uint cPhysicalMonitors; var hwnd = new WindowInteropHelper(this).EnsureHandle(); IntPtr CurrentMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); //TBLog.Text += "CurMonitor: " + CurrentMonitor.ToString() + "\n"; //GetMonitorDetailedInformation(CurrentMonitor); if (GetNumberOfPhysicalMonitorsFromHMONITOR(CurrentMonitor, out cPhysicalMonitors) == false) { TBLog.Text += "GetNumberOfPhysicalMonitorsFromHMONITOR Error \n"; return; } TBLog.Text += "Number of Monitor: " + cPhysicalMonitors.ToString() + "\n"; PHYSICAL_MONITOR[] physicalMonitorArray = new PHYSICAL_MONITOR[cPhysicalMonitors]; Array.Clear(physicalMonitorArray, 0, (int)cPhysicalMonitors); if (!GetPhysicalMonitorsFromHMONITOR(CurrentMonitor, cPhysicalMonitors, physicalMonitorArray)) { TBLog.Text += "GetPhysicalMonitorsFromHMONITOR Error \n"; return; } String mystring = new String(physicalMonitorArray[0].szPhysicalMonitorDescription); TBLog.Text += "Name: " + mystring + "\n"; uint uiCapabilityies = 0, uiColorTemp = 0; if (!GetMonitorCapabilities(physicalMonitorArray[0].hPhysicalMonitor, ref uiCapabilityies, ref uiColorTemp)) { TBLog.Text += "Don't Support the MSDN provide API to get Brightness..etc. \nUse the VCP API to get the customize data\n"; } DistinguishCaps(uiCapabilityies, uiColorTemp, physicalMonitorArray); if (CBInternalFunction.IsChecked == true) { ShowInternalCaps(physicalMonitorArray[0].hPhysicalMonitor); } SendVCPCommand(physicalMonitorArray[0].hPhysicalMonitor); DestroyPhysicalMonitors(cPhysicalMonitors, physicalMonitorArray); }
private PHYSICAL_MONITOR[] GetPhysicalMonitors(IntPtr ptr) { var physicalMonitorCount = GetPhysicalMonitorCount(ptr); var monitors = new PHYSICAL_MONITOR[physicalMonitorCount]; if (GetPhysicalMonitorsFromHMONITOR(ptr, physicalMonitorCount, monitors)) { return(monitors); } var error = Marshal.GetLastWin32Error(); throw new Exception($"Cannot get phisical monitor handle! {error}"); }
public static PHYSICAL_MONITOR[] GetPhysicalMonitors(IntPtr hMonitor) { uint dwNumberOfPhysicalMonitors; if (!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, out dwNumberOfPhysicalMonitors)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } PHYSICAL_MONITOR[] physicalMonitorArray = new PHYSICAL_MONITOR[dwNumberOfPhysicalMonitors]; if (!GetPhysicalMonitorsFromHMONITOR(hMonitor, dwNumberOfPhysicalMonitors, physicalMonitorArray)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return(physicalMonitorArray); }
public static bool SetVCPFeatureByMonitor(IntPtr hMonitor, byte bVCPCode, short dwNewValue) { uint pdwNumberOfPhysicalMonitors = 0; bool res = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, ref pdwNumberOfPhysicalMonitors); if (!res || pdwNumberOfPhysicalMonitors == 0) { return(false); } var pPhysicalMonitorArray = new PHYSICAL_MONITOR[pdwNumberOfPhysicalMonitors]; GetPhysicalMonitorsFromHMONITOR(hMonitor, pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray); res = _SetVCPFeature(pPhysicalMonitorArray[0].hPhysicalMonitor, bVCPCode, dwNewValue); DestroyPhysicalMonitors(pdwNumberOfPhysicalMonitors, ref pPhysicalMonitorArray); return(res); }
private void UpdateMonitors() { DisposeMonitors(this.Monitors); var monitors = new List <MonitorInfo>(); EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData) => { uint physicalMonitorsCount = 0; if (!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, ref physicalMonitorsCount)) { // Cannot get monitor count return(true); } var physicalMonitors = new PHYSICAL_MONITOR[physicalMonitorsCount]; if (!GetPhysicalMonitorsFromHMONITOR(hMonitor, physicalMonitorsCount, physicalMonitors)) { // Cannot get physical monitor handle return(true); } foreach (PHYSICAL_MONITOR physicalMonitor in physicalMonitors) { uint minValue = 0, currentValue = 0, maxValue = 0; if (!GetMonitorBrightness(physicalMonitor.hPhysicalMonitor, ref minValue, ref currentValue, ref maxValue)) { DestroyPhysicalMonitor(physicalMonitor.hPhysicalMonitor); continue; } var info = new MonitorInfo { Handle = physicalMonitor.hPhysicalMonitor, MinValue = minValue, CurrentValue = currentValue, MaxValue = maxValue, }; monitors.Add(info); } return(true); }, IntPtr.Zero); this.Monitors = monitors; }
private static MonitorResult VerifyMonitorCapabilities(IntPtr inMonitor) { IntPtr hMonitor = inMonitor; EnumDisplayMonitors( inMonitor, inMonitor, delegate(IntPtr monitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData) { hMonitor = monitor; return(true); }, inMonitor); int lastWin32Error = Marshal.GetLastWin32Error(); uint pdwNumberOfPhysicalMonitors = 0u; bool numberOfPhysicalMonitorsFromHmonitor = GetNumberOfPhysicalMonitorsFromHMONITOR( hMonitor, ref pdwNumberOfPhysicalMonitors); lastWin32Error = Marshal.GetLastWin32Error(); PHYSICAL_MONITOR[] pPhysicalMonitorArray = new PHYSICAL_MONITOR[pdwNumberOfPhysicalMonitors]; bool physicalMonitorsFromHmonitor = GetPhysicalMonitorsFromHMONITOR( hMonitor, pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray); lastWin32Error = Marshal.GetLastWin32Error(); uint pdwMonitorCapabilities = 0u; uint pdwSupportedColorTemperatures = 0u; var monitorCapabilities = GetMonitorCapabilities( pPhysicalMonitorArray[0].hPhysicalMonitor, ref pdwMonitorCapabilities, ref pdwSupportedColorTemperatures); lastWin32Error = Marshal.GetLastWin32Error(); bool hasMonitorCapabilities = (((int)MC_CAPS_BRIGHTNESS & pdwMonitorCapabilities) > 0); MonitorResult monitorResult = new MonitorResult(); monitorResult.success = hasMonitorCapabilities; monitorResult.hMonitor = pPhysicalMonitorArray[0].hPhysicalMonitor; return(monitorResult); }
public static void SetBrightnessOffset(PHYSICAL_MONITOR physicalMonitor, double offset, double CurrentBrightness, uint dwMinimumBrightness, uint dwMaximumBrightness) { double brightness = CurrentBrightness + offset; if (brightness > 1) { brightness = 1; } else if (brightness < 0) { brightness = 0; } if (!SetMonitorBrightness(physicalMonitor.hPhysicalMonitor, (uint)(dwMinimumBrightness + (dwMaximumBrightness - dwMinimumBrightness) * brightness))) { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
private static Monitor GetMonitorInternal(IntPtr hMonitor, bool additionalRetrive = false) { var info = new MONITORINFOEX(); info.cbSize = Marshal.SizeOf(info); if (NativeMethods.GetMonitorInfo(hMonitor, ref info)) { var name = info.szDevice; if (additionalRetrive) { var cPhysicalMonitors = 0; if (NativeMethods.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, ref cPhysicalMonitors)) { var closing = false; var numberOfMonitors = 1u; var physicalMonitors = new PHYSICAL_MONITOR[numberOfMonitors]; try { if (NativeMethods.GetPhysicalMonitorsFromHMONITOR(hMonitor, numberOfMonitors, physicalMonitors)) { name = physicalMonitors[0].szPhysicalMonitorDescription; closing = true; } } finally { if (closing) { NativeMethods.DestroyPhysicalMonitors(numberOfMonitors, physicalMonitors); } } } } return(new Monitor(name, info.rcMonitor.ToRect(), info.rcWork.ToRect())); } throw new Win32Exception(Marshal.GetLastWin32Error()); }
public static bool GetContrastSupport(PHYSICAL_MONITOR physicalMonitor) { return((GetMonitorCapabilities(physicalMonitor) & MC_CAPS_CONTRAST) != 0); }
public static IEnumerable <PhysicalItem> EnumeratePhysicalMonitors(IntPtr monitorHandle) { if (!GetNumberOfPhysicalMonitorsFromHMONITOR( monitorHandle, out uint count)) { Debug.WriteLine($"Failed to get the number of physical monitors. ({Error.CreateMessage()})"); yield break; } if (count == 0) { yield break; } var physicalMonitors = new PHYSICAL_MONITOR[count]; try { if (!GetPhysicalMonitorsFromHMONITOR( monitorHandle, count, physicalMonitors)) { Debug.WriteLine($"Failed to get an array of physical monitors. ({Error.CreateMessage()})"); yield break; } int monitorIndex = 0; foreach (var physicalMonitor in physicalMonitors) { var handle = new SafePhysicalMonitorHandle(physicalMonitor.hPhysicalMonitor); bool isBrightnessSupported = GetMonitorCapabilities( handle, out MC_CAPS caps, out MC_SUPPORTED_COLOR_TEMPERATURE _) && caps.HasFlag(MC_CAPS.MC_CAPS_BRIGHTNESS); bool isLowLevel = false; if (!isBrightnessSupported) { if (GetCapabilitiesStringLength( handle, out uint capabilitiesStringLength)) { var capabilitiesString = new StringBuilder((int)capabilitiesStringLength); isLowLevel = isBrightnessSupported = CapabilitiesRequestAndCapabilitiesReply( handle, capabilitiesString, capabilitiesStringLength) && IsLowLevelBrightnessSupported(capabilitiesString.ToString()); } } //Debug.WriteLine($"Description: {physicalMonitor.szPhysicalMonitorDescription}"); //Debug.WriteLine($"Handle: {physicalMonitor.hPhysicalMonitor}"); //Debug.WriteLine($"IsBrighnessSupported: {isBrightnessSupported}"); //Debug.WriteLine($"IsLowLevel: {isLowLevel}"); yield return(new PhysicalItem( description: physicalMonitor.szPhysicalMonitorDescription, monitorIndex: monitorIndex, handle: handle, isBrightnessSupported: isBrightnessSupported, isLowLevel: isLowLevel)); monitorIndex++; } } finally { // The physical monitor handles should be destroyed at a later stage. } }
public static bool GetBrightnessSupport(PHYSICAL_MONITOR physicalMonitor) { return (GetMonitorCapabilities(physicalMonitor) & MC_CAPS_BRIGHTNESS) != 0; }
internal static extern bool DestroyPhysicalMonitors(uint dwPhysicalMonitorArraySize, ref PHYSICAL_MONITOR[] pPhysicalMonitorArray);
public static IEnumerable <PhysicalItem> EnumeratePhysicalMonitors(IntPtr monitorHandle, bool verbose = false) { if (!GetNumberOfPhysicalMonitorsFromHMONITOR( monitorHandle, out uint count)) { Debug.WriteLine($"Failed to get the number of physical monitors. {Error.GetMessage()}"); yield break; } if (count == 0) { yield break; } var physicalMonitors = new PHYSICAL_MONITOR[count]; try { if (!GetPhysicalMonitorsFromHMONITOR( monitorHandle, count, physicalMonitors)) { Debug.WriteLine($"Failed to get an array of physical monitors. {Error.GetMessage()}"); yield break; } int monitorIndex = 0; foreach (var physicalMonitor in physicalMonitors) { var handle = new SafePhysicalMonitorHandle(physicalMonitor.hPhysicalMonitor); bool isHighLevelSupported = GetMonitorCapabilities( handle, out MC_CAPS caps, out _) && caps.HasFlag(MC_CAPS.MC_CAPS_BRIGHTNESS); bool isLowLevelSupported = false; string capabilities = null; if (!isHighLevelSupported || verbose) { if (GetCapabilitiesStringLength( handle, out uint capabilitiesStringLength)) { var capabilitiesString = new StringBuilder((int)capabilitiesStringLength); if (CapabilitiesRequestAndCapabilitiesReply( handle, capabilitiesString, capabilitiesStringLength)) { capabilities = capabilitiesString.ToString(); isLowLevelSupported = IsLowLevelSupported(capabilities); } } } //Debug.WriteLine($"Description: {physicalMonitor.szPhysicalMonitorDescription}"); //Debug.WriteLine($"Handle: {physicalMonitor.hPhysicalMonitor}"); //Debug.WriteLine($"IsHighLevelSupported: {isHighLevelSupported}"); //Debug.WriteLine($"IsLowLevelSupported: {isLowLevelSupported}"); //Debug.WriteLine($"Capabilities: {capabilities}"); yield return(new PhysicalItem( description: physicalMonitor.szPhysicalMonitorDescription, monitorIndex: monitorIndex, handle: handle, isHighLevelSupported: isHighLevelSupported, isLowLevelSupported: isLowLevelSupported, capabilities: verbose ? capabilities : null)); monitorIndex++; } } finally { // The physical monitor handles should be destroyed at a later stage. } }
public static bool GetBrightnessSupport(PHYSICAL_MONITOR physicalMonitor) { return((GetMonitorCapabilities(physicalMonitor) & MC_CAPS_BRIGHTNESS) != 0); }
public static bool GetContrastSupport(PHYSICAL_MONITOR physicalMonitor) { return (GetMonitorCapabilities(physicalMonitor) & MC_CAPS_CONTRAST) != 0; }
internal static extern bool GetPhysicalMonitorsFromHMONITOR(HMONITOR hMonitor, DWORD dwPhysicalMonitorArraySize, ref PHYSICAL_MONITOR pPhysicalMonitorArray);
internal static extern bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, int physicalMonitorArraySize, ref PHYSICAL_MONITOR physicalMonitorArray);
internal static extern bool DestroyPhysicalMonitors(int physicalMonitorArraySize, ref PHYSICAL_MONITOR physicalMonitorArray);
private void EvBrightnessChange(object sender, EventArrivedEventArgs e) { var arrhMonitor = new List <IntPtr>(); EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData) => { arrhMonitor.Add(hMonitor); return(true); }, IntPtr.Zero); var arrPhysicalMonitor = new List <PHYSICAL_MONITOR>(); foreach (var hMonitor in arrhMonitor) { uint sz; if (!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, out sz)) { var lastError = Marshal.GetLastWin32Error(); Text.PushTextError("BrightnessSync::GetNumberOfPhysicalMonitorsFromHMONITOR failed (0x" + lastError.ToString("X8") + ": " + new Win32Exception(lastError).Message + ")"); return; } PHYSICAL_MONITOR[] pPhysicalMonitorArray = new PHYSICAL_MONITOR[sz]; if (!GetPhysicalMonitorsFromHMONITOR(hMonitor, sz, pPhysicalMonitorArray)) { var lastError = Marshal.GetLastWin32Error(); Text.PushTextError("BrightnessSync::GetPhysicalMonitorsFromHMONITOR failed (0x" + lastError.ToString("X8") + ": " + new Win32Exception(lastError).Message + ")"); return; } arrPhysicalMonitor.AddRange(pPhysicalMonitorArray); } var brightnessPercent = (byte)e.NewEvent.Properties["Brightness"].Value; foreach (var physicalMonitor in arrPhysicalMonitor) { /*MONITOR_CAPABILITIES caps; * MONITOR_SUPPORTED_COLOR_TEMPERATURE temp; * if (!GetMonitorCapabilities(physicalMonitor.hPhysicalMonitor, out caps, out temp)) { * var lastError = Marshal.GetLastWin32Error(); * Text.PushTextError("BrightnessSync::GetMonitorCapabilities failed (0x" + lastError.ToString("X8") + ": " + new Win32Exception(lastError).Message + ")"); * continue; * } * * if (!caps.HasFlag(MONITOR_CAPABILITIES.MC_CAPS_BRIGHTNESS)) { * Text.PushText("Monitor " + physicalMonitor.szPhysicalMonitorDescription + " (0x" + physicalMonitor.hPhysicalMonitor.ToString("X8") + ") doesn't support brightness set/get", ConsoleColor.DarkYellow); * continue; * }*/ uint minBrightness, curBrightness, maxBrightness; if (!GetMonitorBrightness(physicalMonitor.hPhysicalMonitor, out minBrightness, out curBrightness, out maxBrightness)) { var lastError = Marshal.GetLastWin32Error(); Text.PushTextError("BrightnessSync::GetMonitorBrightness failed (0x" + lastError.ToString("X8") + ": " + new Win32Exception(lastError).Message + ")"); continue; } if (!SetMonitorBrightness(physicalMonitor.hPhysicalMonitor, (uint)(brightnessPercent * ((float)maxBrightness / 100)))) { var lastError = Marshal.GetLastWin32Error(); Text.PushTextError("BrightnessSync::SetMonitorBrightness failed (0x" + lastError.ToString("X8") + ": " + new Win32Exception(lastError).Message + ")"); continue; } Text.ImportantMessageAddTimeout(physicalMonitor.szPhysicalMonitorDescription + " -> " + (uint)(brightnessPercent * ((float)maxBrightness / 100)), TimeSpan.FromMilliseconds(500)); //Text.PushTextNormal(physicalMonitor.szPhysicalMonitorDescription + " -> min: " + minBrightness + ", cur: " + curBrightness + ", max: " + maxBrightness); } if (!DestroyPhysicalMonitors((uint)arrPhysicalMonitor.Count, arrPhysicalMonitor.ToArray())) { var lastError = Marshal.GetLastWin32Error(); Text.PushTextError("BrightnessSync::DestroyPhysicalMonitors failed (0x" + lastError.ToString("X8") + ": " + new Win32Exception(lastError).Message + ")"); } }
private static uint GetMonitorCapabilities(PHYSICAL_MONITOR physicalMonitor) { uint dwMonitorCapabilities, dwSupportedColorTemperatures; if (!GetMonitorCapabilities(physicalMonitor.hPhysicalMonitor, out dwMonitorCapabilities, out dwSupportedColorTemperatures)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return dwMonitorCapabilities; }