显示/隐藏一个C#控制台应用程序的控制台窗口 [英] Show/Hide the console window of a C# console application

查看:591
本文介绍了显示/隐藏一个C#控制台应用程序的控制台窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Google搜索周围就如何隐藏自己的控制台窗口的信息。不可思议的是,唯一的解决办法我能找到人,涉及哈克解决方案 FindWindow函数()找到控制台窗口中的其标题的。我挖了一个深一点到Windows API,发现有一个更好更简单的方式,所以我想在这里发布为他人找到。

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.

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

推荐答案

下面的方式:

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

这篇关于显示/隐藏一个C#控制台应用程序的控制台窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆