11using System . Collections . Generic ;
2- using System ;
32using System . Runtime . InteropServices ;
43using System . Windows ;
54using Windows . Win32 ;
65using Windows . Win32 . Foundation ;
76using Windows . Win32 . Graphics . Gdi ;
87using Windows . Win32 . UI . WindowsAndMessaging ;
98
10- namespace Flow . Launcher . Infrastructure ;
9+ namespace Flow . Launcher . Plugin . SharedModels ;
1110
1211/// <summary>
1312/// Contains full information about a display monitor.
14- /// Codes are edited from: <see href=" https://github.com/Jack251970/DesktopWidgets3"> .
13+ /// Inspired from: https://github.com/Jack251970/DesktopWidgets3.
1514/// </summary>
16- internal class MonitorInfo
15+ /// <remarks>
16+ /// Use this class to replace the System.Windows.Forms.Screen class which can cause possible System.PlatformNotSupportedException.
17+ /// </remarks>
18+ public class MonitorInfo
1719{
1820 /// <summary>
1921 /// Gets the display monitors (including invisible pseudo-monitors associated with the mirroring drivers).
@@ -23,14 +25,14 @@ public static unsafe IList<MonitorInfo> GetDisplayMonitors()
2325 {
2426 var monitorCount = PInvoke . GetSystemMetrics ( SYSTEM_METRICS_INDEX . SM_CMONITORS ) ;
2527 var list = new List < MonitorInfo > ( monitorCount ) ;
26- var callback = new MONITORENUMPROC ( ( HMONITOR monitor , HDC deviceContext , RECT * rect , LPARAM data ) =>
28+ var callback = new MONITORENUMPROC ( ( monitor , deviceContext , rect , data ) =>
2729 {
2830 list . Add ( new MonitorInfo ( monitor , rect ) ) ;
2931 return true ;
3032 } ) ;
3133 var dwData = new LPARAM ( ) ;
3234 var hdc = new HDC ( ) ;
33- bool ok = PInvoke . EnumDisplayMonitors ( hdc , ( RECT ? ) null , callback , dwData ) ;
35+ bool ok = PInvoke . EnumDisplayMonitors ( hdc , null , callback , dwData ) ;
3436 if ( ! ok )
3537 {
3638 Marshal . ThrowExceptionForHR ( Marshal . GetLastWin32Error ( ) ) ;
@@ -43,11 +45,11 @@ public static unsafe IList<MonitorInfo> GetDisplayMonitors()
4345 /// </summary>
4446 /// <param name="hwnd">Window handle</param>
4547 /// <returns>The display monitor that is nearest to a given window, or null if no monitor is found.</returns>
46- public static unsafe MonitorInfo GetNearestDisplayMonitor ( HWND hwnd )
48+ public static unsafe MonitorInfo GetNearestDisplayMonitor ( nint hwnd )
4749 {
48- var nearestMonitor = PInvoke . MonitorFromWindow ( hwnd , MONITOR_FROM_FLAGS . MONITOR_DEFAULTTONEAREST ) ;
50+ var nearestMonitor = PInvoke . MonitorFromWindow ( new ( hwnd ) , MONITOR_FROM_FLAGS . MONITOR_DEFAULTTONEAREST ) ;
4951 MonitorInfo nearestMonitorInfo = null ;
50- var callback = new MONITORENUMPROC ( ( HMONITOR monitor , HDC deviceContext , RECT * rect , LPARAM data ) =>
52+ var callback = new MONITORENUMPROC ( ( monitor , deviceContext , rect , data ) =>
5153 {
5254 if ( monitor == nearestMonitor )
5355 {
@@ -58,25 +60,83 @@ public static unsafe MonitorInfo GetNearestDisplayMonitor(HWND hwnd)
5860 } ) ;
5961 var dwData = new LPARAM ( ) ;
6062 var hdc = new HDC ( ) ;
61- bool ok = PInvoke . EnumDisplayMonitors ( hdc , ( RECT ? ) null , callback , dwData ) ;
63+ bool ok = PInvoke . EnumDisplayMonitors ( hdc , null , callback , dwData ) ;
6264 if ( ! ok )
6365 {
6466 Marshal . ThrowExceptionForHR ( Marshal . GetLastWin32Error ( ) ) ;
6567 }
6668 return nearestMonitorInfo ;
6769 }
6870
71+ /// <summary>
72+ /// Gets the primary display monitor (the one that contains the taskbar).
73+ /// </summary>
74+ /// <returns>The primary display monitor, or null if no monitor is found.</returns>
75+ public static unsafe MonitorInfo GetPrimaryDisplayMonitor ( )
76+ {
77+ var primaryMonitor = PInvoke . MonitorFromWindow ( new HWND ( nint . Zero ) , MONITOR_FROM_FLAGS . MONITOR_DEFAULTTOPRIMARY ) ;
78+ MonitorInfo primaryMonitorInfo = null ;
79+ var callback = new MONITORENUMPROC ( ( monitor , deviceContext , rect , data ) =>
80+ {
81+ if ( monitor == primaryMonitor )
82+ {
83+ primaryMonitorInfo = new MonitorInfo ( monitor , rect ) ;
84+ return false ;
85+ }
86+ return true ;
87+ } ) ;
88+ var dwData = new LPARAM ( ) ;
89+ var hdc = new HDC ( ) ;
90+ bool ok = PInvoke . EnumDisplayMonitors ( hdc , null , callback , dwData ) ;
91+ if ( ! ok )
92+ {
93+ Marshal . ThrowExceptionForHR ( Marshal . GetLastWin32Error ( ) ) ;
94+ }
95+ return primaryMonitorInfo ;
96+ }
97+
98+ /// <summary>
99+ /// Gets the display monitor that contains the cursor.
100+ /// </summary>
101+ /// <returns>The display monitor that contains the cursor, or null if no monitor is found.</returns>
102+ public static unsafe MonitorInfo GetCursorDisplayMonitor ( )
103+ {
104+ if ( ! PInvoke . GetCursorPos ( out var pt ) )
105+ {
106+ Marshal . ThrowExceptionForHR ( Marshal . GetLastWin32Error ( ) ) ;
107+ }
108+ var cursorMonitor = PInvoke . MonitorFromPoint ( pt , MONITOR_FROM_FLAGS . MONITOR_DEFAULTTONEAREST ) ;
109+ MonitorInfo cursorMonitorInfo = null ;
110+ var callback = new MONITORENUMPROC ( ( monitor , deviceContext , rect , data ) =>
111+ {
112+ if ( monitor == cursorMonitor )
113+ {
114+ cursorMonitorInfo = new MonitorInfo ( monitor , rect ) ;
115+ return false ;
116+ }
117+ return true ;
118+ } ) ;
119+ var dwData = new LPARAM ( ) ;
120+ var hdc = new HDC ( ) ;
121+ bool ok = PInvoke . EnumDisplayMonitors ( hdc , null , callback , dwData ) ;
122+ if ( ! ok )
123+ {
124+ Marshal . ThrowExceptionForHR ( Marshal . GetLastWin32Error ( ) ) ;
125+ }
126+ return cursorMonitorInfo ;
127+ }
128+
69129 private readonly HMONITOR _monitor ;
70130
71131 internal unsafe MonitorInfo ( HMONITOR monitor , RECT * rect )
72132 {
73- RectMonitor =
133+ Bounds =
74134 new Rect ( new Point ( rect ->left , rect ->top ) ,
75135 new Point ( rect ->right , rect ->bottom ) ) ;
76136 _monitor = monitor ;
77137 var info = new MONITORINFOEXW ( ) { monitorInfo = new MONITORINFO ( ) { cbSize = ( uint ) sizeof ( MONITORINFOEXW ) } } ;
78138 GetMonitorInfo ( monitor , ref info ) ;
79- RectWork =
139+ WorkingArea =
80140 new Rect ( new Point ( info . monitorInfo . rcWork . left , info . monitorInfo . rcWork . top ) ,
81141 new Point ( info . monitorInfo . rcWork . right , info . monitorInfo . rcWork . bottom ) ) ;
82142 Name = new string ( info . szDevice . AsSpan ( ) ) . Replace ( "\0 " , "" ) . Trim ( ) ;
@@ -93,23 +153,23 @@ internal unsafe MonitorInfo(HMONITOR monitor, RECT* rect)
93153 /// <remarks>
94154 /// <note>If the monitor is not the primary display monitor, some of the rectangle's coordinates may be negative values.</note>
95155 /// </remarks>
96- public Rect RectMonitor { get ; }
156+ public Rect Bounds { get ; }
97157
98158 /// <summary>
99159 /// Gets the work area rectangle of the display monitor, expressed in virtual-screen coordinates.
100160 /// </summary>
101161 /// <remarks>
102162 /// <note>If the monitor is not the primary display monitor, some of the rectangle's coordinates may be negative values.</note>
103163 /// </remarks>
104- public Rect RectWork { get ; }
164+ public Rect WorkingArea { get ; }
105165
106166 /// <summary>
107- /// Gets if the monitor is the the primary display monitor.
167+ /// Gets if the monitor is the primary display monitor.
108168 /// </summary>
109- public bool IsPrimary => _monitor == PInvoke . MonitorFromWindow ( new ( IntPtr . Zero ) , MONITOR_FROM_FLAGS . MONITOR_DEFAULTTOPRIMARY ) ;
169+ public bool IsPrimary => _monitor == PInvoke . MonitorFromWindow ( new ( nint . Zero ) , MONITOR_FROM_FLAGS . MONITOR_DEFAULTTOPRIMARY ) ;
110170
111171 /// <inheritdoc />
112- public override string ToString ( ) => $ "{ Name } { RectMonitor . Width } x{ RectMonitor . Height } ";
172+ public override string ToString ( ) => $ "{ Name } { Bounds . Width } x{ Bounds . Height } ";
113173
114174 private static unsafe bool GetMonitorInfo ( HMONITOR hMonitor , ref MONITORINFOEXW lpmi )
115175 {
0 commit comments