How to determine whether my application is active (has focus)

前端 未结 5 1281
天命终不由人
天命终不由人 2021-02-05 18:53

Is there a way to tell whether my application is active i.e. any of its windows has .IsActive=true?

I\'m writing messenger app and want it to flash in taskbar when it is

5条回答
  •  春和景丽
    2021-02-05 19:23

    One way (may be there would be more better ways) can be to find the Active window through Windows API then find the process name of active window.

    if(yourProcessName == ActiveWindowProcessName)
    {
        //your window is in focus
    }
    

    Another way could be to keep a reference of all the windows and when you want to find out whether your app is active or not just iterate through all the windows and check IsActive value

    Another way could be to use OwnedWindows property of MainWindow. Whenever you are creating a new window assign main window it's owner. Then you can iterate all the OwnedWindows of MainWindow and check whether any is active or not.(never tried this approach)

提交回复
热议问题