How do I center a window onscreen in C#?

后端 未结 12 2012
情书的邮戳
情书的邮戳 2020-12-02 09:05

I need a way to center the current window. So for example, if a user pushes a button, I want the window to center itself onscreen. I know you can use the startposition prope

相关标签:
12条回答
  • 2020-12-02 09:40

    In case of multi monitor and If you prefer to center on correct monitor/screen then you might like to try these lines:

    // Save values for future(for example, to center a form on next launch)
    int screen_x = Screen.FromControl(Form).WorkingArea.X;
    int screen_y = Screen.FromControl(Form).WorkingArea.Y;
    
    // Move it and center using correct screen/monitor
    Form.Left = screen_x;
    Form.Top = screen_y;
    Form.Left += (Screen.FromControl(Form).WorkingArea.Width - Form.Width) / 2;
    Form.Top += (Screen.FromControl(Form).WorkingArea.Height - Form.Height) / 2;
    
    0 讨论(0)
  • 2020-12-02 09:43

    Might not be completely relevant to the question. But maybe can help someone.

    Center Screen non of the above work for me. Reason was I was adding controls dynamically to the form. Technically when it centered it was correct , based on the form before adding the controls.

    So here was my solution. ( Should work with both scenarios )

    int x = Screen.PrimaryScreen.Bounds.Width - this.PreferredSize.Width;
    int y = Screen.PrimaryScreen.Bounds.Height - this.PreferredSize.Height;
    
    this.Location = new Point(x / 2, y / 2);
    

    So you will notice that I am using "PreferredSize" instead of just using Height / Width. The preferred size will hold the value of the form after adding the controls. Where Height / Width won't.

    Hope this helps someone .

    Cheers

    0 讨论(0)
  • 2020-12-02 09:44
    1. Using the Property window

      Select form → go to property window → select "start position" → select whatever the place you want.

      "

    2. Programmatically

      Form form1 = new Form(); form1.StartPosition = FormStartPosition.CenterScreen; form1.ShowDialog();

      Note: Do not directly call Form.CenterToScreen() from your code. Read here.

    0 讨论(0)
  • 2020-12-02 09:44

    You can use the Screen.PrimaryScreen.Bounds to retrieve the size of the primary monitor (or inspect the Screen object to retrieve all monitors). Use those with MyForms.Bounds to figure out where to place your form.

    0 讨论(0)
  • 2020-12-02 09:45

    Use Form.CenterToScreen() method.

    0 讨论(0)
  • 2020-12-02 09:46

    If you want to center your windows during runtime use the code below, copy it into your application:

    protected void ReallyCenterToScreen()
    {
      Screen screen = Screen.FromControl(this);
    
      Rectangle workingArea = screen.WorkingArea;
      this.Location = new Point() {
        X = Math.Max(workingArea.X, workingArea.X + (workingArea.Width - this.Width) / 2),
        Y = Math.Max(workingArea.Y, workingArea.Y + (workingArea.Height - this.Height) / 2)};
    }
    

    And finally call the method above to get it working:

    ReallyCenterToScreen();
    
    0 讨论(0)
提交回复
热议问题