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

2024-06-28

我在 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 This is what a user with the problem wrote to describe the problem:

不过,在使用 Windows 8 和 Windows 8.1 时发现了一个错误,尚未使用 7 进行测试:驱动程序验证程序是相当无用的补充,因为当您单击它打开(底部的红色/棕色按钮)时,该工具的主窗口拒绝移动、最小化或关闭,保持焦点,因此无法看到“驱动程序验证程序”窗口,因为它仍然位于工具窗口下方(请参见屏幕截图)。

对于较大的显示器和多个显示系统,这只是一个小问题,因为驱动程序验证程序可以移到一边以便完全看到,但在较小的单显示系统上,它使该工具中的整个驱动程序验证程序毫无用处。


我认为这可能有效:

[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();
    }

如果你想要它是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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将进程窗口置于前面? 的相关文章

随机推荐