How can i bring a process window to the front?

11,829

I think this may work:

[DllImport("user32")]
private static extern bool SetForegroundWindow(IntPtr hwnd);
public static void Processing(string WorkingDirectory, string FileName, string Arguments, bool StandardOutput, string OutputFileName)
    {
        Process proc = new Process();
        proc.EnableRaisingEvents = true;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = StandardOutput;
        proc.StartInfo.FileName = FileName;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.WorkingDirectory = WorkingDirectory;
        proc.StartInfo.Arguments = Arguments;
        proc.Start();
        //Added code
        System.Threading.Thread.Sleep(500);
        SetForegroundWindow(proc.MainWindowHandle);
        //........................................
        if (StandardOutput == true)
        {
            string output = proc.StandardOutput.ReadToEnd();
            DumpOutput(WorkingDirectory + "\\" + OutputFileName, output);
        }
        proc.WaitForExit();
        proc.Close();
    }

If you want it to be Topmost, use SetWindowPos:

[DllImport("user32")]
private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hwnd2, int x, int y, int cx, int cy, int flags);
//instead of calling SetForegroundWindow
SetWindowPos(proc.MainWindowHandle, new IntPtr(-1), 0,0,0,0, 0x1 | 0x2);
//SWP_NOSIZE = 1, SWP_NOMOVE = 2  -> keep the current pos and size (ignore x,y,cx,cy).
//the second param = -1   -> set window as Topmost.
Share:
11,829
DanielVest
Author by

DanielVest

Updated on June 09, 2022

Comments

  • DanielVest
    DanielVest over 1 year

    I have this button click code in Form1:

    private void DriverVerifier_Click(object sender, EventArgs e)
            {
    
                if (MessageBox.Show("Are you Sure you want to Launch the Driver Verifier. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
    
                }
                else
                {
                    ProcessRun.Processing(Environment.SystemDirectory, "verifier.exe", "", false, "");
                }
    
            }
    

    This will run the driver verifier manager program that come with windows 7 and 8 and other versions.

    The problem is in some windows versions for some users when the driver verifier manager is running its behind the Form and you cant bring it to the front.

    My question is if there is any way maybe to force this process to be in the front ?

    This is the code of my class ProcessRun:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;
    using System.IO;
    
    namespace Diagnostic_Tool_Blue_Screen
    {
        static class ProcessRun
        {
    
            public static void Processing(string WorkingDirectory, string FileName, string Arguments, bool StandardOutput, string OutputFileName)
            {
                Process proc = new Process();
                proc.EnableRaisingEvents = true;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = StandardOutput;
                proc.StartInfo.FileName = FileName;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.WorkingDirectory = WorkingDirectory;
                proc.StartInfo.Arguments = Arguments;
                proc.Start();
                if (StandardOutput == true)
                {
                    string output = proc.StandardOutput.ReadToEnd();
                    DumpOutput(WorkingDirectory + "\\" + OutputFileName, output);
                }
                proc.WaitForExit();
                proc.Close();
            }
    
            private static void DumpOutput(string filename, string output)
            {
                StreamWriter w = new StreamWriter(filename);
                w.Write(output);
                w.Close();
            }
        }
    }
    

    This is an image of how the program look like when its behind the Form in the background:

    enter image description here This is what a user with the problem wrote to describe the problem:

    Found one bug, though, using both Windows 8 and Windows 8.1, have yet not tested with Seven: the Driver Verifier is quite useless addition, as when you click it open (red / brown button at bottom), the tool's main window refuses to move, minimize or close, keeping focus, so the Driver Verifier window can't be seen as it remains under the tool window (see screenshot).

    This is just a minor issue with bigger displays and multiple display systems as the Driver Verifier can be moved to aside to be fully seen, but on smaller single display systems it makes the whole Driver Verifier in this tool useless.