简体   繁体   English

如何在C#Windows窗体中的特定位置放置打开窗体?

[英]How to position the opening form at specific location in C# Windows Forms?

The Location property in the form is set to 0,0 (Properties Window). 表单中的Location属性设置为0,0(“属性窗口”)。 However, the form doesn't open at the specified location. 但是,该窗体不会在指定的位置打开。 Am I missing something? 我想念什么吗?

You need to set StartPosition to manual to make the form set start position to the value in Location Property. 您需要将StartPosition设置为manual,以使表单将开始位置设置为Location Property中的值。

public Form1()
{
    InitializeComponent();
    this.StartPosition = FormStartPosition.Manual;
    this.Location = new Point(0, 0);
}

Intelisense Summary for FormStartPosition.Manual 智能感知摘要FormStartPosition.Manual

FormStartPosition FormStartPosition.Manual FormStartPosition FormStartPosition.Manual

The position of the form is determined by the System.Windows.Forms.Control.Location property . 窗体的位置由System.Windows.Forms.Control.Location属性确定

By default the start position is set to be WindowsDefaultLocation which will cause the form to ignore the location you are setting. 默认情况下,起始位置设置为WindowsDefaultLocation,这将导致表单忽略您正在设置的位置。 To easily have the set location enforced, change the StartPosition to Manual. 要轻松执行设置的位置,请将StartPosition更改为Manual。

StartPosition属性图片

Try: 尝试:

this.Location = new Point(Screen.PrimaryScreen.Bounds.X, //should be (0,0)
                          Screen.PrimaryScreen.Bounds.Y);
this.TopMost = true;
this.StartPosition = FormStartPosition.Manual;

Setting the Location at 0,0 has no effect if you forget to set StartPosition to FormStartPosition.Manual 如果忘记将StartPosition设置为FormStartPosition,则将Location设置为0,0无效。

This property enables you to set the starting position of the form when it is displayed at run time. 使用此属性,可以设置在运行时显示窗体的开始位置。 The form's position can be specified manually by setting the Location property or use the default location specified by Windows. 可以通过设置Location属性来手动指定表单的位置,也可以使用Windows指定的默认位置。 You can also position the form to display in the center of the screen or in the center of its parent form for forms such as multiple-document interface (MDI) child forms. 对于诸如多文档界面(MDI)子窗体之类的窗体,您还可以将窗体放置在屏幕的中心或其父窗体的中心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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