@@ -107,38 +107,26 @@ public unsafe WindowPosition GetWindowPosition(
107107
108108 var pos = new WindowPosition ( )
109109 {
110- Coordinates = new Coordinates ( relPos . x , relPos . y ) ,
110+ Coordinates = ( relPos . x , relPos . y ) ,
111111 Size = new Size ( clientRect . right , clientRect . bottom )
112112 } ;
113113
114114 // Check if the window is minimized.
115115 if ( failIfMinimized && pos . IsMinimized )
116116 throw new Exception ( "The window has been minimized." ) ;
117117
118- // Validate the position.
119- this . ValidateWindowPosition ( pos ) ;
120118 return pos ;
121119 }
122120
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-
133121 public void CreateWindowScreenshot (
134122 IntPtr hWnd ,
123+ WindowPosition windowPosition ,
135124 [ NotNull ] ref ScreenshotContent ? existingScreenshot ,
136- bool failIfNotInForeground = true ,
137125 bool fromScreen = false )
138126 {
139127 ScreenshotContent . Create (
140128 fromScreen ? IntPtr . Zero : hWnd ,
141- this . GetWindowPosition ( hWnd , out _ , failIfNotInForeground ) ,
129+ windowPosition ,
142130 ref existingScreenshot ) ;
143131 }
144132
@@ -170,6 +158,11 @@ public bool TrySetWindowTopmost(IntPtr hWnd, bool topmost, bool throwIfNotSucces
170158 return result ;
171159 }
172160
161+ public void SetWindowEnabled ( IntPtr hWnd , bool enabled )
162+ {
163+ _ = NativeMethods . EnableWindow ( hWnd , enabled ) ;
164+ }
165+
173166 public void MoveMouse ( int x , int y )
174167 {
175168 this . DoMouseInput ( x , y , true , null ) ;
@@ -188,13 +181,12 @@ public void ReleaseMouseButton()
188181 private void DoMouseInput ( int x , int y , bool absoluteCoordinates , bool ? mouseDown )
189182 {
190183 // 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 ) ;
193185
194186 var mi = new NativeMethods . MOUSEINPUT ( )
195187 {
196- dx = cs . X ,
197- dy = cs . Y
188+ dx = mouseX ,
189+ dy = mouseY
198190 } ;
199191
200192 if ( absoluteCoordinates )
@@ -213,26 +205,27 @@ private void DoMouseInput(int x, int y, bool absoluteCoordinates, bool? mouseDow
213205 NativeMethods . MOUSEEVENTF . LEFTUP ;
214206 }
215207
216- var input = new NativeMethods . INPUT
208+ Span < NativeMethods . INPUT > inputs = stackalloc NativeMethods . INPUT [ 1 ] ;
209+ inputs [ 0 ] = new NativeMethods . INPUT
217210 {
218211 type = NativeMethods . InputType . INPUT_MOUSE ,
219212 InputUnion = {
220213 mi = mi
221214 }
222215 } ;
223216
224- NativeMethods . SendInput ( input ) ;
217+ NativeMethods . SendInput ( inputs ) ;
225218 }
226219
227- private Coordinates GetMouseCoordinatesFromScreenCoordinates ( Coordinates screenCoords )
220+ private ( int mouseX , int mouseY ) GetMouseCoordinatesFromScreenCoordinates ( int screenX , int screenY )
228221 {
229222 // Note: The mouse coordinates are relative to the primary monitor size and
230223 // location, not to the virtual screen size, so we use
231224 // SystemInformation.PrimaryMonitorSize.
232225 var primaryScreenSize = SystemInformation . PrimaryMonitorSize ;
233226
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 ;
236229
237230 /* For correct conversion when converting the flointing point numbers
238231 * to integers, we need round away from 0, e.g.
@@ -255,7 +248,7 @@ private Coordinates GetMouseCoordinatesFromScreenCoordinates(Coordinates screenC
255248 int resX = checked ( ( int ) ( x >= 0 ? Math . Ceiling ( x ) : Math . Floor ( x ) ) ) ;
256249 int resY = checked ( ( int ) ( y >= 0 ? Math . Ceiling ( y ) : Math . Floor ( y ) ) ) ;
257250
258- return new Coordinates ( resX , resY ) ;
251+ return ( resX , resY ) ;
259252 }
260253
261254 public Coordinates GetCurrentMousePosition ( )
@@ -284,7 +277,8 @@ private void PressOrReleaseKey(VirtualKey keyCode, bool down)
284277 if ( ! down )
285278 ki . dwFlags = NativeMethods . KEYEVENTF . KEYUP ;
286279
287- var input = new NativeMethods . INPUT
280+ Span < NativeMethods . INPUT > inputs = stackalloc NativeMethods . INPUT [ 1 ] ;
281+ inputs [ 0 ] = new NativeMethods . INPUT
288282 {
289283 type = NativeMethods . InputType . INPUT_KEYBOARD ,
290284 InputUnion =
@@ -293,12 +287,15 @@ private void PressOrReleaseKey(VirtualKey keyCode, bool down)
293287 }
294288 } ;
295289
296- NativeMethods . SendInput ( input ) ;
290+ NativeMethods . SendInput ( inputs ) ;
297291 }
298292
299293 public void WriteText ( string characters )
300294 {
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 ] ;
302299
303300 for ( int i = 0 ; i < inputs . Length ; i ++ )
304301 {
@@ -323,7 +320,7 @@ public void WriteText(string characters)
323320 inputs [ i ] = input ;
324321 }
325322
326- NativeMethods . SendInputs ( inputs ) ;
323+ NativeMethods . SendInput ( inputs ) ;
327324 }
328325
329326 public void MoveWindowMouse ( IntPtr hWnd , int x , int y , bool isButtonDown )
@@ -468,7 +465,7 @@ private ScreenshotContent(WindowPosition pos)
468465
469466 public Size Size
470467 {
471- get => new Size ( this . bmp . Width , this . bmp . Height ) ;
468+ get => new ( this . bmp . Width , this . bmp . Height ) ;
472469 }
473470
474471 public WindowPosition WindowPosition
@@ -614,7 +611,9 @@ private void FillScreenshot(IntPtr windowHandle)
614611
615612 public ScreenshotColor GetPixel ( Coordinates coords )
616613 {
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 ) ) ) ;
618617 }
619618
620619 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
0 commit comments