How to use GetSystemMetrics method of FlaUI.Core.WindowsAPI.User32 class

Best FlaUI code snippet using FlaUI.Core.WindowsAPI.User32.GetSystemMetrics

Mouse.cs

Source:Mouse.cs Github

copy

Full Screen

...7273 /// <summary>74 /// Flag to indicate if the buttons are swapped (left-handed)75 /// </summary>76 public static bool AreButtonsSwapped => User32.GetSystemMetrics(SystemMetric.SM_SWAPBUTTON) != 0;7778 /// <summary>79 /// Moves the mouse by a given delta from the current position80 /// </summary>81 /// <param name="deltaX">The delta for the x-axis</param>82 /// <param name="deltaY">The delta for the y-axis</param>83 public static void MoveBy(int deltaX, int deltaY)84 {85 var currPos = Position;86 MoveTo(currPos.X + deltaX, currPos.Y + deltaY);87 }8889 /// <summary>90 /// Moves the mouse to a new position91 /// </summary>92 /// <param name="newX">The new position on the x-axis</param>93 /// <param name="newY">The new position on the y-axis</param>94 public static void MoveTo(int newX, int newY)95 {96 // Get starting position97 var startPos = Position;98 var startX = startPos.X;99 var startY = startPos.Y;100101 // Prepare variables102 var totalDistance = startPos.Distance(newX, newY);103104 // Calculate the duration for the speed105 var optimalPixelsPerMillisecond = 1;106 var minDuration = 200;107 var maxDuration = 500;108 var duration = Convert.ToInt32(totalDistance / optimalPixelsPerMillisecond).Clamp(minDuration, maxDuration);109110 // Calculate the steps for the smoothness111 var optimalPixelsPerStep = 10;112 var minSteps = 10;113 var maxSteps = 50;114 var steps = Convert.ToInt32(totalDistance / optimalPixelsPerStep).Clamp(minSteps, maxSteps);115116 // Calculate the interval and the step size117 var interval = duration / steps;118 var stepX = (double)(newX - startX) / steps;119 var stepY = (double)(newY - startY) / steps;120121 // Build a list of movement points (except the last one, to set that one perfectly)122 var movements = new List<Point>();123 for (var i = 1; i < steps; i++)124 {125 var tempX = startX + i * stepX;126 var tempY = startY + i * stepY;127 movements.Add(new Point(tempX.ToInt(), tempY.ToInt()));128 }129130 // Add an exact point for the last one, if it does not fit exactly131 var lastPoint = movements.Last();132 if (lastPoint.X != newX || lastPoint.Y != newY)133 {134 movements.Add(new Point(newX, newY));135 }136137 // Loop thru the steps and set them138 foreach (var point in movements)139 {140 Position = point;141 Thread.Sleep(interval);142 }143 Wait.UntilInputIsProcessed();144 }145146 /// <summary>147 /// Moves the mouse to a new position148 /// </summary>149 /// <param name="newPosition">The new position for the mouse</param>150 public static void MoveTo(Point newPosition)151 {152 MoveTo(newPosition.X, newPosition.Y);153 }154155 /// <summary>156 /// Clicks the specified mouse button at the current location157 /// </summary>158 /// <param name="mouseButton">The mouse button to click</param>159 public static void Click(MouseButton mouseButton)160 {161 var currClickPosition = Position;162 // Check if the position is the same as with last click163 if (LastClickPositions[mouseButton].Equals(currClickPosition))164 {165 // Get the timeout needed to not fire a double click166 var timeout = CurrentDoubleClickTime - DateTime.Now.Subtract(LastClickTimes[mouseButton]).Milliseconds;167 // Wait the needed time to prevent the double click168 if (timeout > 0) Thread.Sleep(timeout + ExtraMillisecondsBecauseOfBugInWindows);169 }170 // Perform the click171 Down(mouseButton);172 Up(mouseButton);173 // Update the time and location174 LastClickTimes[mouseButton] = DateTime.Now;175 LastClickPositions[mouseButton] = Position;176 }177178 /// <summary>179 /// Moves to a specific position and clicks the specified mouse button180 /// </summary>181 /// <param name="mouseButton">The mouse button to click</param>182 /// <param name="point">The position to move to before clicking</param>183 public static void Click(MouseButton mouseButton, Point point)184 {185 Position = point;186 Click(mouseButton);187 }188189 /// <summary>190 /// Double-clicks the specified mouse button at the current location191 /// </summary>192 /// <param name="mouseButton">The mouse button to double-click</param>193 public static void DoubleClick(MouseButton mouseButton)194 {195 Down(mouseButton);196 Up(mouseButton);197 Down(mouseButton);198 Up(mouseButton);199 }200201 /// <summary>202 /// Moves to a specific position and double-clicks the specified mouse button203 /// </summary>204 /// <param name="mouseButton">The mouse button to double-click</param>205 /// <param name="point">The position to move to before double-clicking</param>206 public static void DoubleClick(MouseButton mouseButton, Point point)207 {208 Position = point;209 DoubleClick(mouseButton);210 }211212 /// <summary>213 /// Sends a mouse down command for the specified mouse button214 /// </summary>215 /// <param name="mouseButton">The mouse button to press</param>216 public static void Down(MouseButton mouseButton)217 {218 uint data;219 var flags = GetFlagsAndDataForButton(mouseButton, true, out data);220 SendInput(0, 0, data, flags);221 }222223 /// <summary>224 /// Sends a mouse up command for the specified mouse button225 /// </summary>226 /// <param name="mouseButton">The mouse button to release</param>227 public static void Up(MouseButton mouseButton)228 {229 uint data;230 var flags = GetFlagsAndDataForButton(mouseButton, false, out data);231 SendInput(0, 0, data, flags);232 }233234 /// <summary>235 /// Simulates scrolling of the mouse wheel up or down236 /// </summary>237 public static void Scroll(double lines)238 {239 var amount = (uint)(WheelDelta * lines);240 SendInput(0, 0, amount, MouseEventFlags.MOUSEEVENTF_WHEEL);241 }242243 /// <summary>244 /// Simulates scrolling of the horizontal mouse wheel left or right245 /// </summary>246 public static void HorizontalScroll(double lines)247 {248 var amount = (uint)(WheelDelta * lines);249 SendInput(0, 0, amount, MouseEventFlags.MOUSEEVENTF_HWHEEL);250 }251252 /// <summary>253 /// Drags the mouse horizontally254 /// </summary>255 /// <param name="mouseButton">The mouse button to use for dragging</param>256 /// <param name="startingPoint">Starting point of the drag</param>257 /// <param name="distance">The distance to drag, + for right, - for left</param>258 public static void DragHorizontally(MouseButton mouseButton, Point startingPoint, int distance)259 {260 Drag(mouseButton, startingPoint, distance, 0);261 }262263 /// <summary>264 /// Drags the mouse vertically265 /// </summary>266 /// <param name="mouseButton">The mouse button to use for dragging</param>267 /// <param name="startingPoint">Starting point of the drag</param>268 /// <param name="distance">The distance to drag, + for down, - for up</param>269 public static void DragVertically(MouseButton mouseButton, Point startingPoint, int distance)270 {271 Drag(mouseButton, startingPoint, 0, distance);272 }273274 /// <summary>275 /// Drags the mouse from the starting point with the given distance.276 /// </summary>277 /// <param name="mouseButton">The mouse button to use for dragging.</param>278 /// <param name="startingPoint">Starting point of the drag.</param>279 /// <param name="distanceX">The x distance to drag, + for down, - for up.</param>280 /// <param name="distanceY">The y distance to drag, + for right, - for left.</param>281 public static void Drag(MouseButton mouseButton, Point startingPoint, int distanceX, int distanceY)282 {283 var endingPoint = new Point(startingPoint.X + distanceX, startingPoint.Y + distanceY);284 Drag(mouseButton, startingPoint, endingPoint);285 }286287 /// <summary>288 /// Drags the mouse from the starting point to another point.289 /// </summary>290 /// <param name="mouseButton">The mouse button to use for dragging.</param>291 /// <param name="startingPoint">Starting point of the drag.</param>292 /// <param name="endingPoint">Ending point of the drag.</param>293 public static void Drag(MouseButton mouseButton, Point startingPoint, Point endingPoint)294 {295 Position = startingPoint;296 Wait.UntilInputIsProcessed();297 Down(mouseButton);298 Wait.UntilInputIsProcessed();299 Position = endingPoint;300 Wait.UntilInputIsProcessed();301 Up(mouseButton);302 Wait.UntilInputIsProcessed();303 }304305 /// <summary>306 /// Converts the button to the correct <see cref="MouseEventFlags" /> object307 /// and fills the additional data if needed308 /// </summary>309 private static MouseEventFlags GetFlagsAndDataForButton(MouseButton mouseButton, bool isDown, out uint data)310 {311 MouseEventFlags mouseEventFlags;312 var mouseData = MouseEventDataXButtons.NOTHING;313 switch (SwapButtonIfNeeded(mouseButton))314 {315 case MouseButton.Left:316 mouseEventFlags = isDown ? MouseEventFlags.MOUSEEVENTF_LEFTDOWN : MouseEventFlags.MOUSEEVENTF_LEFTUP;317 break;318 case MouseButton.Middle:319 mouseEventFlags = isDown ? MouseEventFlags.MOUSEEVENTF_MIDDLEDOWN : MouseEventFlags.MOUSEEVENTF_MIDDLEUP;320 break;321 case MouseButton.Right:322 mouseEventFlags = isDown ? MouseEventFlags.MOUSEEVENTF_RIGHTDOWN : MouseEventFlags.MOUSEEVENTF_RIGHTUP;323 break;324 case MouseButton.XButton1:325 mouseEventFlags = isDown ? MouseEventFlags.MOUSEEVENTF_XDOWN : MouseEventFlags.MOUSEEVENTF_XUP;326 mouseData = MouseEventDataXButtons.XBUTTON1;327 break;328 case MouseButton.XButton2:329 mouseEventFlags = isDown ? MouseEventFlags.MOUSEEVENTF_XDOWN : MouseEventFlags.MOUSEEVENTF_XUP;330 mouseData = MouseEventDataXButtons.XBUTTON2;331 break;332 default:333 throw new ArgumentOutOfRangeException("mouseButton");334 }335 data = (uint)mouseData;336 return mouseEventFlags;337 }338339 /// <summary>340 /// Swaps the left/right button if <see cref="AreButtonsSwapped" /> is set341 /// </summary>342 private static MouseButton SwapButtonIfNeeded(MouseButton mouseButton)343 {344 if (!AreButtonsSwapped) return mouseButton;345 switch (mouseButton)346 {347 case MouseButton.Left:348 return MouseButton.Right;349 case MouseButton.Right:350 return MouseButton.Left;351 default:352 return mouseButton;353 }354 }355356 /// <summary>357 /// Effectively sends the mouse input command358 /// </summary>359 [PermissionSet(SecurityAction.Assert, Name = "FullTrust")]360 private static void SendInput(int x, int y, uint data, MouseEventFlags flags)361 {362 // Demand the correct permissions363 var permissions = new PermissionSet(PermissionState.Unrestricted);364 permissions.Demand();365366 // Check if we are trying to do an absolute move367 if (flags.HasFlag(MouseEventFlags.MOUSEEVENTF_ABSOLUTE))368 {369 // Absolute position requires normalized coordinates370 NormalizeCoordinates(ref x, ref y);371 flags |= MouseEventFlags.MOUSEEVENTF_VIRTUALDESK;372 }373374 // Build the mouse input object375 var mouseInput = new MOUSEINPUT376 {377 dx = x,378 dy = y,379 mouseData = data,380 dwExtraInfo = User32.GetMessageExtraInfo(),381 time = 0,382 dwFlags = flags383 };384385 // Build the input object386 var input = INPUT.MouseInput(mouseInput);387 // Send the command388 if (User32.SendInput(1, new[] { input }, INPUT.Size) == 0)389 {390 // An error occured391 var errorCode = Marshal.GetLastWin32Error();392 Logger.Default.Warn("Could not send mouse input. ErrorCode: {0}", errorCode);393 }394 }395396 /// <summary>397 /// Normalizes the coordinates to get the absolute values from 0 to 65536398 /// </summary>399 private static void NormalizeCoordinates(ref int x, ref int y)400 {401 var vScreenWidth = User32.GetSystemMetrics(SystemMetric.SM_CXVIRTUALSCREEN);402 var vScreenHeight = User32.GetSystemMetrics(SystemMetric.SM_CYVIRTUALSCREEN);403 var vScreenLeft = User32.GetSystemMetrics(SystemMetric.SM_XVIRTUALSCREEN);404 var vScreenTop = User32.GetSystemMetrics(SystemMetric.SM_YVIRTUALSCREEN);405406 x = (x - vScreenLeft) * 65536 / vScreenWidth + 65536 / (vScreenWidth * 2);407 y = (y - vScreenTop) * 65536 / vScreenHeight + 65536 / (vScreenHeight * 2);408 }409410 #region Convenience methods411 public static void LeftClick()412 {413 Click(MouseButton.Left);414 }415416 public static void LeftClick(Point point)417 {418 Click(MouseButton.Left, point); ...

Full Screen

Full Screen

Capture.cs

Source:Capture.cs Github

copy

Full Screen

...19 public static CaptureImage MainScreen(CaptureSettings settings = null)20 {21 var primaryScreenBounds = new Rectangle(22 0, 0,23 User32.GetSystemMetrics(SystemMetric.SM_CXSCREEN), User32.GetSystemMetrics(SystemMetric.SM_CYSCREEN));24 return Rectangle(primaryScreenBounds, settings);25 }26 /// <summary>27 /// Captures the whole screen (all monitors).28 /// </summary>29 public static CaptureImage Screen(int screenIndex = -1, CaptureSettings settings = null)30 {31 Rectangle capturingRectangle;32 // Take the appropriate screen if requested33 if (screenIndex >= 0 && screenIndex < User32.GetSystemMetrics(SystemMetric.SM_CMONITORS))34 {35 var rectangle = GetBoundsByScreenIndex(screenIndex);36 capturingRectangle = rectangle;37 }38 else39 {40 // Use the entire desktop41 capturingRectangle = new Rectangle(42 User32.GetSystemMetrics(SystemMetric.SM_XVIRTUALSCREEN), User32.GetSystemMetrics(SystemMetric.SM_YVIRTUALSCREEN),43 User32.GetSystemMetrics(SystemMetric.SM_CXVIRTUALSCREEN), User32.GetSystemMetrics(SystemMetric.SM_CYVIRTUALSCREEN));44 }45 return Rectangle(capturingRectangle, settings);46 }47 /// <summary>48 /// Captures all screens an element is on.49 /// </summary>50 public static CaptureImage ScreensWithElement(AutomationElement element, CaptureSettings settings = null)51 {52 var elementRectangle = element.BoundingRectangle;53 var intersectedScreenBounds = new List<Rectangle>();54 // Calculate which screens intersect with the element55 for (var screenIndex = 0; screenIndex < User32.GetSystemMetrics(SystemMetric.SM_CMONITORS); screenIndex++)56 {57 var screenRectangle = GetBoundsByScreenIndex(screenIndex);58 if (screenRectangle.IntersectsWith(elementRectangle))59 {60 intersectedScreenBounds.Add(screenRectangle);61 }62 }63 if (intersectedScreenBounds.Count > 0)64 {65 var minX = intersectedScreenBounds.Min(x => x.Left);66 var maxX = intersectedScreenBounds.Max(x => x.Right);67 var minY = intersectedScreenBounds.Min(x => x.Top);68 var maxY = intersectedScreenBounds.Max(x => x.Bottom);69 var captureRect = new Rectangle(minX, minY, maxX - minX, maxY - minY);...

Full Screen

Full Screen

User32.cs

Source:User32.cs Github

copy

Full Screen

...37 [DllImport("user32.dll", SetLastError = true)]38 public static extern uint GetDoubleClickTime();3940 [DllImport("user32.dll", SetLastError = true)]41 public static extern int GetSystemMetrics(SystemMetric nIndex);4243 [DllImport("user32.dll", SetLastError = true)]44 public static extern IntPtr GetMessageExtraInfo();4546 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]47 public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint msg, UIntPtr wParam, IntPtr lParam, SendMessageTimeoutFlags fuFlags, uint uTimeout, out UIntPtr lpdwResult);4849 [DllImport("user32.dll", SetLastError = true)]50 public static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);5152 [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]53 public static extern short VkKeyScan(char ch);5455 [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] ...

Full Screen

Full Screen

GetSystemMetrics

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.WindowsAPI;2using System;3using System.Windows.Forms;4{5 {6 public Form1()7 {8 InitializeComponent();9 }10 private void btnGetSystemMetrics_Click(object sender, EventArgs e)11 {12 var width = User32.GetSystemMetrics(User32.SystemMetric.SM_CXSCREEN);13 var height = User32.GetSystemMetrics(User32.SystemMetric.SM_CYSCREEN);14 MessageBox.Show("Width: " + width + " Height: " + height);15 }16 }17}

Full Screen

Full Screen

GetSystemMetrics

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.WindowsAPI;2{3 {4 [DllImport("user32.dll")]5 public static extern int GetSystemMetrics(SystemMetric smIndex);6 }7}8using FlaUI.Core.WindowsAPI;9{10 {

Full Screen

Full Screen

GetSystemMetrics

Using AI Code Generation

copy

Full Screen

1using System;2using System.Windows.Forms;3using FlaUI.Core.WindowsAPI;4{5 {6 [DllImport("user32.dll")]7 public static extern int GetSystemMetrics(SystemMetric smIndex);8 }9}10using System;11using System.Windows.Forms;12using FlaUI.Core.WindowsAPI;13{14 {15 [DllImport("user32.dll")]16 public static extern int GetSystemMetrics(SystemMetric smIndex);17 }18}19using System;20using System.Windows.Forms;21using FlaUI.Core.WindowsAPI;22{23 {24 [DllImport("user32.dll")]25 public static extern int GetSystemMetrics(SystemMetric smIndex);26 }27}28using System;29using System.Windows.Forms;30using FlaUI.Core.WindowsAPI;31{32 {33 [DllImport("user32.dll")]34 public static extern int GetSystemMetrics(SystemMetric smIndex);35 }36}37using System;38using System.Windows.Forms;39using FlaUI.Core.WindowsAPI;40{41 {42 [DllImport("user32.dll")]43 public static extern int GetSystemMetrics(SystemMetric smIndex);44 }45}46using System;47using System.Windows.Forms;48using FlaUI.Core.WindowsAPI;49{50 {51 [DllImport("user32.dll")]52 public static extern int GetSystemMetrics(SystemMetric smIndex);53 }54}55using System;56using System.Windows.Forms;

Full Screen

Full Screen

GetSystemMetrics

Using AI Code Generation

copy

Full Screen

1using System;2using FlaUI.Core.WindowsAPI;3{4 {5 static void Main(string[] args)6 {7 int width = User32.GetSystemMetrics(SystemMetric.SM_CXSCREEN);8 int height = User32.GetSystemMetrics(SystemMetric.SM_CYSCREEN);9 Console.WriteLine("The screen resolution is {0}x{1}", width, height);10 }11 }12}

Full Screen

Full Screen

GetSystemMetrics

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.WindowsAPI;2using FlaUI.Core.WindowsAPI.Enums;3using System;4using System.Windows.Forms;5{6 {7 public Form1()8 {9 InitializeComponent();10 }11 private void button1_Click(object sender, EventArgs e)12 {13 var screen = Screen.PrimaryScreen;14 var width = User32.GetSystemMetrics(SystemMetric.SM_CXSCREEN);15 var height = User32.GetSystemMetrics(SystemMetric.SM_CYSCREEN);16 MessageBox.Show("Width: " + width + "\r17Height: " + height);18 }19 }20}21using FlaUI.Core.WindowsAPI;22using FlaUI.Core.WindowsAPI.Enums;23using System;24using System.Windows.Forms;25{26 {27 public Form1()28 {29 InitializeComponent();30 }31 private void button1_Click(object sender, EventArgs e)32 {33 var screen = Screen.PrimaryScreen;34 var width = User32.GetSystemMetrics(SystemMetric.SM_CXSCREEN);35 var height = User32.GetSystemMetrics(SystemMetric.SM_CYSCREEN);36 MessageBox.Show("Width: " + width + "\r37Height: " + height);38 }39 }40}41using FlaUI.Core.WindowsAPI;42using FlaUI.Core.WindowsAPI.Enums;43using System;44using System.Windows.Forms;45{46 {47 public Form1()48 {49 InitializeComponent();50 }51 private void button1_Click(object sender, EventArgs e)52 {53 var screen = Screen.PrimaryScreen;54 var width = User32.GetSystemMetrics(SystemMetric.SM_CXSCREEN);55 var height = User32.GetSystemMetrics(SystemMetric.SM_CYSCREEN);56 MessageBox.Show("Width: " + width + "\r57Height: " + height);58 }59 }60}61using FlaUI.Core.WindowsAPI;62using FlaUI.Core.WindowsAPI.Enums;63using System;64using System.Windows.Forms;65{

Full Screen

Full Screen

GetSystemMetrics

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core.WindowsAPI;7{8 {9 [DllImport("user32.dll")]10 public static extern int GetSystemMetrics(SystemMetric smIndex);11 }12}13using System;14using System.Collections.Generic;15using System.Linq;16using System.Text;17using System.Threading.Tasks;18using FlaUI.Core.WindowsAPI;19{20 {

Full Screen

Full Screen

GetSystemMetrics

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.WindowsAPI;2using System;3{4 {5 static void Main(string[] args)6 {7 Console.WriteLine("Screen width: " + User32.GetSystemMetrics(User32.SystemMetric.SM_CXSCREEN));8 Console.WriteLine("Screen height: " + User32.GetSystemMetrics(User32.SystemMetric.SM_CYSCREEN));9 Console.ReadKey();10 }11 }12}13public static int GetSystemMetrics(SystemMetric nIndex)14User32.GetSystemMetrics(User32.SystemMetric.SM_CXSCREEN)

Full Screen

Full Screen

GetSystemMetrics

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.WindowsAPI;2using System;3{4 {5 static void Main(string[] args)6 {7 int width = User32.GetSystemMetrics(SystemMetric.SM_CXSCREEN);8 int height = User32.GetSystemMetrics(SystemMetric.SM_CYSCREEN);9 Console.WriteLine("Width: " + width);10 Console.WriteLine("Height: " + height);11 Console.ReadLine();12 }13 }14}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful