当父母没有焦点时,CenterParent 不工作 [英] CenterParent not working when parent does not have focus

查看:30
本文介绍了当父母没有焦点时,CenterParent 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有计时器的表单.计时器会加载一个对话框以显示该表单正忙.当主窗体没有焦点时,忙碌的窗体会加载在电脑屏幕的左上角(这不是预期的).当表单具有焦点时,它会按预期工作.如果将 ShowInTaskbar 设置为 true,那么即使主窗体没有焦点,它也会按预期工作.这里发生了什么,我该如何解决?

I have a form with a timer.The timer loads a dialog to show that the form is busy. When the main form does not have focus, the busy form is loaded in the top left of the pc screen (Which is not expected). When the form has focus it works as expected. If set ShowInTaskbar to true then it does work as expected even when the main form does not have focus. What is going on here and how can i fix ?

C# 代码

namespace CenterParentIssue
{

    public partial class Main : Form
    {
        WaitingForm formWindowsWaitingForm;
        public Main()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            formWindowsWaitingForm = new WaitingForm();
            formWindowsWaitingForm.StartPosition = FormStartPosition.CenterParent;
            formWindowsWaitingForm.ShowInTaskbar = false;
            formWindowsWaitingForm.ShowDialog();
        }
    }
}

推荐答案

你需要给 WaitingForm 一个父上下文.ShowDialog() 有一个重载,它以一个 Window 作为参数,这个窗口是父/所有者.

You need to give the WaitingForm a parent context. ShowDialog() has an overload which takes a Window as an argument, this window is the parent/owner.

//...
formWindowsWaitingForm.ShowDialog(this);
//...

这篇关于当父母没有焦点时,CenterParent 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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