显示/隐藏C#控制台应用程序的控制台窗口

Tim*_*mwi 177 c# console console-application

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

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

Tim*_*mwi 244

这是如何做:

using System.Runtime.InteropServices;
Run Code Online (Sandbox Code Playgroud)
[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;
Run Code Online (Sandbox Code Playgroud)
var handle = GetConsoleWindow();

// Hide
ShowWindow(handle, SW_HIDE);

// Show
ShowWindow(handle, SW_SHOW);
Run Code Online (Sandbox Code Playgroud)

  • 窗口仍然在开始时暂时出现.我想除非应用程序类型被更改,否则没有办法解决这个问题? (12认同)
  • @Vaccano:只能通过手动编辑`csproj`文件,使您的应用程序成为调试模式的控制台应用程序.Visual Studio没有GUI来执行此操作,但如果正确编辑`csproj`文件,它将遵循该设置. (8认同)
  • 这是一个非常好的答案,但我可以补充一点,添加的另一个选项是`const int SW_SHOWMINIMIZED = 2;`然后`ShowWindow(handle,SW_SHOWMINIMIZED);`这样控制台启动时不会隐藏,只是最小化. (4认同)
  • 需要:`using System.Runtime.InteropServices;` (3认同)
  • 如果有办法解决这个问题会很好.这样我可以在调试模式下显示控制台,但是当我处于正常运行模式时,只需运行我的程序并退出(没有窗口). (2认同)

小智 243

只需转到应用程序的属性,然后将输出类型控制台应用程序更改为Windows应用程序.

  • 即使这不符合OP的问题,我也非常感谢你给出这个答案.这正是我需要的:) (9认同)
  • 这不是解决方案,因为这种方式窗口无法显示. (5认同)
  • 这不是海报问的解决方案. (4认同)
  • 虽然很棒,但此解决方案不允许您以编程方式控制何时显示和隐藏控制台。假设您接受一个控制台参数,在设置时您想隐藏您的控制台(即静默模式,verbose=false) (3认同)
  • 当您要在后台更新主可执行文件然后运行它时,这非常有用。谢谢 (2认同)
  • 通过使用 AllocConsole API 函数,可以从*任何* GUI 应用程序显示(和使用)控制台窗口。 (2认同)

Sas*_*sha 24

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

我建议将Project Output类型设置为Windows Application而不是Console应用程序.它不会向您显示控制台窗口,但会执行所有操作,例如控制台应用程序.

  • 因为有时候我真的想要展示它.就像,控制台应用程序试图执行的东西,并不会打扰任何人,只要它成功.如果没有,它会弹出并提供一个CLI. (26认同)

Mai*_*gma 17

您可以执行相反的操作并将Application输出类型设置为:Windows Application.然后将此代码添加到应用程序的开头.

[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
}
Run Code Online (Sandbox Code Playgroud)

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

  • 它确实显示带有闪烁光标的控制台,但 Console.WriteLine("text") 和 standardOutput.WriteLine("text") 在我的情况下都没有显示任何内容。是不是少了什么? (5认同)

小智 9

在这里看我的帖子:

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

您可以创建Windows应用程序(带或不带窗口)并根据需要显示控制台.使用此方法,除非您明确显示,否则控制台窗口永远不会出现.我将它用于双模式应用程序,我想在控制台或gui模式下运行,具体取决于它们的打开方式.


N T*_*N T 7

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

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

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


小智 7

根据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
    }
}
Run Code Online (Sandbox Code Playgroud)