代码之家  ›  专栏  ›  技术社区  ›  Stefan Steiger Marco van de Voort

如何隐藏控制台窗口?

  •  16
  • Stefan Steiger Marco van de Voort  · 技术社区  · 15 年前

    现在我可以像这样在开始后隐藏窗口:

    static void Main(string[] args)
    {
        var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
        Console.WriteLine(currentProcess.MainWindowTitle);
    
        IntPtr hWnd = currentProcess.MainWindowHandle;//FindWindow(null, "Your console windows caption"); //put your console window caption here
        if (hWnd != IntPtr.Zero)
        {
            //Hide the window
            ShowWindow(hWnd, 0); // 0 = SW_HIDE
        }
    

    问题是这会在一瞬间显示窗口。 有没有一个控制台程序的构造器,我可以在窗口显示之前隐藏它?

    其次:

    我用

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    


    4 回复  |  直到 14 年前
        1
  •  32
  •   Richard    15 年前

    如果您不需要控制台(例如 Console.WriteLine )然后将应用程序生成选项更改为Windows应用程序。

    .exe

        2
  •  9
  •   si618    15 年前

    如果我理解您的问题,只需手动创建控制台进程并隐藏控制台窗口:

    Process process = new Process();
    process.StartInfo.FileName = "Bogus.exe";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.CreateNoWindow = true;
    

    我这样做是为了一个WPF应用程序,它执行一个控制台应用程序(在后台工作程序中)并重定向标准输出,以便您可以在需要时在窗口中显示结果。如果您需要查看更多代码(工作代码、重定向、参数传递等),请告诉我

        3
  •  3
  •   abatishchev Karl Johan    11 年前
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool CloseHandle(IntPtr handle);
    
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool FreeConsole();
    
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    private static extern IntPtr GetStdHandle([MarshalAs(UnmanagedType.I4)]int nStdHandle);
    
    // see the comment below
    private enum StdHandle
    {
        StdIn = -10,
        StdOut = -11,
        StdErr = -12
    };
    
    void HideConsole()
    {
        var ptr = GetStdHandle((int)StdHandle.StdOut);
        if (!CloseHandle(ptr))
            throw new Win32Exception();
    
        ptr = IntPtr.Zero;
    
        if (!FreeConsole())
            throw new Win32Exception();
    }
    

    查看更多与控制台相关的API调用 here

        4
  •  2
  •   Rich Guernsey    12 年前
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("kernel32")]
        public static extern IntPtr GetConsoleWindow();
        [DllImport("Kernel32")]
        private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
    
        static void Main(string[] args)
        {
            IntPtr hConsole = GetConsoleWindow();
            if (IntPtr.Zero != hConsole)
            {
                ShowWindow(hConsole, 0); 
            }
        }
    

    这应该符合你的要求。

    推荐文章