[英]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.