@@ -107,38 +107,26 @@ public unsafe WindowPosition GetWindowPosition(
107
107
108
108
var pos = new WindowPosition ( )
109
109
{
110
- Coordinates = new Coordinates ( relPos . x , relPos . y ) ,
110
+ Coordinates = ( relPos . x , relPos . y ) ,
111
111
Size = new Size ( clientRect . right , clientRect . bottom )
112
112
} ;
113
113
114
114
// Check if the window is minimized.
115
115
if ( failIfMinimized && pos . IsMinimized )
116
116
throw new Exception ( "The window has been minimized." ) ;
117
117
118
- // Validate the position.
119
- this . ValidateWindowPosition ( pos ) ;
120
118
return pos ;
121
119
}
122
120
123
- /// <summary>
124
- /// When overridden in subclasses, throws an exception if the window position is
125
- /// not valid. This implementation does nothing.
126
- /// </summary>
127
- /// <param name="pos">The WindowPosition to validate.</param>
128
- protected virtual void ValidateWindowPosition ( WindowPosition pos )
129
- {
130
- // Do nothing.
131
- }
132
-
133
121
public void CreateWindowScreenshot (
134
122
IntPtr hWnd ,
123
+ WindowPosition windowPosition ,
135
124
[ NotNull ] ref ScreenshotContent ? existingScreenshot ,
136
- bool failIfNotInForeground = true ,
137
125
bool fromScreen = false )
138
126
{
139
127
ScreenshotContent . Create (
140
128
fromScreen ? IntPtr . Zero : hWnd ,
141
- this . GetWindowPosition ( hWnd , out _ , failIfNotInForeground ) ,
129
+ windowPosition ,
142
130
ref existingScreenshot ) ;
143
131
}
144
132
@@ -170,6 +158,11 @@ public bool TrySetWindowTopmost(IntPtr hWnd, bool topmost, bool throwIfNotSucces
170
158
return result ;
171
159
}
172
160
161
+ public void SetWindowEnabled ( IntPtr hWnd , bool enabled )
162
+ {
163
+ _ = NativeMethods . EnableWindow ( hWnd , enabled ) ;
164
+ }
165
+
173
166
public void MoveMouse ( int x , int y )
174
167
{
175
168
this . DoMouseInput ( x , y , true , null ) ;
@@ -188,13 +181,12 @@ public void ReleaseMouseButton()
188
181
private void DoMouseInput ( int x , int y , bool absoluteCoordinates , bool ? mouseDown )
189
182
{
190
183
// Convert the screen coordinates into mouse coordinates.
191
- var cs = new Coordinates ( x , y ) ;
192
- cs = this . GetMouseCoordinatesFromScreenCoordinates ( cs ) ;
184
+ var ( mouseX , mouseY ) = this . GetMouseCoordinatesFromScreenCoordinates ( x , y ) ;
193
185
194
186
var mi = new NativeMethods . MOUSEINPUT ( )
195
187
{
196
- dx = cs . X ,
197
- dy = cs . Y
188
+ dx = mouseX ,
189
+ dy = mouseY
198
190
} ;
199
191
200
192
if ( absoluteCoordinates )
@@ -213,26 +205,27 @@ private void DoMouseInput(int x, int y, bool absoluteCoordinates, bool? mouseDow
213
205
NativeMethods . MOUSEEVENTF . LEFTUP ;
214
206
}
215
207
216
- var input = new NativeMethods . INPUT
208
+ Span < NativeMethods . INPUT > inputs = stackalloc NativeMethods . INPUT [ 1 ] ;
209
+ inputs [ 0 ] = new NativeMethods . INPUT
217
210
{
218
211
type = NativeMethods . InputType . INPUT_MOUSE ,
219
212
InputUnion = {
220
213
mi = mi
221
214
}
222
215
} ;
223
216
224
- NativeMethods . SendInput ( input ) ;
217
+ NativeMethods . SendInput ( inputs ) ;
225
218
}
226
219
227
- private Coordinates GetMouseCoordinatesFromScreenCoordinates ( Coordinates screenCoords )
220
+ private ( int mouseX , int mouseY ) GetMouseCoordinatesFromScreenCoordinates ( int screenX , int screenY )
228
221
{
229
222
// Note: The mouse coordinates are relative to the primary monitor size and
230
223
// location, not to the virtual screen size, so we use
231
224
// SystemInformation.PrimaryMonitorSize.
232
225
var primaryScreenSize = SystemInformation . PrimaryMonitorSize ;
233
226
234
- double x = ( double ) 0x10000 * screenCoords . X / primaryScreenSize . Width ;
235
- double y = ( double ) 0x10000 * screenCoords . Y / primaryScreenSize . Height ;
227
+ double x = ( double ) 0x10000 * screenX / primaryScreenSize . Width ;
228
+ double y = ( double ) 0x10000 * screenY / primaryScreenSize . Height ;
236
229
237
230
/* For correct conversion when converting the flointing point numbers
238
231
* to integers, we need round away from 0, e.g.
@@ -255,7 +248,7 @@ private Coordinates GetMouseCoordinatesFromScreenCoordinates(Coordinates screenC
255
248
int resX = checked ( ( int ) ( x >= 0 ? Math . Ceiling ( x ) : Math . Floor ( x ) ) ) ;
256
249
int resY = checked ( ( int ) ( y >= 0 ? Math . Ceiling ( y ) : Math . Floor ( y ) ) ) ;
257
250
258
- return new Coordinates ( resX , resY ) ;
251
+ return ( resX , resY ) ;
259
252
}
260
253
261
254
public Coordinates GetCurrentMousePosition ( )
@@ -284,7 +277,8 @@ private void PressOrReleaseKey(VirtualKey keyCode, bool down)
284
277
if ( ! down )
285
278
ki . dwFlags = NativeMethods . KEYEVENTF . KEYUP ;
286
279
287
- var input = new NativeMethods . INPUT
280
+ Span < NativeMethods . INPUT > inputs = stackalloc NativeMethods . INPUT [ 1 ] ;
281
+ inputs [ 0 ] = new NativeMethods . INPUT
288
282
{
289
283
type = NativeMethods . InputType . INPUT_KEYBOARD ,
290
284
InputUnion =
@@ -293,12 +287,15 @@ private void PressOrReleaseKey(VirtualKey keyCode, bool down)
293
287
}
294
288
} ;
295
289
296
- NativeMethods . SendInput ( input ) ;
290
+ NativeMethods . SendInput ( inputs ) ;
297
291
}
298
292
299
293
public void WriteText ( string characters )
300
294
{
301
- var inputs = new NativeMethods . INPUT [ 2 * characters . Length ] ;
295
+ int inputsLength = 2 * characters . Length ;
296
+ var inputs = inputsLength <= 128 ?
297
+ stackalloc NativeMethods . INPUT [ inputsLength ] :
298
+ new NativeMethods . INPUT [ inputsLength ] ;
302
299
303
300
for ( int i = 0 ; i < inputs . Length ; i ++ )
304
301
{
@@ -323,7 +320,7 @@ public void WriteText(string characters)
323
320
inputs [ i ] = input ;
324
321
}
325
322
326
- NativeMethods . SendInputs ( inputs ) ;
323
+ NativeMethods . SendInput ( inputs ) ;
327
324
}
328
325
329
326
public void MoveWindowMouse ( IntPtr hWnd , int x , int y , bool isButtonDown )
@@ -468,7 +465,7 @@ private ScreenshotContent(WindowPosition pos)
468
465
469
466
public Size Size
470
467
{
471
- get => new Size ( this . bmp . Width , this . bmp . Height ) ;
468
+ get => new ( this . bmp . Width , this . bmp . Height ) ;
472
469
}
473
470
474
471
public WindowPosition WindowPosition
@@ -614,7 +611,9 @@ private void FillScreenshot(IntPtr windowHandle)
614
611
615
612
public ScreenshotColor GetPixel ( Coordinates coords )
616
613
{
617
- return this . GetPixel ( coords . X , coords . Y ) ;
614
+ return this . GetPixel (
615
+ checked ( ( int ) MathF . Round ( coords . X ) ) ,
616
+ checked ( ( int ) MathF . Round ( coords . Y ) ) ) ;
618
617
}
619
618
620
619
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
0 commit comments