How to place the cursor Exact center of the screen in C# ?
without Resolution independent (it can be 1024X768 or 1600X900)
If you have an optical mouse (LED or laser) with erratic behavior, the optical eye may be blocked. Hair or fuzz can block the sensor on the bottom of the mouse, preventing the optical sensor from working correctly. Turn the mouse over and make sure there is no debris blocking the hole.
How to automatically align the mouse cursor? To active the snap to button, you will need to go into the Settings, then navigate to Devices > Mouse. Here you will find a second menu where you can select Additional mouse options in the related settings area. Go to the Pointer Options tab.
How about this, assuming you have only 1 monitor:
Cursor.Position = new Point(Screen.PrimaryScreen.Bounds.Width / 2,
Screen.PrimaryScreen.Bounds.Height / 2);
Start by getting the Screen instance of interest. If you only ever want the main monitor then simply ask for the PrimaryScreen instance. If however you want the monitor that currently contains the mouse pointer then use the FromPoint static method.
// Main monitor
Screen s = Screen.PrimaryScreen;
// Monitor that contains the mouse pointer
Screen s = Screen.FromPoint(Cursor.Position);
To get the raw monitor bounds then just use the Bounds instance property. But if you want the working area of the monitor, the area that is left over after allocating space for the task bar and widgets then use the WorkingArea instance property.
// Raw bounds of the monitor (i.e. actual pixel resolution)
Rectangle b = s.Bounds;
// Working area after subtracting task bar/widget area etc...
Rectangle b = s.WorkingArea;
Finally position the mouse in the center of the calculated bounds.
// On multi monitor systems the top left will not necessarily be 0,0
Cursor.Position = new Point(b.Left + b.Width / 2,
b.Top + b.Height / 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With