1
1
using System . Collections . Generic ;
2
- using System ;
3
2
using System . Runtime . InteropServices ;
4
3
using System . Windows ;
5
4
using Windows . Win32 ;
6
5
using Windows . Win32 . Foundation ;
7
6
using Windows . Win32 . Graphics . Gdi ;
8
7
using Windows . Win32 . UI . WindowsAndMessaging ;
9
8
10
- namespace Flow . Launcher . Infrastructure ;
9
+ namespace Flow . Launcher . Plugin . SharedModels ;
11
10
12
11
/// <summary>
13
12
/// 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.
15
14
/// </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
17
19
{
18
20
/// <summary>
19
21
/// Gets the display monitors (including invisible pseudo-monitors associated with the mirroring drivers).
@@ -23,14 +25,14 @@ public static unsafe IList<MonitorInfo> GetDisplayMonitors()
23
25
{
24
26
var monitorCount = PInvoke . GetSystemMetrics ( SYSTEM_METRICS_INDEX . SM_CMONITORS ) ;
25
27
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 ) =>
27
29
{
28
30
list . Add ( new MonitorInfo ( monitor , rect ) ) ;
29
31
return true ;
30
32
} ) ;
31
33
var dwData = new LPARAM ( ) ;
32
34
var hdc = new HDC ( ) ;
33
- bool ok = PInvoke . EnumDisplayMonitors ( hdc , ( RECT ? ) null , callback , dwData ) ;
35
+ bool ok = PInvoke . EnumDisplayMonitors ( hdc , null , callback , dwData ) ;
34
36
if ( ! ok )
35
37
{
36
38
Marshal . ThrowExceptionForHR ( Marshal . GetLastWin32Error ( ) ) ;
@@ -43,11 +45,11 @@ public static unsafe IList<MonitorInfo> GetDisplayMonitors()
43
45
/// </summary>
44
46
/// <param name="hwnd">Window handle</param>
45
47
/// <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 )
47
49
{
48
- var nearestMonitor = PInvoke . MonitorFromWindow ( hwnd , MONITOR_FROM_FLAGS . MONITOR_DEFAULTTONEAREST ) ;
50
+ var nearestMonitor = PInvoke . MonitorFromWindow ( new ( hwnd ) , MONITOR_FROM_FLAGS . MONITOR_DEFAULTTONEAREST ) ;
49
51
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 ) =>
51
53
{
52
54
if ( monitor == nearestMonitor )
53
55
{
@@ -58,25 +60,83 @@ public static unsafe MonitorInfo GetNearestDisplayMonitor(HWND hwnd)
58
60
} ) ;
59
61
var dwData = new LPARAM ( ) ;
60
62
var hdc = new HDC ( ) ;
61
- bool ok = PInvoke . EnumDisplayMonitors ( hdc , ( RECT ? ) null , callback , dwData ) ;
63
+ bool ok = PInvoke . EnumDisplayMonitors ( hdc , null , callback , dwData ) ;
62
64
if ( ! ok )
63
65
{
64
66
Marshal . ThrowExceptionForHR ( Marshal . GetLastWin32Error ( ) ) ;
65
67
}
66
68
return nearestMonitorInfo ;
67
69
}
68
70
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
+
69
129
private readonly HMONITOR _monitor ;
70
130
71
131
internal unsafe MonitorInfo ( HMONITOR monitor , RECT * rect )
72
132
{
73
- RectMonitor =
133
+ Bounds =
74
134
new Rect ( new Point ( rect ->left , rect ->top ) ,
75
135
new Point ( rect ->right , rect ->bottom ) ) ;
76
136
_monitor = monitor ;
77
137
var info = new MONITORINFOEXW ( ) { monitorInfo = new MONITORINFO ( ) { cbSize = ( uint ) sizeof ( MONITORINFOEXW ) } } ;
78
138
GetMonitorInfo ( monitor , ref info ) ;
79
- RectWork =
139
+ WorkingArea =
80
140
new Rect ( new Point ( info . monitorInfo . rcWork . left , info . monitorInfo . rcWork . top ) ,
81
141
new Point ( info . monitorInfo . rcWork . right , info . monitorInfo . rcWork . bottom ) ) ;
82
142
Name = new string ( info . szDevice . AsSpan ( ) ) . Replace ( "\0 " , "" ) . Trim ( ) ;
@@ -93,23 +153,23 @@ internal unsafe MonitorInfo(HMONITOR monitor, RECT* rect)
93
153
/// <remarks>
94
154
/// <note>If the monitor is not the primary display monitor, some of the rectangle's coordinates may be negative values.</note>
95
155
/// </remarks>
96
- public Rect RectMonitor { get ; }
156
+ public Rect Bounds { get ; }
97
157
98
158
/// <summary>
99
159
/// Gets the work area rectangle of the display monitor, expressed in virtual-screen coordinates.
100
160
/// </summary>
101
161
/// <remarks>
102
162
/// <note>If the monitor is not the primary display monitor, some of the rectangle's coordinates may be negative values.</note>
103
163
/// </remarks>
104
- public Rect RectWork { get ; }
164
+ public Rect WorkingArea { get ; }
105
165
106
166
/// <summary>
107
- /// Gets if the monitor is the the primary display monitor.
167
+ /// Gets if the monitor is the primary display monitor.
108
168
/// </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 ) ;
110
170
111
171
/// <inheritdoc />
112
- public override string ToString ( ) => $ "{ Name } { RectMonitor . Width } x{ RectMonitor . Height } ";
172
+ public override string ToString ( ) => $ "{ Name } { Bounds . Width } x{ Bounds . Height } ";
113
173
114
174
private static unsafe bool GetMonitorInfo ( HMONITOR hMonitor , ref MONITORINFOEXW lpmi )
115
175
{
0 commit comments