Show/Hide the console window of a C# console application

I googled around for information on how to hide one's own console window. Amazingly, the only solutions I could find were hacky solutions that involved FindWindow() to find the console window by its title. I dug a bit deeper into the Windows API and found that there is a much better and easier way, so I wanted to post it here for others to find.

How do you hide (and show) the console window associated with my own C# console application?


就是这样:

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);

只需转到应用程序的“属性”并将“输出”类型从“控制台应用程序”更改为“ Windows应用程序


Why do you need a console application if you want to hide console itself? =)

I recommend setting Project Output type to Windows Application instead of Console application. It will not show you console window, but execute all actions, like Console application do.

链接地址: http://www.djcxy.com/p/50572.html

上一篇: 如何阻止C#控制台应用程序自动关闭?

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