1

Is there a way to activate or bring a window of an external application to the front? The closest I could get was

var application = Application.Attach("SearchApp");
var searchWindow = application.GetWindows()[0];

searchWindow.Focus(DisplayState.Maximized);

but all that does is maximize it in the background if it's not currently active.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
Amorphous
  • 675
  • 1
  • 8
  • 26
  • Did you google? One of the first results for "bring wpf window to front": [How do bring a WPF Window to front?](http://stackoverflow.com/questions/3399495/how-do-bring-a-wpf-window-to-front) Here's [another duplicate](http://stackoverflow.com/questions/7211523/wpf-bring-window-to-front) and [yet another](http://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf) – tnw Jul 17 '14 at 14:46
  • I did. Problem is I don't need to bring my window to the front, I need to do it to an external application. – Amorphous Jul 17 '14 at 14:49
  • 1
    *Oh*. That's a very different question. I've edited yours to reflect that. Don't forget to include *all* relevant information like that in your post. – tnw Jul 17 '14 at 14:51
  • Ah thanks. Sorry for the ambiguity. – Amorphous Jul 17 '14 at 14:51
  • 1
    You would need the user32.dll. See here: http://www.pinvoke.net/default.aspx/user32/BringWindowToTop.html. And here to get all Window names, and then you can find yours: http://social.msdn.microsoft.com/Forums/vstudio/en-US/410feea9-37e9-44af-8b84-cee6df4aee07/how-do-i-get-the-window-class-and-window-name-of-an-application-without-spy?forum=csharpgeneral – Sonhja Jul 17 '14 at 14:55

1 Answers1

2

Got it working.

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool SetForegroundWindow(IntPtr windowHandle);


    public bool SearchTest(string file) {
        try
        {
            // White stuff, not relevant to problem
            //var application = Application.Attach("SearchApp"); 
            //var searchWindow = application.GetWindows()[0];

            Process p = Process.GetProcessesByName("SearchApp")[0];
            SetForegroundWindow(p.MainWindowHandle);
Amorphous
  • 675
  • 1
  • 8
  • 26