简体   繁体   中英

How can i set the Form to be in the Center of the Screen?

I'm using this code:

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    this.Show();
    this.WindowState = FormWindowState.Normal;
    //this.Location = new Point(form1_location_on_x, form1_location_on_y);
    //this.StartPosition = FormStartPosition.CenterScreen;

Either the line

this.Location = new Point(form1_location_on_x, form1_location_on_y);

or the line

this.StartPosition = FormStartPosition.CenterScreen;

are working when I'm on my original screen resolution 1920x1080, but once I'm changing the resolution to 1024x768, the Form is on the right bottom corner not hidden I see it all but it's not in the center.

form1_location_on_x and on_y are:

form1_location_on_x = this.Location.X;
form1_location_on_y = this.Location.Y;

The question is what should I do to make it work on any other resolution like 1024x768 or any others? I tried many changes but nothing worked so far.

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

Make sure that you set StartPosition = FormStartPosition.Manual;

Tested and working with 1920x1080 and 1024 x 768

You could calculate the top and left position of your form using this formula:

int formWidth = yourForm.Width;
int formHeight = yourForm.Height;
int screenH = (Screen.PrimaryScreen.WorkingArea.Top + 
              Screen.PrimaryScreen.WorkingArea.Height) / 2;
int screenW = (Screen.PrimaryScreen.WorkingArea.Left + 
              Screen.PrimaryScreen.WorkingArea.Width) / 2;

int top = screenH - formWidth / 2;
int left = screenW - formHeight / 2;
yourForm.Location = new Point(top, left);

Of course, these days, you have the problem of dual monitors.
I don't know if you want your form to appear always on the primary screen or you want the form appear in the current screen (the one where the form is currently displayed). In this second case you need to find where your form is displayed

private void CenterForm(Form yuorForm)
{    
    foreach(var s in Screen.AllScreens)
    {
       if(s.WorkingArea.Contains(yourForm.Location))
       {
            int screenH = s.WorkingArea.Height / 2;
            int screenW = s.WorkingArea.Width / 2;

            int top = (screenH + s.WorkingArea.Top) - formWidth / 2;
            int left = (screenW + s.WorkingArea.Left) - formHeight / 2;
            yourForm.Location = new Point(top, left);
            break;
       }
    }
}

EDIT: Thanks to @alex I will complete the answer with the information on SystemEvents class

If you want to be notified by the system when the user suddenly change the resolution of your screen you could subscribe to the event SystemEvents.DisplaySettingsChanged ( using Microsoft.Win32; needed)

SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged);  

and then handle the reposition of your form in the event

// This method is called when the display settings change.
void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
    RecenterForm(yourForm);
}

try using one of these after the resize:

this.CenterToScreen();

or

this.CenterToParent();

You can use StartPosition property of Form objects. It determines the position of a form. Set it's value to CenterScreen if you want your form to open in the center of the screen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM