From 8ed6e0402d3d55c762423d75ba84eb037f748125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20=C5=A0ulek?= Date: Sun, 29 Jan 2023 02:44:24 +0100 Subject: [PATCH] #371 Add Create method for types MONITORINFO and MONITORINFOEX (#372) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #371 Add Create method for types MONITORINFO and MONITORINFOEX * fixed CodeFactor issues * #371 Add Create method for types MONITORINFO and MONITORINFOEX * fixed CodeFactor issues * changed Create methods to Default property * fixed MONITORINFOEX.Create() with MONITORINFOEX.Default --------- Co-authored-by: Peter Šulek --- PInvoke/User32/WinUser.Monitor.cs | 28 ++++++++++++- UnitTests/PInvoke/User32/User32MonitorTests.cs | 58 ++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 UnitTests/PInvoke/User32/User32MonitorTests.cs diff --git a/PInvoke/User32/WinUser.Monitor.cs b/PInvoke/User32/WinUser.Monitor.cs index a7478df8..296e174e 100644 --- a/PInvoke/User32/WinUser.Monitor.cs +++ b/PInvoke/User32/WinUser.Monitor.cs @@ -514,6 +514,7 @@ namespace Vanara.PInvoke /// /// You must set the cbSize member of the structure to sizeof(MONITORINFO) or sizeof(MONITORINFOEX) before calling the /// GetMonitorInfo function. Doing so lets the function determine the type of structure you are passing to it. + /// You can do this by calling to create new instance with properly set of . /// /// /// The MONITORINFOEX structure is a superset of the MONITORINFO structure. It has one additional member: a string that contains a @@ -543,6 +544,7 @@ namespace Vanara.PInvoke /// /// You must set the cbSize member of the structure to sizeof(MONITORINFO) or sizeof(MONITORINFOEX) before calling the /// GetMonitorInfo function. Doing so lets the function determine the type of structure you are passing to it. + /// You can do this by calling to create new instance with properly set of . /// /// /// The MONITORINFOEX structure is a superset of the MONITORINFO structure. It has one additional member: a string that contains a @@ -744,6 +746,18 @@ namespace Vanara.PInvoke /// /// public MonitorInfoFlags dwFlags; + + /// + /// Creates new instance of structure + /// + /// + /// Returns new instance of properly initialized structure. + /// + /// + public static MONITORINFO Default => new() + { + cbSize = (uint)Marshal.SizeOf(typeof(MONITORINFO)) + }; } /// @@ -809,6 +823,18 @@ namespace Vanara.PInvoke /// [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string szDevice; - } + + /// + /// Creates new instance of structure + /// + /// + /// Returns new instance of properly initialized structure. + /// + /// + public static MONITORINFOEX Default => new() + { + cbSize = (uint)Marshal.SizeOf(typeof(MONITORINFOEX)) + }; + } } } \ No newline at end of file diff --git a/UnitTests/PInvoke/User32/User32MonitorTests.cs b/UnitTests/PInvoke/User32/User32MonitorTests.cs new file mode 100644 index 00000000..34c55793 --- /dev/null +++ b/UnitTests/PInvoke/User32/User32MonitorTests.cs @@ -0,0 +1,58 @@ +using NUnit.Framework; +using System; +using System.Collections.Generic; +using static Vanara.PInvoke.User32; + +namespace Vanara.PInvoke.Tests +{ + [TestFixture()] + public partial class User32Tests + { + [Test] + public void EnumDisplayMonitorsTest() + { + var hdc = new HDC(); + var monitors = new List(); + bool result = EnumDisplayMonitors(hdc, null, (monitor, _, _, _) => + { + monitors.Add(monitor); + return true; + }, IntPtr.Zero); + + Assert.IsTrue(result, "Error calling EnumDisplayMonitors()"); + Assert.IsNotEmpty(monitors, "Calling EnumDisplayMonitors() returns empty list"); + + TestContext.WriteLine($"EnumDisplayMonitors() returned {monitors.Count} monitor(s)"); + + var primaryDisplayHandle = IntPtr.Zero; + foreach (var monitor in monitors) + { + var info = MONITORINFOEX.Default; + bool getInfoResult = GetMonitorInfo(monitor, ref info); + Assert.IsTrue(getInfoResult, $"Error calling GetMonitorInfo(h: {monitor}) returned: {getInfoResult}"); + + Assert.IsFalse(info.rcMonitor.IsEmpty, $"Bounds of monitor(handle: {monitor}) are empty!"); + + var isPrimary = (info.dwFlags & MonitorInfoFlags.MONITORINFOF_PRIMARY) != 0; + + TestContext.WriteLine( + $"handle: {monitor}, name: {info.szDevice}, flags: {info.dwFlags}, isPrimary: {isPrimary}," + + $" bounds: {info.rcMonitor}, workarea: {info.rcWork}"); + + if (isPrimary) + { + if (primaryDisplayHandle == IntPtr.Zero) + { + primaryDisplayHandle = monitor; + } + else + { + Assert.Fail( + $"Error calling GetMonitorInfo(h: {monitor}) returned flag that this monitor is primary, " + + $"but there is already another monitor (handle: {primaryDisplayHandle}) with flag primary."); + } + } + } + } + } +} \ No newline at end of file