Win32 程序在啓動時激活前一個啓動程序的窗口

UWP 程序天生單實例。當然,新 API (10.0.17134)開始也提供了多實例功能。不過,傳統 Win32 程序可就要自己來控制單實例了。

本文介紹簡單的幾個 Win32 方法調用,使 Win32 程序也支持單實例。


激活之前進程的窗口

我們可以通過進程名稱找到此前已經啓動過的進程實例,如果發現,就激活它的窗口。

[STAThread]
static void Main(string[] args)
{
    var current = Process.GetCurrentProcess();
    var process = Process.GetProcessesByName(current.ProcessName).FirstOrDefault(x => x.Id != current.Id);
    if (process != null)
    {
        var hwnd = process.MainWindowHandle;
        ShowWindow(hwnd, 9);
        return;
    }

    // 啓動自己的主窗口,此部分代碼省略。
}

[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hwnd, uint nCmdShow);

你一定覺得那個 9 很奇怪,它是多個不同的 nCmdShow 的值:

  • 0 Hide
  • 1 Minimized
  • 2 Maximized
  • 9 Restore

另外,找到的窗口此時可能並不處於激活狀態。例如在 Windows 10 中,此窗口可能在其他桌面上。那麼我們需要添加額外的代碼將其顯示出來。

在前面的 ShowWindow 之後,再調用一下 SetForegroundWindow 即可將其激活到最前面來。如果在其他桌面,則會切換到對應的桌面。

[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
var hwnd = process.MainWindowHandle;
ShowWindow(hwnd, 9);
SetForegroundWindow(hwnd);

找到並激活窗口

以上方法適用於普通的主窗口。然而當窗口並不是進程的主窗口,或者 ShowInTaskBar 設爲了 false 的時候就不生效了(此時窗口句柄會改變)。

於是,我們需要改用其他的方式來查找窗口。

[STAThread]
static void Main(string[] args)
{
    var hwnd = FindWindow(null, "那個窗口的標題欄文字");
    if (hwnd != IntPtr.Zero)
    {
        ShowWindow(hwnd, 9);
        return;
    }

    // 啓動自己的主窗口,此部分代碼省略。
}

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

參考資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章