如何检测如果应用程序通过单击任务栏启动单击、任务栏、应用程序

2023-09-06 07:56:16 作者:把班主任捐给灾区

我有几个窗口的应用程序,其中仅主窗口出现在任务栏中。

I have an application with several windows where only the main window appears in the taskbar.

通过在任务栏我想我所有的应用程序窗口显示在其他任何打开的窗口顶部的图标上点击。

With a click on the icon in the taskbar I want all my application windows to show up on top of any other open windows.

我试过Form_Activated事件,但是这也解雇了,如果应用程序是通过直接点击主窗口激活。

I tried Form_Activated event but this is also fired if the application is activated by a direct click in the main window.

因此​​,我怎么检测,如果有人只是从任务栏启动的应用程序?

So how do I detect if someone activated the application just from the taskbar ?

推荐答案

它已经运作的方式。然而,你必须使用Show(业主)的过载,这样的'孩子'窗口总是在主窗口的顶部,不能迷失其他应用程序的窗口的后面。几乎所有的商业程序的工作原理类似的。

It already works that way. You do however have to use the Show(owner) overload so that the 'child' windows are always on top of your main window and cannot get lost behind the window of another application. Almost any commercial program works like that.

激活原因之间的区别是可能的,Windows提供了$ P $的窗口句柄pviously活动窗口。然后你可以检查,看它是否是你自己的窗口之一。这不是在事件不过可用,你必须陷阱自己的消息。像这样的:

Distinguishing between Activated reasons is possible, Windows provides the window handle of the previously active window. Which you could then check to see if it was one of your own windows. This isn't however available in the event, you have to trap the message yourself. Like this:

    protected override void WndProc(ref Message m) {
        base.WndProc(ref m);
        // Trap WM_ACTIVATE when we get active
        if (m.Msg == 6 && m.WParam.ToInt32() == 1) {
            if (Control.FromHandle(m.LParam) == null) {
                Console.WriteLine("activated from another process");
            }
        }
    }