C # specify the display position of the window

Source: Internet
Author: User

When starting a program, we want the position displayed in the window to be in the center of the screen. You can set it as follows:

MainFormmainForm = newMainForm ();

MainForm. StartPosition = FormStartPosition. CenterScreen;

MainForm. Show ();

If you must log on before the main window can be operated, the logon window is displayed. In this case, the main window appears after the logon window and cannot be operated.

MainFormmainForm = newMainForm ();

LoginFormdlg = newLoginForm ();

Dlg. ShowDialog ();

The ShowDialog method indicates that you must complete the dlg window before you can operate the main form.

If you want to log on to the center of the Main Window, set the following before display:

Dlg. StartPosition = FormStartPosition. CenterParent;

Dlg. ShowDialog ();

The premise is that the main form must be defined and displayed first. Otherwise, the logon form may not be able to find the parent form.

In addition, you can also manually set the position displayed in the window, that is, the window coordinate.

First, you must set the display position of the form to manual.

Dlg. StartPosition = FormStartPosition. Manual;

Then obtain the screen resolution, that is, the screen size of the monitor.

IntxWidth = SystemInformation. PrimaryMonitorSize. Width; // obtain the display Width.

IntyHeight = SystemInformation. PrimaryMonitorSize. Height; // Height

Then define the window position. Take the main form as an example.

MainForm. Location = new Point (xWidth/2, yHeight/2); // the width and height of the form must be halved.

MainForm. Show ();

After these three steps, a form that accurately locates on the screen is displayed.

When using the Point class, you must first include it and write it at the beginning of the program:

UsingSystem. Drawing;

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.