Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Nielsen committed Sep 19, 2024
1 parent 2af2bbe commit 2ce2301
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 384 deletions.
30 changes: 20 additions & 10 deletions src/WinUIEx/MonitorInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Windows.Foundation;
using System.Collections.Generic;
using Windows.Win32;
using System;

namespace WinUIEx
{
Expand All @@ -18,27 +19,36 @@ public class MonitorInfo
/// <returns>A list of display monitors</returns>
public unsafe static IList<MonitorInfo> GetDisplayMonitors()
{

int monitorCount = PInvoke.GetSystemMetrics(Windows.Win32.UI.WindowsAndMessaging.SYSTEM_METRICS_INDEX.SM_CMONITORS);
List<MonitorInfo> list = new List<MonitorInfo>(monitorCount);
MONITORENUMPROC callback = new MONITORENUMPROC((HMONITOR monitor, HDC deviceContext, RECT* rect, LPARAM data) =>
{
list.Add(new MonitorInfo(monitor, rect));
return true;
});
LPARAM data = new LPARAM();
bool ok = PInvoke.EnumDisplayMonitors(new HDC(0), (RECT?)null, callback, data);
var cbhandle = GCHandle.Alloc(list);
var ptr = GCHandle.ToIntPtr(cbhandle);

LPARAM data = new LPARAM(ptr);
bool ok = PInvoke.EnumDisplayMonitors(new HDC(0), (RECT?)null, &MonitorEnumProc, data);
cbhandle.Free();
if (!ok)
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
return list;
}

[UnmanagedCallersOnly(CallConvs = new Type[] { typeof(System.Runtime.CompilerServices.CallConvStdcall) })]
private unsafe static BOOL MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, RECT* lprcMonitor, LPARAM dwData)
{
var handle = GCHandle.FromIntPtr(dwData.Value);
if(!lprcMonitor->IsEmpty && handle.IsAllocated && handle.Target is List<MonitorInfo> list)
list.Add(new MonitorInfo(hMonitor, *lprcMonitor));
return new BOOL(1);
}

private readonly HMONITOR _monitor;

internal unsafe MonitorInfo(HMONITOR monitor, RECT* rect)
internal unsafe MonitorInfo(HMONITOR monitor, RECT rect)
{
RectMonitor =
new Rect(new Point(rect->left, rect->top),
new Point(rect->right, rect->bottom));
new Rect(new Point(rect.left, rect.top),
new Point(rect.right, rect.bottom));
_monitor = monitor;
var info = new MONITORINFOEXW() { monitorInfo = new MONITORINFO() { cbSize = (uint)sizeof(MONITORINFOEXW) } };
GetMonitorInfo(monitor, ref info);
Expand Down
Loading

0 comments on commit 2ce2301

Please sign in to comment.