在 Mdi 子项之间切换
Toggle between Mdi Children
我有一个 MDI 窗体,我在其中创建了两个 MDIChild 窗体,cf1 和 cf2。
我有一个按钮,单击该按钮时,应在 cf1 和 cf2 之间切换 "focus"(即哪种形式为 "on top")。
在 MDI 父级的构造函数中,我分配了各自 cf1 和 cf2 的 MDIParent。然后我执行 cf1.Show() 然后 cf2.Show(),因此 cf2 结束 "on top" 或 "focused."
当我第一次按下切换按钮时,cf1 变为焦点,而 cf2 变为非活动状态。
在此之后更改 z 顺序的任何进一步尝试均失败。
我试过 Activate、ActivateMdiChild、TopMost 和 BringToFront,都没有成功。
using System;
using System.Windows.Forms;
namespace MDITest
{
public partial class Form1 : Form
{
private readonly ChildForm cf1 = new ChildForm();
private readonly ChildForm cf2 = new ChildForm();
public Form1()
{
InitializeComponent();
cf1.MdiParent = this;
cf2.MdiParent = this;
cf1.Text = "Window 1";
cf2.Text = "Window 2";
cf1.Show();
cf2.Show();
}
private void Child_WMSize(object sender, EventArgs e)
{
LblWindow1State.Text = $"Window 1 - {cf1.WindowState.ToString()}";
LblWindow2State.Text = $"Window 2 = {cf2.WindowState.ToString()}";
}
private void BtnFocus_Click(object sender, EventArgs e)
{
//if (ActiveMdiChild == cf1) cf2.Activate();
//if (ActiveMdiChild == cf2) cf1.Activate();
//if (ActiveMdiChild == cf1) ActivateMdiChild(cf2);
//if (ActiveMdiChild == cf2) ActivateMdiChild(cf1);
//if (ActiveMdiChild == cf1) cf2.TopMost = true;
//if (ActiveMdiChild == cf2) cf1.TopMost = true;
if (ActiveMdiChild == cf1) cf2.BringToFront();
if (ActiveMdiChild == cf2) cf1.BringToFront();
}
}
}
预期的结果是根据需要在两个表单之间切换焦点。实际结果是我只能从cf2改成cf1,反之不行
我什至尝试过霰弹枪方法:
if (ActiveMdiChild == cf1)
{
cf2.BringToFront();
cf2.Activate();
ActivateMdiChild(cf2);
cf2.TopMost = true;
cf1.TopMost = false;
}
if (ActiveMdiChild == cf2)
{
cf1.BringToFront();
cf1.Activate();
ActivateMdiChild(cf1);
cf1.TopMost = true;
cf2.TopMost = false;
}
我的结果没有变化。
您可以使用以下代码在任意数量的 Mdi 子级之间切换 windows:
if (MdiChildren.Length == 0)
return;
var i = Array.IndexOf(MdiChildren, ActiveMdiChild);
MdiChildren[(MdiChildren.Length + i + 1) % MdiChildren.Length].Activate();
或者
this.Controls.OfType<MdiClient>().First()
.SelectNextControl(ActiveMdiChild, true,true, true, true);
或
SendKeys.SendWait("^{TAB}"); // Control + TAB as well as Control + F6
我有一个 MDI 窗体,我在其中创建了两个 MDIChild 窗体,cf1 和 cf2。 我有一个按钮,单击该按钮时,应在 cf1 和 cf2 之间切换 "focus"(即哪种形式为 "on top")。 在 MDI 父级的构造函数中,我分配了各自 cf1 和 cf2 的 MDIParent。然后我执行 cf1.Show() 然后 cf2.Show(),因此 cf2 结束 "on top" 或 "focused." 当我第一次按下切换按钮时,cf1 变为焦点,而 cf2 变为非活动状态。 在此之后更改 z 顺序的任何进一步尝试均失败。
我试过 Activate、ActivateMdiChild、TopMost 和 BringToFront,都没有成功。
using System;
using System.Windows.Forms;
namespace MDITest
{
public partial class Form1 : Form
{
private readonly ChildForm cf1 = new ChildForm();
private readonly ChildForm cf2 = new ChildForm();
public Form1()
{
InitializeComponent();
cf1.MdiParent = this;
cf2.MdiParent = this;
cf1.Text = "Window 1";
cf2.Text = "Window 2";
cf1.Show();
cf2.Show();
}
private void Child_WMSize(object sender, EventArgs e)
{
LblWindow1State.Text = $"Window 1 - {cf1.WindowState.ToString()}";
LblWindow2State.Text = $"Window 2 = {cf2.WindowState.ToString()}";
}
private void BtnFocus_Click(object sender, EventArgs e)
{
//if (ActiveMdiChild == cf1) cf2.Activate();
//if (ActiveMdiChild == cf2) cf1.Activate();
//if (ActiveMdiChild == cf1) ActivateMdiChild(cf2);
//if (ActiveMdiChild == cf2) ActivateMdiChild(cf1);
//if (ActiveMdiChild == cf1) cf2.TopMost = true;
//if (ActiveMdiChild == cf2) cf1.TopMost = true;
if (ActiveMdiChild == cf1) cf2.BringToFront();
if (ActiveMdiChild == cf2) cf1.BringToFront();
}
}
}
预期的结果是根据需要在两个表单之间切换焦点。实际结果是我只能从cf2改成cf1,反之不行
我什至尝试过霰弹枪方法:
if (ActiveMdiChild == cf1)
{
cf2.BringToFront();
cf2.Activate();
ActivateMdiChild(cf2);
cf2.TopMost = true;
cf1.TopMost = false;
}
if (ActiveMdiChild == cf2)
{
cf1.BringToFront();
cf1.Activate();
ActivateMdiChild(cf1);
cf1.TopMost = true;
cf2.TopMost = false;
}
我的结果没有变化。
您可以使用以下代码在任意数量的 Mdi 子级之间切换 windows:
if (MdiChildren.Length == 0)
return;
var i = Array.IndexOf(MdiChildren, ActiveMdiChild);
MdiChildren[(MdiChildren.Length + i + 1) % MdiChildren.Length].Activate();
或者
this.Controls.OfType<MdiClient>().First()
.SelectNextControl(ActiveMdiChild, true,true, true, true);
或
SendKeys.SendWait("^{TAB}"); // Control + TAB as well as Control + F6