C#中的StartPosition问题? [英] StartPosition problem in C#?

查看:272
本文介绍了C#中的StartPosition问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在中心屏幕中显示WinApp,因此我将StartPosition属性设置为CenterScreen,但是窗口未显示在屏幕中心.

I wanna show my WinApp in Center Screen, so I set StartPosition property to CenterScreen but the window doesn't show in center of screen.

这是怎么了?我想念什么吗?

What's wrong with it ? Am I missing something?

P.S:
我从一个主窗口显示一个窗口,并带有一个按钮.

P.S:
I show the window from a main window and with a button.


我用来显示窗口的代码.


The code that I'm using to show the window.

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);
    }
}

推荐答案

FormStartPosition.CenterScreen可能会出现问题,如果表单重新缩放,将自身调整为视频DPI设置.将此代码粘贴到您的表单中进行修复:

FormStartPosition.CenterScreen can be a problem if the form rescales, adjusting itself to the video DPI setting. Paste this code in your form to fix it:

    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);
    }

这篇关于C#中的StartPosition问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆