开发者

How to place the cursor Exact center of the screen

开发者 https://www.devze.com 2023-03-12 22:51 出处:网络
How to place the cursor Exact center of the screen in C# ? withou开发者_如何学Ct Resolution independent (it can be 1024X768 or 1600X900)How about this, assuming you have only 1 monitor:

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

withou开发者_如何学Ct Resolution independent (it can be 1024X768 or 1600X900)


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);


Try

        var r = Screen.PrimaryScreen.Bounds;

        System.Windows.Forms.Cursor.Position = new Point(r.Bottom/2,r.Right/2);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号