52

Win32 程序在启动时激活前一个启动程序的窗口

 5 years ago
source link: https://walterlv.github.io/post/show-previous-process-instance-window-when-startup.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

发布于 2018-08-05 21:48更新于 2018-08-05 13:54

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 的值:

Hide
Minimized
Maximized
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);

参考资料

本文会经常更新,请阅读原文: https://walterlv.github.io/post/show-previous-process-instance-window-when-startup.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

VrIr6j.png!web 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://walterlv.github.io ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK