2

I am developing a Windows desktop application dual monitor where I need to display my form sometimes on primary screen and sometimes on secondary, which works fine but when I display it on my secondary screen I want it to be displayed on centre of my screen which is not working.

Here is my code:

if (Screen.AllScreens.Length > 1)
myForm.Location = Screen.AllScreens[1].WorkingArea.Location;
myForm.StartPosition = FormStartPosition.Manual; // because i wrote manual it is displayed on Top left of my secondaryScreen which is ok
myForm.show();

but I want to diplay it on centre so I wrote

myForm.StartPosition = FormStartPosition.CentreScreen;
//it is not working again a form is displayed on Centre of PrimaryScreen..

Any idea why?

DeanOC
  • 7,142
  • 6
  • 42
  • 56
DeveloperSD
  • 129
  • 1
  • 2
  • 8

4 Answers4

2

You cannot use StartPosition.CenterScreen because that picks the monitor on which the mouse is currently located. Usually desirable but not what you are asking for. You must use the form's Load event to move it where you want it. Using form's Load is important, you do not know the size of the window until after it is created and the user's preferences are applied and it is rescaled to match the video DPI.

Boilerplate code should look like this:

    private void button1_Click(object sender, EventArgs e) {
        var form = new Form2();
        form.Load += CenterOnSecondMonitor;
        form.Show();
    }

    private void CenterOnSecondMonitor(object sender, EventArgs e) {
        var form = (Form)sender;
        var area = Screen.AllScreens.Length > 1 ? Screen.AllScreens[1].WorkingArea : Screen.PrimaryScreen.WorkingArea;
        form.Location = new Point((area.Width - form.Width) / 2, (area.Height - form.Height) / 2);
        form.Load -= CenterOnSecondMonitor;
    }

Or you put this code into the form itself, the more common choice:

    protected override void OnLoad(EventArgs e) {
        var area = Screen.AllScreens.Length > 1 ? Screen.AllScreens[1].WorkingArea : Screen.PrimaryScreen.WorkingArea;
        this.Location = new Point((area.Width - this.Width) / 2, (area.Height - this.Height) / 2);
        base.OnLoad(e);
    }
Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • math is off on this one. You need to include the Left and Top of the screen you use. This centers using the size of second screen, but not the position. – Garr Godfrey Jul 05 '17 at 06:29
0

look for the property of your winform named StartPosition then set it into Center Screen

dada
  • 273
  • 2
  • 14
  • u can read my post above on Property window its already ScreenCentre when i load it on primary screen its ok but when i load it on my secondart screen its not working – DeveloperSD Jul 15 '15 at 08:38
  • base on your post, you are using hard coded ui. but I am suggesting to use property window – dada Jul 15 '15 at 08:44
  • Initially i am using property window ad well which works perfectly fine with single monitor but when u load your form in secondary monitor then it is not displayes on centre using centerScreen property that is the problem. – DeveloperSD Jul 15 '15 at 09:01
0

You could write an extension method:

public static void MoveForm(this Form form, Screen screen = null)
{
    if(screen == null)
    {
        //If we have a single screen, we are not moving the form
        if(Screen.AllScreens.Length > 1) return;

        screen = Screen.AllScreens[1];
    }
    var bounds = screen.Bounds;

    form.Left = ((bounds.Left + bounds.Right) / 2) - (form.Width / 2);
    form.Top = ((bounds.Top + bounds.Bottom) / 2) - (form.Height / 2);            
}
Bruno Saboia
  • 332
  • 3
  • 18
0
    private void CenterOnTheCurrentScreen()
    {
        Rectangle workingArea = Screen.FromControl(this).WorkingArea;
        Point center = new Point((workingArea.Width - this.Width) / 2, (workingArea.Height - this.Height) / 2);

        this.Location = new Point(workingArea.X + center.X, workingArea.Y + center.Y);
    }
Mehdi
  • 2,194
  • 2
  • 25
  • 39