如何将进程窗口置于前台?

6

我有一个在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, "");
            }

        }

这将运行与Windows 7、8和其他版本一起提供的驱动程序验证器管理器程序。

问题在于,在某些Windows版本中,对于某些用户,当驱动程序验证器管理器正在运行时,它位于窗体后面,您无法将其置于前面。

我的问题是是否有任何方法可以强制此进程处于前台?

这是我类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();
        }
    }
}

这是程序在背景表格后的外观图像:

enter image description here 用户描述问题如下:

发现一个bug,使用Windows 8和Windows 8.1均有此问题,但尚未在Seven上进行测试:驱动程序验证器是一种相当无用的附加功能,因为当您单击打开它时(底部的红色/棕色按钮),该工具的主窗口会拒绝移动、最小化或关闭,保持焦点,因此驱动程序验证器窗口无法被看到,因为它仍然在工具窗口下面(请参见屏幕截图)。

这只是大型显示器和多显示器系统中的一个小问题,因为驱动程序验证器可以被移动到侧面以完全查看,但在较小的单显示器系统中,这个工具中的整个驱动程序验证器就变得毫无用处。

1个回答

10
我认为这可能有效:
[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();
    }

如果您想使其置于顶层,请使用 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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接