【问题标题】:StartPosition problem in C#?C# 中的起始位置问题?
【发布时间】:2011-01-31 19:36:18
【问题描述】:

我想在屏幕中心显示我的 WinApp,所以我将 StartPosition 属性设置为 CenterScreen 但窗口没有显示在屏幕中心。

它有什么问题?我错过了什么吗?

附注:
我从一个主窗口和一个按钮显示窗口。

编辑:
我用来显示窗口的代码。

Form_CO form_CO = new Form_CO();
void button_CO_Click(object sender, EventArgs e)
{
    try
    {
        //StaticVariables.Form_CO_IsShown is to prevent opening the same multiple windows
        if (!StaticVariables.Form_CO_IsShown)
        {
            form_CO = new Form_CO();
            form_CO.Show();
            StaticVariables.Form_CO_IsShown = true;
        }
        else
        {
            form_CO.WindowState = FormWindowState.Normal;
            form_CO.Activate();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

【问题讨论】:

  • 很可能,你做错了什么。

标签: c# visual-studio-2008 winapp


【解决方案1】:

FormStartPosition.CenterScreen 可能会在表单重新缩放以适应视频 DPI 设置时出现问题。将此代码粘贴到您的表单中以修复它:

    protected override void OnLoad(EventArgs e) {
        var scr = Screen.FromPoint(this.Location);
        this.Left = scr.WorkingArea.Left + (scr.WorkingArea.Width - this.Width) / 2;
        this.Top = scr.WorkingArea.Top + (scr.WorkingArea.Height - this.Height) / 2;
        base.OnLoad(e);
    }

【讨论】:

  • 非常感谢兄弟,我的问题已经用上面的代码解决了。
猜你喜欢
  • 2016-07-07
  • 2018-10-31
  • 1970-01-01
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多