[英]Bringing up to the front a program's secondary window without changing focus from the primary window
So I have a main window/form for my program, and a partner-in-crime window/form, shown/hidden on request.因此,我有一个用于我的程序的主窗口/表单,以及一个应要求显示/隐藏的犯罪伙伴窗口/表单。 I would like both of them to be in the front when the main window is called to the front (eg by Alt-Tab).
当主窗口被调用到前面时(例如通过 Alt-Tab),我希望它们都在前面。
Trying to use the main window's Activated function and using the partner window's Focused
or Activate
functions calls focus to the partner window when said partner window is Visible
, and the problem is that at that point, every time I try to click on the main window (y'know, to use it), I focus on it, meaning that it brings focus to the partner window instead.尝试使用主窗口的 Activated 函数并使用合作伙伴窗口的
Focused
或Activate
函数会在所述合作伙伴窗口为Visible
时将焦点调用到合作伙伴窗口,问题是在这一点上,每次我尝试单击主窗口(你知道,使用它),我专注于它,这意味着它将焦点转移到合作伙伴窗口。
How do I bring a partner window up to the front while keeping focus on the main window?如何在保持专注于主窗口的同时将合作伙伴窗口置于最前面?
With hope,怀着希望,
radzo73拉佐73
I have found this first-line solution that works but can certainly be improved or adapted:我发现这个一线解决方案有效但肯定可以改进或调整:
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;
}
}
Perhaps there are some Win32APIs to do that better.也许有一些 Win32API 可以做得更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.