简体   繁体   English

激活时如何将WinForm完全置于前面

[英]How to completely bring to front a WinForm when activated

I have two dialogs, FormA and FormB .我有两个对话框, FormAFormB I use the following code to show (modeless) FormB .我使用以下代码显示(无模式) FormB The code is a button click executed from FormA .该代码是从FormA执行的按钮单击。

    private void button_Click(object sender, EventArgs e)
    {
        FormB fB = new FormB();
        fB.Show(this); // FormA is the owner of FormB
    }

The problem is that when FormB is over FormA on the screen, if I click FormA , it is activated, but not brought to front.问题是当FormB在屏幕上超过FormA时,如果我单击FormA ,它会被激活,但不会被带到前面。 Actually FormB is always over FormA实际上 FormB 总是在 FormA 之上

形式

Do you know why, and how to change this behavior, without remove the owner property?您知道为什么以及如何在不删除所有者属性的情况下更改此行为吗?

NOTE: This is a simplification of my problem.注意:这是对我的问题的简化。 In the real problem, FormA is a Windows Explorer window, and FormB is a managed WinForm, but the behavior is the same.在实际问题中,FormA 是 Windows Explorer window,FormB 是托管 WinForm,但行为相同。 If I do not pass the IWin32Window to Show() , it works fine, but If I close A, B is not closed and it does not respond to events (see the following entry ).如果我没有将 IWin32Window 传递给Show() ,它可以正常工作,但如果我关闭 A,B 不会关闭并且它不会响应事件(请参阅以下条目)。

You cant do this without removing the owner property.如果不删除所有者属性,您将无法执行此操作。

From Documentation: Owned forms are also never displayed behind their owner form.来自文档:拥有的 forms 也永远不会显示在其所有者表单后面。

Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx来源: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx

For your specific problem why do you not listen for the Close Event and then explicitly close your own form?对于您的具体问题,您为什么不收听 Close 事件然后明确关闭您自己的表单?

You can set TopMost property to true.您可以将TopMost属性设置为 true。

A hack is to set WindowState = FormWindowState.Minimized in the OnDeactivate method of FormB (orverride it).一个技巧是在 FormB 的 OnDeactivate 方法中设置 WindowState = FormWindowState.Minimized (或覆盖它)。

  protected override void OnDeactivate(EventArgs e)
  {
        base.OnDeactivate(e);
        this.WindowState = FormWindowState.Minimized;
  }

I don't know if that's what you would like.我不知道这是否是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM