Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

Give focus to WPF application if other instances are running

I want to create a Spotify-like behavior for my WPF application. When the application is running, but minimized in the taskbar and the user tries to start a new instace, it should restore it from the taskbar, and give focus to the already running instance. The problem is stumble upon is to open to application from minimized state in the taskbar to be in focus. It works if the application is open, but other windows are placed over it.

 var processHndl = FindWindow(null, "MyApplicationName!");
            if (processHndl != IntPtr.Zero)
            {
                var isIconic = IsIconic(processHndl);
                MessageBox.Show("Iconic: " + isIconic);
                if (IsIconic(processHndl))
                {
                    ShowWindow(processHndl, 9);
                }
                SetForegroundWindow(processHndl);
            }

Any ideas?

Upvotes: 0

Views: 193

Answers (1)

weismat
weismat

Reputation: 7411

Please try

SwitchToThisWindow

Windows Function Reference.

Upvotes: 3

Related Questions