Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place the cursor Exact center of the screen

Tags:

c#

winforms

How to place the cursor Exact center of the screen in C# ?

without Resolution independent (it can be 1024X768 or 1600X900)

like image 350
Gali Avatar asked Jun 15 '11 11:06

Gali


People also ask

Why is my cursor not pointing correctly?

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 do I fix my cursor alignment?

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.


2 Answers

How about this, assuming you have only 1 monitor:

Cursor.Position = new Point(Screen.PrimaryScreen.Bounds.Width / 2,
                            Screen.PrimaryScreen.Bounds.Height / 2);
like image 51
Gabe Avatar answered Nov 06 '22 02:11

Gabe


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);
like image 33
Phil Wright Avatar answered Nov 06 '22 01:11

Phil Wright