1

快速的问题,希望一个简单的解决方案。我对 C# 有点陌生,并试图在第二个表单打开时将其居中显示在辅助屏幕上。到目前为止,我可以让它在第二个屏幕上打开没问题,但它位于左上角,我无法让它居中。我知道Location = Screen.AllScreens[1].WorkingArea.Location;将把它放在所述工作区的左上角。我想知道是否有办法(基本上)将其更改.Location为其他无论实际屏幕尺寸如何都可以居中的东西?这将适用于具有不同屏幕尺寸的多个不同系统。这是我到目前为止的代码。

在第一个表格上。

public partial class FrmPrompt : Form
{
    public FrmPrompt()
    {
        InitializeComponent();
    }

    private void ButNo_Click(object sender, EventArgs e)
    {
        frmConfirm confirm = new frmConfirm();
        Screen[] screens = Screen.AllScreens;
        lblConfirmMsg.Text = "Please Wait For Customer To Confirm...";
        butContinue.Hide();
        confirm.Show();
    }
}

第二种形式:

public partial class frmConfirm : Form
{
    public frmConfirm()
    {
        InitializeComponent();
        Location = Screen.AllScreens[1].WorkingArea.Location;
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

谢谢!

4

2 回答 2

2

CenterScreen将在当前屏幕上找到表单,因此如果您FrmPrompt在第二个屏幕上,当您单击时ButNo- 这将起作用。但我认为这不是你要求的。

更重要的是,CenterScreen 将覆盖您在Show 方法调用之前设置的 from Location 的任何位置设置。所以我建议重写 OnShown 方法frmConfirm

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    var area = Screen.AllScreens[1].WorkingArea;
    var location = area.Location;

    location.Offset((area.Width - Width) / 2, (area.Height - Height) / 2);
    Location = location;
}
于 2018-05-22T04:13:13.600 回答
1

在第一种形式上试试这个,不需要在第二种形式中设置任何东西。

        //put it after this line: frmConfirm confirm = new frmConfirm();
        confirm.StartPosition = FormStartPosition.CenterScreen;
于 2018-05-22T03:31:54.770 回答