[ACCEPTED]-Make a running process the active Window-ms-access

Accepted answer
Score: 14

Eric's answer didn't work for me. I found 5 a better solution here on SO with SetForegroundWindow. First 4 I wondered, why it one time worked, next 3 time it did'n.Then I excluded the current 2 process from the list. So, here's my final 1 version:

static void BringWindowToFront()
{
    var currentProcess = Process.GetCurrentProcess();
    var processes = Process.GetProcessesByName(currentProcess.ProcessName);
    var process = processes.FirstOrDefault(p => p.Id!=currentProcess.Id);
    if (process == null) return;

    SetForegroundWindow(process.MainWindowHandle);
}

[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
Score: 3

Try this:

[DllImport("user32.dll", CharSet=CharSet.Auto,ExactSpelling=true)]
public static extern IntPtr SetFocus(HandleRef hWnd);


[TestMethod]
public void PlayAround()
{
    Process[] processList = Process.GetProcesses();

    foreach (Process theProcess in processList)
    {
        string processName = theProcess.ProcessName;
        string mainWindowTitle = theProcess.MainWindowTitle;
        SetFocus(new HandleRef(null, theProcess.MainWindowHandle));
    }

}

0

More Related questions