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).
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.
How do I bring a partner window up to the front while keeping focus on the main window?
With hope,
radzo73
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.