Form.StartPosition 属性

定义

获取或设置运行时窗体的起始位置。

C#
public System.Windows.Forms.FormStartPosition StartPosition { get; set; }

属性值

FormStartPosition

FormStartPosition,表示窗体的起始位置。

例外

指定值不在有效值范围内。

示例

以下示例创建一 Form 个新实例,并调用该方法 ShowDialog 以对话框形式显示窗体。 本示例设置窗体的外观和功能更改为对话框的属性FormBorderStyleCancelButtonAcceptButtonStartPosition 该示例还使用 Add 窗体 Controls 集合的方法添加两 Button 个控件。 该示例使用 HelpButton 该属性在对话框的标题栏中显示帮助按钮。

C#
public void CreateMyForm()
 {
    // Create a new instance of the form.
    Form form1 = new Form();
    // Create two buttons to use as the accept and cancel buttons.
    Button button1 = new Button ();
    Button button2 = new Button ();
   
    // Set the text of button1 to "OK".
    button1.Text = "OK";
    // Set the position of the button on the form.
    button1.Location = new Point (10, 10);
    // Set the text of button2 to "Cancel".
    button2.Text = "Cancel";
    // Set the position of the button based on the location of button1.
    button2.Location
       = new Point (button1.Left, button1.Height + button1.Top + 10);
    // Set the caption bar text of the form.   
    form1.Text = "My Dialog Box";
    // Display a help button on the form.
    form1.HelpButton = true;
 
    // Define the border style of the form to a dialog box.
    form1.FormBorderStyle = FormBorderStyle.FixedDialog;
    // Set the accept button of the form to button1.
    form1.AcceptButton = button1;
    // Set the cancel button of the form to button2.
    form1.CancelButton = button2;
    // Set the start position of the form to the center of the screen.
    form1.StartPosition = FormStartPosition.CenterScreen;
    
    // Add button1 to the form.
    form1.Controls.Add(button1);
    // Add button2 to the form.
    form1.Controls.Add(button2);
    
    // Display the form as a modal dialog box.
    form1.ShowDialog();
 }

注解

此属性使你可以在运行时显示窗体时设置窗体的起始位置。 可以通过设置 Location 属性或使用 Windows 指定的默认位置来手动指定窗体的位置。 还可以将窗体置于屏幕中心或窗体父窗体的中心,例如多文档界面 (MDI) 子窗体。

应在显示窗体之前设置此属性。 可以在调用 ShowShowDialog 方法或窗体构造函数之前设置此属性。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅