private static extern bool EnumDisplayMonitors (HandleRef hdc, IntPtr rcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
/// <summary> /// 刷新容器 /// </summary> public void RefreshActualScreens() { try { ActualScreens.Clear(); MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref Rect prect, int d) => { ActualScreens.Add(new MonitorInfo() { Bounds = new Rectangle() { X = prect.left, Y = prect.top, Width = prect.right - prect.left, Height = prect.bottom - prect.top, }, IsPrimary = (prect.left == 0) && (prect.top == 0), }); Hashtable hs = new Hashtable(); hs.Add("ip", GetSystemInfo.GetClientLocalIPv4Address()); RZMRequest.Get.QueryGetWebService("http://192.168.13.97:8080/GxgyService.asmx", "GetBreakScreen", hs); return(true); }; EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0); } catch (Exception ex) { LogHelper.WriteLog(ex); } }
public static int GetMonitorCount() { allScreens.Clear(); int monCount = 0; MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref ScreenRect prect, int d) => { Console.WriteLine("Left {0}", prect.left); Console.WriteLine("Right {0}", prect.right); Console.WriteLine("Top {0}", prect.top); Console.WriteLine("Bottom {0}", prect.bottom); allScreens.AddLast(new WpfScreen(prect)); return(++monCount > 0); }; if (EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0)) { Console.WriteLine("You have {0} monitors", monCount); } else { Console.WriteLine("An error occured while enumerating monitors"); } return(monCount); }
public static List <Monitors> GetAllMonitors() { List <Monitors> physicalMonitors = new List <Monitors>(); MonitorEnumProc callback = (MonitorEnumProc)((IntPtr hDesktop, IntPtr hdc, ref HNStruct.VCPRect r, int d) => { uint pdwNumberOfPhysicalMonitors = 0; if (!GetNumberOfPhysicalMonitorsFromHMONITOR(hDesktop, ref pdwNumberOfPhysicalMonitors)) { throw new Exception("无法获取显示器个数"); } HNStruct.PHYSICAL_MONITOR[] pPhysicalMonitorArray = new HNStruct.PHYSICAL_MONITOR[(int)pdwNumberOfPhysicalMonitors]; if (!GetPhysicalMonitorsFromHMONITOR(hDesktop, pdwNumberOfPhysicalMonitors, pPhysicalMonitorArray)) { throw new Exception("无法获取显示器句柄"); } physicalMonitors.Add(GetMonitorCapabilities(new Monitors() { PhysicalMonitor = pPhysicalMonitorArray[0] })); return(true); }); EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0); return(physicalMonitors); }
static DisplayCounter() { windowPointers = new List <IntPtr>(); callback = (IntPtr hDesktop, IntPtr hdc, ref Rect prect, int d) => { windowPointers.Add(hDesktop); return(++monCount > 0); }; }
public static IEnumerable <NativeDisplay> GetAllDisplays() { DisplayEnumCallback closure = new DisplayEnumCallback(); MonitorEnumProc proc = new MonitorEnumProc(closure.Callback); EnumDisplayMonitorsNative(new HandleRef(null, IntPtr.Zero), IntPtr.Zero, proc, IntPtr.Zero); return(closure.Displays.Cast <NativeDisplay>()); }
private static int DetectMonitorCount() { int monCount = 0; MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref Rect prect, int d) => ++ monCount > 0; EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0); return(monCount); }
public static Rect GetLeftMonitorCoords() { Rect res = new Rect(); var list = new List <Rect>(); MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref Rect prect, int d) => { list.Add(prect); return(true); }; if (EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0)) { res = list.OrderBy(p => p.left).ThenBy(p => p.top).First(); } return(res); }
static Screen() { var numOfMonitors = GetSystemMetrics(SM_CMONITORS); AllScreens = new Screen[numOfMonitors]; var dc = GetDC(new HandleRef(null, IntPtr.Zero)); var href = new HandleRef(null, dc); MonitorEnumProc proc = MonEnumProc; bool res = EnumDisplayMonitors(href, IntPtr.Zero, proc, IntPtr.Zero); PrimaryScreen = AllScreens.First((S) => S.Primary); }
// This will return the total number of Displays that windows is drawing. Meaning that two Mirrored monitors will report as a single display private List <IntPtr> GetMonitorHandles() { var hMonitors = new List <IntPtr>(); MonitorEnumProc callback = (IntPtr hMonitor, IntPtr hdc, ref Rect prect, int d) => { hMonitors.Add(hMonitor); return(true); }; EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0); return(hMonitors); }
public static IEnumerable <NativeDisplay> GetAllDisplays() { ArrayList displays = new ArrayList(); MonitorEnumProc proc = new MonitorEnumProc((IntPtr hMonitor, IntPtr hdcMonitor, IntPtr lprcMonitor, IntPtr dwData) => { displays.Add(new NativeDisplay(hMonitor)); return(true); }); EnumDisplayMonitorsNative(new HandleRef(null, IntPtr.Zero), IntPtr.Zero, proc, IntPtr.Zero); return(displays.Cast <NativeDisplay>()); }
public static void Main() { int monCount = 0; Rect r = new Rect(); MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref Rect prect, int d) => ++ monCount > 0; if (EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0)) { Console.WriteLine("You have {0} monitors", monCount); } else { Console.WriteLine("An error occured while enumerating monitors"); } }
public static int GetMonitorCount() { int monCount = 0; MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref ScreenRect prect, int d) => ++ monCount > 0; if (EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0)) { Console.WriteLine("You have {0} monitors", monCount); } else { Console.WriteLine("An error occured while enumerating monitors"); } return(monCount); }
/// <summary> /// 获取到所有的显示器信息 /// </summary> private static void GetMonitors() { if (primaryPtr == IntPtr.Zero) { primaryPtr = MonitorFromWindow(IntPtr.Zero, MONITOR_DEFAULTTOPRIMARY); } if (m_monitorInfos == null || m_monitorInfos.Count == 0) { MonitorEnumProc callback = MonitorEnumProcCallBack; if (EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0)) { UnityEngine.Debug.Log(string.Format("You have {0} monitors", monitorInfos.Count.ToString())); } else { UnityEngine.Debug.Log("An error occured while enumerating monitors"); } } }
public static void RefreshActualScreens() { ActualScreens.Clear(); MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref Rect prect, int d) => { ActualScreens.Add(new MonitorInfo() { Bounds = new Rectangle() { X = prect.left, Y = prect.top, Width = prect.right - prect.left, Height = prect.bottom - prect.top, }, IsPrimary = (prect.left == 0) && (prect.top == 0), }); return(true); }; EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0); }
/// <summary> /// Loops through all connected monitors and caches their display information /// into an array /// </summary> /// <returns>Display array</returns> public static Display[] GetDisplays() { List <Display> displays = new List <Display>(); MonitorEnumProc callback = (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, int d) => { MonitorInfoEx info = new MonitorInfoEx(); info.Size = Marshal.SizeOf(info); if (User32Interop.GetMonitorInfo(hMonitor, ref info)) { Rectangle r = lprcMonitor.ToRectangle(); Display display = new Display(hMonitor, r, info.DeviceName, true); displays.Add(display); } return(true); }; bool result = User32Interop.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0); if (result) { return(displays.ToArray()); } return(null); }
public static extern bool EnumDisplayMonitors(System.Runtime.InteropServices.HandleRef hdc, COMRECT rcClip, MonitorEnumProc lpfnEnum, System.IntPtr dwData);
private static extern bool EnumDisplayMonitors(HandleRef hdc, COMRECT rcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
private static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, [MarshalAs(UnmanagedType.FunctionPtr)] MonitorEnumProc lpfnEnum, IntPtr dwData);
public static extern int EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
public static extern bool EnumDisplayMonitors(IntPtr hdc, COMRECT rcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
public void TEST() { List <(IntPtr ptr, Rect rect, int d)> hMonitors = new List <(IntPtr, Rect, int)>(); MonitorEnumProc callback = (IntPtr hDesktop, IntPtr hdc, ref Rect prect, int d) => { hMonitors.Add((hDesktop, prect, d)); return(true); }; List <PHYSICAL_MONITOR> monitors = new List <PHYSICAL_MONITOR>(); if (EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0)) { foreach (var m in hMonitors) { uint mcount = 0; if (!GetNumberOfPhysicalMonitorsFromHMONITOR(m.ptr, ref mcount)) { throw new Exception("Cannot get monitor count!"); } PHYSICAL_MONITOR[] physicalMonitors = new PHYSICAL_MONITOR[mcount]; if (!GetPhysicalMonitorsFromHMONITOR(m.ptr, mcount, physicalMonitors)) { throw new Exception("Cannot get phisical monitor handle!"); } Debug.WriteLine($"PM:{physicalMonitors.Length}) RECT: T:{m.rect.top}/L:{m.rect.left}/R:{m.rect.right}/B:{m.rect.bottom} SCALE:{m.d}"); monitors.AddRange(physicalMonitors); } for (int i = 0; i < monitors.Count; i++) { uint cv = 0, mv = 0; if (GetVCPFeatureAndVCPFeatureReply(monitors[i].hPhysicalMonitor, SVC_FEATURE__POWER_MODE, IntPtr.Zero, ref cv, ref mv)) { Debug.WriteLine($"{i}) {monitors[i].hPhysicalMonitor} + {monitors[i].szPhysicalMonitorDescription} + `{cv}/{mv}`"); } else { string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message; Debug.WriteLine($"{i}) ERROR: {errorMessage}"); // TODO: use HighLevel API to set brightness on these monitors // https://docs.microsoft.com/en-us/windows/win32/api/highlevelmonitorconfigurationapi/nf-highlevelmonitorconfigurationapi-setmonitorbrightness } } // call power //:: Current value> 1=On / 2=Standby / 3=Suspended / 4=Off / 5=OffByButton MonitorSwitch(monitors[2], PowerModeEnum.PowerOn); // <<<< OK but doesn't work on laptop screen //MonitorBrightness(monitors[0], 60); // <<<< OK but doesn't work on laptop screen PHYSICAL_MONITOR[] toDestroy = monitors.ToArray(); DestroyPhysicalMonitors((uint)toDestroy.Length, ref toDestroy); } else { //error } }
internal static extern bool EnumDisplayMonitors( nint hdc, nint lpRect, MonitorEnumProc callback, nint dwData);
public static extern bool EnumDisplayMonitors(HandleRef hdc, COMRECT rcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
public static extern bool EnumDisplayMonitors( IntPtr hdc, ref Rect a, MonitorEnumProc lpfnEnum, object dwData );
unsafe internal static extern bool EnumDisplayMonitors( [In] IntPtr deviceContextHandle, [In] Rect *clipRect, [In, MarshalAs(UnmanagedType.FunctionPtr)] MonitorEnumProc callback, [In] IntPtr data );
static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lpRect, MonitorEnumProc callback, int dwData);
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumProc lpfnEnum, int dwData);
internal static extern bool EnumDisplayMonitors( HandleRef hdc, IntPtr rcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lpRect, MonitorEnumProc callback, int dwData);
public static extern bool EnumDisplayMonitors(HDC hdc, PRECT lprcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
public void Initialize() { monitors = new List <Monitor>(); physicalMonitors = new List <PHYSICAL_MONITOR>(); List <(IntPtr monitor, Rect rect)> hMonitors = new List <(IntPtr, Rect)>(); MonitorEnumProc callback = (IntPtr hMonitor, IntPtr hdc, ref Rect prect, int d) => { monitors.Add(new Monitor { hMonitor = hMonitor, rect = prect, }); return(true); }; if (EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0)) { foreach (var m in monitors) { uint mcount = 0; if (!GetNumberOfPhysicalMonitorsFromHMONITOR(m.hMonitor, ref mcount)) { throw new Exception("Cannot get monitor count!"); } PHYSICAL_MONITOR[] physicalMonitors = new PHYSICAL_MONITOR[mcount]; if (!GetPhysicalMonitorsFromHMONITOR(m.hMonitor, mcount, physicalMonitors)) { throw new Exception("Cannot get phisical monitor handle!"); } Debug.WriteLine($"PM:{physicalMonitors.Length}) RECT: T:{m.rect.Top}/L:{m.rect.Left}/R:{m.rect.Right}/B:{m.rect.Bottom}"); this.physicalMonitors.AddRange(physicalMonitors); m.physicalMonitors = physicalMonitors.Select(a => new PhysicalMonitor { DeviceName = a.szPhysicalMonitorDescription, hPhysicalMonitor = a.hPhysicalMonitor }).ToList(); } foreach (var p in monitors.SelectMany(a => a.physicalMonitors)) { uint cv = 0; // power if (GetFeatureValue(p.hPhysicalMonitor, SVC_FEATURE__POWER_MODE, ref cv)) { p.IsPoweredOn = (cv == (uint)PowerModeEnum.PowerOn); Debug.WriteLine($"{p.hPhysicalMonitor} + {p.DeviceName} + POWER={cv}"); p.IsEnabled = true; } else { string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message; Debug.WriteLine($"ERROR for {p.DeviceName}: `{errorMessage}`"); } // BRIG if (GetFeatureValue(p.hPhysicalMonitor, SVC_FEATURE__BRIGHTNESS, ref cv)) { p.BrightnessLevel = cv; Debug.WriteLine($"{p.hPhysicalMonitor} + {p.DeviceName} + BRIGHTNESS={cv}"); } else { string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message; Debug.WriteLine($"ERROR for {p.DeviceName}: `{errorMessage}`"); } } } }
private static extern bool EnumDisplayMonitors( IntPtr hdc, IntPtr lprcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
/// <summary> /// Loops through all connected monitors and caches their display information /// into an array /// </summary> /// <returns>Display array</returns> public static Display[] GetDisplays() { List <Display> displays = new List <Display>(); MonitorEnumProc callback = (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, int d) => { MonitorInfoEx info = new MonitorInfoEx(); info.Size = Marshal.SizeOf(info); if (User32Interop.GetMonitorInfo(hMonitor, ref info)) { Rectangle r = lprcMonitor.ToRectangle(); DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE(); displayDevice.cb = Marshal.SizeOf(displayDevice); const int EDD_GET_DEVICE_INTERFACE_NAME = 0x1; uint deviceIndex = 0; string deviceID = string.Empty; string deviceString = string.Empty; string monitorID = string.Empty; int displayIndex = -1; int monitorIndex = -1; while (EnumDisplayDevices(null, deviceIndex, ref displayDevice, 0)) { if (info.DeviceName == displayDevice.DeviceName) { DISPLAY_DEVICE monDisplayDevice = new DISPLAY_DEVICE(); monDisplayDevice.cb = Marshal.SizeOf(monDisplayDevice); uint monitorNum = 0; EnumDisplayDevices(displayDevice.DeviceName, monitorNum, ref monDisplayDevice, EDD_GET_DEVICE_INTERFACE_NAME); try { deviceID = monDisplayDevice.DeviceID; deviceString = monDisplayDevice.DeviceString; monitorID = deviceID.Substring(deviceID.IndexOf("DISPLAY#") + 8, 7); Int32.TryParse(info.DeviceName.Substring(info.DeviceName.IndexOf("DISPLAY") + 7), out displayIndex); Int32.TryParse( monDisplayDevice.DeviceName.Substring( monDisplayDevice.DeviceName.IndexOf("Monitor") + 7), out monitorIndex); monitorNum++; break; } catch (Exception) { } } deviceIndex++; } Display display = new Display(hMonitor, r, info.DeviceName, true, deviceID, deviceString, monitorID, displayIndex, monitorIndex); displays.Add(display); } return(true); }; //deviceIndex = 0; //while() //{ // LogManager.Log(displayDevice.DeviceID); // deviceIndex++; //} bool result = User32Interop.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0); if (result) { return(displays.ToArray()); } return(null); }