Jak wyśrodkować okno na ekranie w C#?

Potrzebuję sposobu na wyśrodkowanie bieżącego okna. Na przykład, jeśli użytkownik naciśnie przycisk, Chcę, aby okno wyśrodkowało się na ekranie. Wiem, że możesz użyć właściwości startposition, ale nie mogę znaleźć sposobu, aby go użyć inaczej niż przy pierwszym uruchomieniu aplikacji. Jak wyśrodkować formularz na ekranie?

Author: Peter Mortensen, 2011-01-05

10 answers

Użyj Formularza .Metoda CenterToScreen () .

 165
Author: dzendras,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-12 04:25:08
  1. Korzystanie z właściwości okno

    Wybierz formularz → przejdź do okna Właściwości → wybierz "pozycja startowa" → Wybierz dowolne miejsce.

    "

  2. Programowo

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

    Uwaga: Nie wywołaj bezpośrednio formularza.CenterToScreen() z twojego kodu. Przeczytaj tutaj .

 113
Author: Nayana Adassuriya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-09 02:37:36

Pojedyncza linia:

this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
                          (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
 27
Author: harveyt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-10 16:22:08

W Windows Forms:

this.StartPosition = FormStartPosition.CenterScreen;

W WPF:

this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
To wszystko, co musisz zrobić...
 25
Author: Saimon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-22 17:09:00

Jeśli chcesz wyśrodkować System windows podczas wykonywania, użyj poniższego kodu, skopiuj go do aplikacji:

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

I na koniec wywołaj powyższą metodę, aby ją uruchomić:

ReallyCenterToScreen();
 15
Author: Sarsur.A,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-21 15:04:35

Centrowanie formularza w runtime

1.Ustaw następującą właściwość formularza:
- > StartPosition : CenterScreen
- > WindowState: Normal

Spowoduje wyśrodkowanie formularza w czasie wykonywania, ale jeśli rozmiar formularza jest większy niż oczekiwany, wykonaj drugi krok.

2. Dodaj niestandardowy rozmiar po InitializeComponent ();

public Form1()
{
    InitializeComponent();
    this.Size = new Size(800, 600);
}
 8
Author: Faiz Siddiqui,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-11 04:58:02
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;
        }
    }
}

Wyśrodkowuje każde okno, z którego można uzyskać uchwyt

 6
Author: Rob,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-05 08:14:54

Użyj właściwości Location formularza. Ustaw go na żądany lewy górny punkt

X = (desktop_width - form_witdh)/2

Y = (desktop_height-from_height) / 2

 2
Author: Sarwar Erfan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-05 07:59:03

Użyj tego:

this.CenterToScreen();  // This will take care of the current form
 2
Author: UJS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-22 17:13:02

Możesz użyć Screen.PrimaryScreen.Bounds, aby pobrać rozmiar głównego monitora (lub sprawdzić obiekt Screen, Aby pobrać wszystkie monitory). Użyj tych z MyForms.Bounds, aby dowiedzieć się, gdzie umieścić formularz.

 1
Author: C.Evenhuis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-05 07:57:04