开发者

How to detect if application is activated by clicking in the taskbar

开发者 https://www.devze.com 2023-01-18 19:12 出处:网络
I have an application with several windows where only the main window appears in the taskbar. With a click on th开发者_如何学运维e icon in the taskbar I want all my application windows to show up on

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

With a click on th开发者_如何学运维e icon in the taskbar I want all my application windows to show up on top of any other open windows.

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 ?


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.

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");
            }
        }
    }


Form_Activated works for me...

0

精彩评论

暂无评论...
验证码 换一张
取 消