#371 Add Create method for types MONITORINFO and MONITORINFOEX (#372)

* #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 <peter.sulek@synotgames.com>
pull/375/head
Peter Šulek 2023-01-29 02:44:24 +01:00 committed by GitHub
parent 93ccf3ca34
commit 8ed6e0402d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 1 deletions

View File

@ -514,6 +514,7 @@ namespace Vanara.PInvoke
/// <para>
/// You must set the <c>cbSize</c> member of the structure to sizeof(MONITORINFO) or sizeof(MONITORINFOEX) before calling the
/// <c>GetMonitorInfo</c> function. Doing so lets the function determine the type of structure you are passing to it.
/// You can do this by calling <see cref="MONITORINFO.Create"/> to create new instance with properly set of <see cref="MONITORINFO.cbSize"/>.
/// </para>
/// <para>
/// 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
/// <para>
/// You must set the <c>cbSize</c> member of the structure to sizeof(MONITORINFO) or sizeof(MONITORINFOEX) before calling the
/// <c>GetMonitorInfo</c> function. Doing so lets the function determine the type of structure you are passing to it.
/// You can do this by calling <see cref="MONITORINFOEX.Create"/> to create new instance with properly set of <see cref="MONITORINFOEX.cbSize"/>.
/// </para>
/// <para>
/// 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
/// </list>
/// </summary>
public MonitorInfoFlags dwFlags;
/// <summary>
/// Creates new instance of <see cref="MONITORINFO"/> structure
/// </summary>
/// <returns>
/// Returns new instance of properly initialized <see cref="MONITORINFO"/> structure.
/// </returns>
/// <seealso cref="User32.GetMonitorInfo(Vanara.PInvoke.HMONITOR,ref Vanara.PInvoke.User32.MONITORINFO)"/>
public static MONITORINFO Default => new()
{
cbSize = (uint)Marshal.SizeOf(typeof(MONITORINFO))
};
}
/// <summary>
@ -809,6 +823,18 @@ namespace Vanara.PInvoke
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string szDevice;
}
/// <summary>
/// Creates new instance of <see cref="MONITORINFOEX"/> structure
/// </summary>
/// <returns>
/// Returns new instance of properly initialized <see cref="MONITORINFOEX"/> structure.
/// </returns>
/// <seealso cref="User32.GetMonitorInfo(Vanara.PInvoke.HMONITOR,ref Vanara.PInvoke.User32.MONITORINFOEX)"/>
public static MONITORINFOEX Default => new()
{
cbSize = (uint)Marshal.SizeOf(typeof(MONITORINFOEX))
};
}
}
}

View File

@ -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<IntPtr>();
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.");
}
}
}
}
}
}