首页 文章

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

提问于
浏览
152

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

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

8 回答

  • 9

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

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

  • 1

    这是如何做:

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

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

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

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

  • 0

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

  • 14

    您可以执行相反的操作并将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
    }
    

    如果 showConsoletrue ,此代码将显示控制台

  • 21

    在这里看我的帖子:

    Show Console in Windows Application

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

  • 0

    如果你不想依赖窗口 Headers 使用这个:

    [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());
    
  • 216

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

    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添加到解决方案中,将构建操作设置为“Content”,将Copy to Output Directory设置为适合您的内容,cmdow将在运行时隐藏控制台窗口 .

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

    HideConsole.StartInfo.Arguments = "MyConsole /Vis";
    

相关问题