223

我四处搜索有关如何隐藏自己的控制台窗口的信息。令人惊讶的是,我能找到的唯一解决方案是通过标题FindWindow()找到控制台窗口的hacky 解决方案。我对 Windows API 进行了更深入的研究,发现有一种更好更简单的方法,所以我想在这里发布它以供其他人查找。

如何隐藏(和显示)与我自己的 C# 控制台应用程序关联的控制台窗口?

4

9 回答 9

325

只需转到应用程序的属性并将输出类型Console Application更改为Windows Application

于 2011-05-01T13:09:04.680 回答
311

就是这样:

using System.Runtime.InteropServices;

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

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

const int SW_HIDE = 0;
const int SW_SHOW = 5;

var handle = GetConsoleWindow();

// Hide
ShowWindow(handle, SW_HIDE);

// Show
ShowWindow(handle, SW_SHOW);
于 2010-08-26T02:19:48.793 回答
23

您可以反过来并将应用程序输出类型设置为:Windows 应用程序。然后将此代码添加到应用程序的开头。

[DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int AllocConsole();

private const int STD_OUTPUT_HANDLE = -11;
private const int MY_CODE_PAGE = 437;
private static bool showConsole = true; //Or false if you don't want to see the console

static void Main(string[] args)
{
    if (showConsole)
    {
        AllocConsole();
        IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
        Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle(stdHandle, true);
        FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
        System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
        StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
        standardOutput.AutoFlush = true;
        Console.SetOut(standardOutput);
    }

    //Your application code
}

showConsole如果是,此代码将显示控制台true

于 2015-11-04T08:00:28.690 回答
21

如果要隐藏控制台本身,为什么需要控制台应用程序?=)

我建议将项目输出类型设置为Windows 应用程序而不是控制台应用程序。它不会向您显示控制台窗口,而是执行所有操作,就像控制台应用程序一样。

于 2012-08-15T14:42:20.210 回答
12

在这里查看我的帖子:

在 Windows 应用程序中显示控制台

您可以制作一个 Windows 应用程序(有或没有窗口)并根据需要显示控制台。使用此方法,控制台窗口永远不会出现,除非您明确显示它。我将它用于希望在控制台或 gui 模式下运行的双模式应用程序,具体取决于它们的打开方式。

于 2013-02-26T00:23:37.033 回答
8

“只是为了隐藏”你可以:

将输出类型从Console Application更改为Windows Application

而不是Console.Readline/key你可以new ManualResetEvent(false).WaitOne()在最后使用来保持应用程序运行。

于 2018-12-02T18:07:41.590 回答
2

如果您不想依赖于窗口标题,请使用:

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

...

    IntPtr h = Process.GetCurrentProcess().MainWindowHandle;
    ShowWindow(h, 0);
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new FormPrincipale());
于 2013-06-28T07:34:41.960 回答
2

根据Timwi的回答,我创建了一个辅助类来包装所需的代码:

using System;
using System.Runtime.InteropServices;
static class ConsoleExtension {
    const int SW_HIDE = 0;
    const int SW_SHOW = 5;
    readonly static IntPtr handle = GetConsoleWindow();
    [DllImport("kernel32.dll")] static extern IntPtr GetConsoleWindow();
    [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd,int nCmdShow);

    public static void Hide() {
        ShowWindow(handle,SW_HIDE); //hide the console
    }
    public static void Show() {
        ShowWindow(handle,SW_SHOW); //show the console
    }
}
于 2021-11-20T15:23:24.493 回答
0

如果您在集成小批量应用程序时没有问题,则有一个名为Cmdow.exe的程序,它允许您根据控制台标题隐藏控制台窗口。

Console.Title = "MyConsole";
System.Diagnostics.Process HideConsole = new System.Diagnostics.Process();
HideConsole.StartInfo.UseShellExecute = false;
HideConsole.StartInfo.Arguments = "MyConsole /hid";
HideConsole.StartInfo.FileName = "cmdow.exe";
HideConsole.Start();

将exe添加到解决方案中,将构建操作设置为“内容”,将复制到输出目录设置为适合您的内容,cmdow在运行时将隐藏控制台窗口。

要使控制台再次可见,您只需更改参数

HideConsole.StartInfo.Arguments = "MyConsole /Vis";
于 2015-02-11T17:37:38.763 回答