[ACCEPTED]-How do I focus a foreign window?-focus

Accepted answer
Score: 10

I had the same problem and SwitchToThisWindow() worked the best 5 for me. The only limitation is that you 4 must have XP sp1 installed. I played with 3 SetForegroundWindow, ShowWindow, and they 2 both had problems pulling the window into 1 view.

Score: 7

C# equivalent of Tom Juergens's answer. Works 1 like a charm for me.

private const  int SW_SHOWNORMAL = 1;

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);

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

public void SetForeground()
{
    Process[] processes = Process.GetProcessesByName("process name");

    foreach (Process p in processes) {
        ShowWindow(p.MainWindowHandle, SW_SHOWNORMAL);
        SetForegroundWindow(p.MainWindowHandle);
    }
}
Score: 5

Same as OP, I found that SetForegroundWindow alone wasn't enough 3 when the window was minimized. Since I didn't 2 want to use SwitchToThisWindow, I chose ShowWindow followed by SetForegroundWindow.

Works 1 well for me!

private const SW_SHOWNORMAL = 1

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As integer) As Boolean
End Function

<DllImport("user32.dll", SetLastError:=True)> _
Private Function SetForegroundWindow(ByVal hwnd As IntPtr) As Boolean
End Function

Sub SetForeground()
    Dim processes As Process() = Process.GetProcessesByName("myprocess")

    For Each p as Process in processes
        ShowWindow(p.MainWindowHandle, SW_SHOWNORMAL)
        SetForegroundWindow(p.MainWindowHandle)
    Next
End Sub
Score: 2

I believe you will want to use SetForegroundWindow

MSDN Example

0

Score: 2

Complete Side Note...

You can use

Process.GetProcessesByName(me.ProcessName) 

instead 3 of looping over all the processes running 2 on the system...

UPDATE

PInvoke Rules for this sort of 1 thing...

Score: 0

Can you grab MainWindowHandle property of 4 the Process object and send it a WM_USER 3 message that you can interpret as "some 2 other instance wants to bring me to the 1 front".

Score: 0

It is a very frequent behavior in desktop 9 applications, I regularly have to do this 8 when I create a new WPF application. So 7 I have created a SingletonApp class which 6 inherits from Application :

public class SingletonApp : Application
{
    private static readonly System.Threading.Mutex mutex;
    private static readonly string processName;

    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, int flags);

    [DllImport("user32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hwnd);

    static SingletonApp()
    {
        processName = Process.GetCurrentProcess().ProcessName;
        mutex = new System.Threading.Mutex(false, $"Local\\{processName}");
    }

    /// <summary>
    /// A base class for application needing to prevent multiple instances
    /// </summary>
    public SingletonApp()
    {
        if (!mutex.WaitOne(0, false))
        {
            // Give focus to existing instance before shutdown
            BringToFront(processName);

            Current.Shutdown();
        }
    }

    public void BringToFront(string processName)
    {
        Process process = Process.GetProcessesByName(processName).FirstOrDefault();

        if (process != null)
        {
            // In case of window is minimized
            ShowWindow(process.MainWindowHandle, 1);     // 1 = Normal

            SetForegroundWindow(process.MainWindowHandle);
        }
    }
}

To use it, you 5 just have to inherit from SingletonApp instead 4 of Application in your App.xaml.cs :

public partial class App : SingletonApp

Don't 3 forget to update App.xaml too :

<utils:SingletonApp x:Class="MyApp.App"
             [...]
             xmlns:utils="clr-namespace:MyApp.Utils"
             Startup="App_OnStartup">

With this 2 it becomes very easy to implement this behavior 1 in every new desktop client.

More Related questions