Enumerating Monitors on Windows in C#

Most of readers of this blog post probably never have been working with Windowsds native API implemented long time ago inside User32 and related DLLs. But, as a professional application developer you migth have to deal sometimes with APIs, that are no more used every day in a life of new software developers. If you feel lost looking for a solution grabbed in secrets of Windows, this post might help you. The problem I want to solve here sounds very simple. Imagine you want to paint text inside a native window, rendered differently than your WPF, Windows Forms, and MAUI application renders it. In that case, you will have to look for help inside User32.Dll. For example, assume you want to enlist all monitors in the system currently connected to your PC. The following code demonstrates the correct C# decaration of all required Windows native functions and structures required to enlist all attached displays (monitors). /// <summary> /// Gets the list of monitors. /// </summary> static Dictionary<string, MONITORINFOEX> GetMonitors() { EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, **MonitorEnumProc**, IntPtr.Zero); foreach (var item in monitors) { Debug.WriteLine($ {item.Value.szDevice}, Resolution: {item.Value.rcMonitor.Right - item.Value.rcMonitor.Left}, {item.Value.rcMonitor.Left- item.Value.rcMonitor.Top}, Pos: {item.Value.rcMonitor.Left},{item.Value.rcMonitor.Top} ); } return monitors; } /// <summary> /// Callback during listing of monitors. Invoked for ever attached display. /// After last invoke, the method EnumDisplayMonitors returns. /// </summary> private static bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdcMonitor, ref Rectangle lprcMonitor, IntPtr dwData) { MONITORINFOEX monitorInfo = new MONITORINFOEX(); monitorInfo.cbSize = Marshal.SizeOf(typeof(MONITORINFOEX)); bool res = GetMonitorInfo(hMonitor, ref monitorInfo); if (res == false) { var err = Marshal.GetLastWin32Error(); } return true; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 4)] public struct MONITORINFOEX { public int cbSize; public Rectangle rcMonitor; public Rectangle rcWork; public uint dwFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string szDevice; } [DllImport( user32.dll )] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, EnumMonitorsDelegate lpfnEnum, IntPtr dwData);

zum Artikel gehen

Multi-Windows-Installation-Stick MDT Windows 10 Apps

Hallo Leser, heute möchte ich gerne mal wieder einige Worte zum Deployment loswerden. Aufmerksame Leser kennen meinen Beitrag Multi-Windows-Installation-Stick mit Microsoft Deployment Toolkit ! Mit meinem heutigen Beitrag möchte ich gerne daran anschließe

zum Artikel gehen

Windows Server 2016 Evaluierungsversion verfügbar

Hallo Server-Freunde, Microsoft hat heute offiziell Windows Server 2016 als Evaluierungsversion veröffentlicht. I’m incredibly excited that this morning at our Ignite conference in Atlanta we launched the newest release of our server operating system – Wi

zum Artikel gehen

Einfache Migration auf Windows 7 mit Beno 4.0 und DX-UNION Schulserver

Migrieren Sie einfach und in gemischten Systemen von Windows 2000 oder XP nach Windows 7 mit beno 4 und DX-UNION Schulserver. Die neue Version beno 4.0 vereinfacht dies durch die eine bessere Integration der Softwarepaketadministration. Weisen Sie direkt

zum Artikel gehen

Server 2012 R2: DirectAccess mit Windows 7, 8.1 10

Hallo Serverfreunde, heute melde ich mich mal wieder mit dem Remotezugriff. Es geht um DirectAccess, wie ich finde, eines der besten Features die mit Windows Server 2008 R2 eingeführt worden ist. Mit Windows Server 2012 (R2) hat die DirectAccess Rolle noc

zum Artikel gehen

Windows Server 2016 TP5 veröffentlicht!

Hallo Serverfreunde! Microsoft hat Windows Server 2016 TP5 veröffentlicht. Für alle Interessierten steht der Download nun über die bekannten Vertriebswege zur Verfügung. Server 2016 TP5 Der Download umfasst 4,83 GB und steht in den meist verwendeten Sp

zum Artikel gehen