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;