[英]Bringing up to the front a program's secondary window without changing focus from the primary window
因此,我有一个用于我的程序的主窗口/表单,以及一个应要求显示/隐藏的犯罪伙伴窗口/表单。 当主窗口被调用到前面时(例如通过 Alt-Tab),我希望它们都在前面。
尝试使用主窗口的 Activated 函数并使用合作伙伴窗口的Focused
或Activate
函数会在所述合作伙伴窗口为Visible
时将焦点调用到合作伙伴窗口,问题是在这一点上,每次我尝试单击主窗口(你知道,使用它),我专注于它,这意味着它将焦点转移到合作伙伴窗口。
如何在保持专注于主窗口的同时将合作伙伴窗口置于最前面?
怀着希望,
拉佐73
我发现这个一线解决方案有效但肯定可以改进或调整:
private Timer ActivatedTimer;
private bool ActivatedMutex;
public FormTest()
{
InitializeComponent();
ActivatedTimer = new Timer();
ActivatedTimer.Interval = 10;
ActivatedTimer.Tick += (sender, e) =>
{
ActivatedTimer.Stop();
try
{
ActivatedMutex = true;
BringToFront();
}
finally
{
ActivatedMutex = false;
}
};
}
private void FormTest_Activated(object sender, EventArgs e)
{
if ( ActivatedMutex ) return;
try
{
ActivatedMutex = true;
foreach ( Form form in Application.OpenForms )
if ( form != this && form.Visible )
form.BringToFront();
ActivatedTimer.Start();
}
finally
{
ActivatedMutex = false;
}
}
也许有一些 Win32API 可以做得更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.