How do I center a window onscreen in C#?

后端 未结 12 2011
情书的邮戳
情书的邮戳 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:19
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace centrewindow
    {
        public partial class Form1 : Form
        {
            public struct RECT
            {
                public int Left;        // x position of upper-left corner
                public int Top;         // y position of upper-left corner
                public int Right;       // x position of lower-right corner
                public int Bottom;      // y position of lower-right corner
            }
    
            [DllImport("user32.dll")]
            public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
    
            [DllImport("user32.dll")]
            public static extern bool GetWindowRect(HandleRef hwnd, out RECT lpRect);
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                CentreWindow(Handle, GetMonitorDimensions());
            }
    
            private void CentreWindow(IntPtr handle, Size monitorDimensions)
            {
                RECT rect;
                GetWindowRect(new HandleRef(this, handle), out rect);
    
                var x1Pos = monitorDimensions.Width/2 - (rect.Right - rect.Left)/2;
                var x2Pos = rect.Right - rect.Left;
                var y1Pos = monitorDimensions.Height/2 - (rect.Bottom - rect.Top)/2;
                var y2Pos = rect.Bottom - rect.Top;
    
                SetWindowPos(handle, 0, x1Pos, y1Pos, x2Pos, y2Pos, 0);
            }
    
            private Size GetMonitorDimensions()
            {
                return SystemInformation.PrimaryMonitorSize;
            }
        }
    }
    

    Centers any window you can get the handle of

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

    In Windows Forms:

    this.StartPosition = FormStartPosition.CenterScreen;
    

    In WPF:

    this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    

    That's all you have to do...

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

    Use Location property of the form. Set it to the desired top left point

    desired x = (desktop_width - form_witdh)/2

    desired y = (desktop_height - from_height)/2

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

    Use this:

    this.CenterToScreen();  // This will take care of the current form
    
    0 讨论(0)
  • 2020-12-02 09:29

    A single line:

    this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
                              (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
    
    0 讨论(0)
  • 2020-12-02 09:38

     Centering a form in runtime

    1.Set following property of Form:
       -> StartPosition : CenterScreen
       -> WindowState: Normal

    This will center the form at runtime but if form size is bigger then expected, do second step.

    2. Add Custom Size after InitializeComponent();

    public Form1()
    {
        InitializeComponent();
        this.Size = new Size(800, 600);
    }
    
    0 讨论(0)
提交回复
热议问题