Comment centrer une fenêtre à l’écran en C #?

J’ai besoin d’un moyen pour centrer la fenêtre actuelle. Par exemple, si un utilisateur appuie sur un bouton, je souhaite que la fenêtre se centre à l’écran. Je sais que vous pouvez utiliser la propriété startposition, mais je ne peux pas trouver un moyen de l’utiliser autrement que lorsque l’application démarre pour la première fois. Alors, comment centrer le formulaire à l’écran?

Utilisez la méthode Form.CenterToScreen () .

  1. Utiliser la fenêtre de propriétés

    Sélectionnez le formulaire → allez dans la fenêtre de propriétés → sélectionnez “position de départ” → sélectionnez le lieu de votre choix.

  2. Par programme

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

    Remarque: N’appelez pas directement Form.CenterToScreen () à partir de votre code. Lire ici

Une seule ligne:

 this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2, (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2); 

Dans Windows Forms:

 this.StartPosition = FormStartPosition.CenterScreen; 

Dans WPF:

 this.WindowStartupLocation = WindowStartupLocation.CenterScreen; 

C’est tout ce que vous avez à faire …

Si vous souhaitez centrer vos fenêtres pendant l’exécution, utilisez le code ci-dessous, copiez-le dans votre 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)}; } 

Et enfin, appelez la méthode ci-dessus pour la faire fonctionner:

 ReallyCenterToScreen(); 

Centrer un formulaire à l’exécution

1.Définissez la propriété suivante du formulaire:
-> StartPosition: CenterScreen
-> WindowState: Normal

Cela centrera le formulaire à l’exécution, mais si la taille du formulaire est plus grande que prévu, effectuez la deuxième étape.

2. Ajoutez une taille personnalisée après InitializeComponent ();

 public Form1() { InitializeComponent(); this.Size = new Size(800, 600); } 
 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; } } } 

Centre toute fenêtre que vous pouvez obtenir la poignée de

Utilisez la propriété Emplacement du formulaire. Réglez-le sur le point supérieur gauche souhaité

x = désiré (desktop_width – form_witdh) / 2

désiré y = (desktop_height – from_height) / 2

Utilisez ceci:

 this.CenterToScreen(); // This will take care of the current form 

Vous pouvez utiliser Screen.PrimaryScreen.Bounds pour récupérer la taille du moniteur principal (ou inspecter l’object Screen pour récupérer tous les moniteurs). Utilisez ceux avec MyForms.Bounds pour savoir où placer votre formulaire.