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 およびその他のバージョンに付属する Driver Verifier Manager プログラムが実行されます。

この問題は、ドライバー検証マネージャーがフォームの背後で実行されていて、それを前面に出すことができない場合に、一部のユーザーの一部の 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();
        }
    }
}

これは、バックグラウンドでフォームの背後にあるときにプログラムがどのように見えるかのイメージです。

ここに画像の説明を入力 これは、問題を抱えているユーザーが問題を説明するために書いたものです。

ただし、Windows 8 と Windows 8.1 の両方を使用して、Seven でまだテストしていないバグが 1 つ見つかりました。Driver Verifier は、クリックして開くと (下部の赤/茶色のボタン)、ツールのメイン ウィンドウが拒否するため、まったく役に立ちません。フォーカスを維持したまま、移動、最小化、または閉じることで、Driver Verifier ウィンドウがツール ウィンドウの下にあるため表示されなくなります (スクリーンショットを参照)。

これは、Driver Verifier を脇に移動して完全に表示できるため、大きなディスプレイや複数のディスプレイ システムでは小さな問題ですが、小さな単一のディスプレイ システムでは、このツールの Driver Verifier 全体が役に立たなくなります。

4

1 に答える 1

9

私はこれがうまくいくと思います:

[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を使用します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.
于 2013-08-06T03:52:42.670 に答える