重新定位Windows窗体(位置)

3
当窗体打开时,我该如何将其定位到屏幕右下角,而不是左上角?
情境:我有一个名为Form1的窗体,实际上它并没有作为窗体执行任何操作,我只是使用它的上下文菜单(我的应用仅从托盘运行)。因此,大部分主要运行代码都放在了Form1类中。当用户点击上下文菜单时,它将进行一些处理,最终它将显示Form2。因此,Form2被Form1的上下文菜单项打开/调用。在这种情况下,我该如何更改Form2的位置?
Form1.cs(触发Form2的部分):
private void menu_upload_file_Click(object sender, EventArgs e)
{
    DialogResult dialogOpened = openFileDialog1.ShowDialog();
    if (dialogOpened == DialogResult.OK)
    {
        string filename = openFileDialog1.FileName;

        using (var client = new WebClient())
        {
            var response = client.UploadFile("http://localhost/imgitv3/upload.php?submit=true&action=upload&request=app", "POST", filename);
            // string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + Path.DirectorySeparatorChar + "response.txt";

            if (response != null)
            {
                string responseContent = System.Text.Encoding.ASCII.GetString(response);
                Form2 linkWindow = new Form2();

                if (isURL(responseContent))
                {
                    linkWindow.toTextBox(responseContent);
                    linkWindow.Show();
                }
            }
        }
    }
}

Form2.Designer.cs

// 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CausesValidation = false;
            this.ClientSize = new System.Drawing.Size(419, 163);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MaximumSize = new System.Drawing.Size(435, 202);
            this.MinimizeBox = false;
            this.MinimumSize = new System.Drawing.Size(435, 202);
            this.Name = "Form2";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "IMGit Image Uploader";
            this.TopMost = true;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.Form2_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

页面的右下角。什么页面?这段代码不在Web浏览器中运行。订阅Load事件以便您知道窗口的实际大小,设置Location属性。 - Hans Passant
抱歉,我说页面是因为我习惯用PHP编程,而且我只学C#两周了。我该如何实现你刚才所描述的?我需要一个示例。 - aborted
4个回答

5
有两件事情你需要知道。第一是你要在屏幕上显示表单的工作区域。工作区域是屏幕大小减去该屏幕上显示的任务栏大小。你可以使用Screen.WorkingArea属性来获取该大小。
第二是窗口的实际大小。通常不是窗体的设计大小,用户可能已经更改了窗口标题栏中文本的大小,或者使用了不同于你的视频适配器的DPI设置。在窗体的Load事件触发之前,你需要等待以确定该大小。
因此,如果您希望在主监视器上显示该表单,请使您的代码如下:
        var frm = new Form2();
        frm.Load += (s, ea) => {
            var wa = Screen.PrimaryScreen.WorkingArea;
            frm.Location = new Point(wa.Right - frm.Width, wa.Bottom - frm.Height);
        };
        frm.Show();

在窗口可见之前重新定位窗口。表单的StartPosition属性并不重要。


2
你可以设置表单属性 StartPosition=Manual,然后将 form.leftform.top 属性设置为所需的值。在显示对话框之前,应该先设置它们。
Form2 linkWindow = new Form2();
linkWindow.StartPosition = FormStartPosition.Manual;
linkWindow.Left = 200;
linkWindow.Top = 200;

if (isURL(responseContent))
{
  linkWindow.toTextBox(responseContent);
  linkWindow.Show();
}

使用左和上值进行操作

如果您能展示一些代码就好了。这就是我在这里提问的原因,因为我不知道该如何编写它。 - aborted
感谢提供示例。但是,在不同的屏幕分辨率下,硬编码XY坐标会有所不同吗?我的意思是,对于1440x900分辨率和1024x768分辨率,200和200会不同吗?我需要一个通用的解决方案。 - aborted
@Aborted,你需要使用某种类型的参考对象来确保一致的放置。如果你是从系统托盘运行的,你可以使用托盘图标作为一个参考点。 - Lee Harrison

2

从Form2中挂接FormLoad事件:

Form2 linkWindow = new Form2();
linkWindow.FormLoad += Form2_Load;

然后在任意位置添加这个方法:

    private void Form2_Load(object sender, EventArgs e)
    {
        this.StartPosition = FormStartPosition.Manual;
        this.Location = new Point(400, 400);  //set x,y to where you want it to appear
    }

将X和y的值更改为您想要定位窗口的任何位置。


2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接