重新定位Windows窗体(位置)

Dug*_*ugi 3 c# windows window winforms

当Windows窗体打开而不是左上角时,如何将Windows窗体放置在屏幕的右下角?

情况:我有一个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();
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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();
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 5

你需要知道两件事.首先是屏幕的工作区域,您将在其中显示表单.工作区域是屏幕的大小减去该屏幕上显示的任务栏.您可以使用Screen.WorkingArea属性.

其次是窗口的实际大小.这通常不是表单的设计大小,您的用户可能已经更改了窗口标题栏中文本的大小,或者可能在与您的DPI设置不同的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();
Run Code Online (Sandbox Code Playgroud)

在窗口变得可见之前重新定位窗口.表单的StartPosition属性无关紧要.