using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices; public static partial class ConsoleManager
{
private static readonly ConsoleCtrlDelegate ConsoleCtrlDelegateHandlerRoutine = HandlerRoutine; #region Code Page Identifiers private static readonly Dictionary<int, string[]> DicCodePageIdentifiers = new Dictionary<int, string[]>
{
{, new[] {"IBM037", "IBM EBCDIC US-Canada"}},
{, new[] {"IBM437", "OEM United States"}},
{, new[] {"IBM500", "IBM EBCDIC International"}},
{, new[] {"ASMO-708", "Arabic (ASMO 708)"}},
{, new[] {"", "Arabic (ASMO-449+, BCON V4)"}},
{, new[] {"", "Arabic - Transparent Arabic"}},
{, new[] {"DOS-720", "Arabic (Transparent ASMO); Arabic (DOS)"}},
{, new[] {"ibm737", "OEM Greek (formerly 437G); Greek (DOS)"}},
{, new[] {"ibm775", "OEM Baltic; Baltic (DOS)"}},
{, new[] {"ibm850", "OEM Multilingual Latin 1; Western European (DOS)"}},
{, new[] {"ibm852", "OEM Latin 2; Central European (DOS)"}},
{, new[] {"IBM855", "OEM Cyrillic (primarily Russian)"}},
{, new[] {"ibm857", "OEM Turkish; Turkish (DOS)"}},
{, new[] {"IBM00858", "OEM Multilingual Latin 1 + Euro symbol"}},
{, new[] {"IBM860", "OEM Portuguese; Portuguese (DOS)"}},
{, new[] {"ibm861", "OEM Icelandic; Icelandic (DOS)"}},
{, new[] {"DOS-862", "OEM Hebrew; Hebrew (DOS)"}},
{, new[] {"IBM863", "OEM French Canadian; French Canadian (DOS)"}},
{, new[] {"IBM864", "OEM Arabic; Arabic (864)"}},
{, new[] {"IBM865", "OEM Nordic; Nordic (DOS)"}},
{, new[] {"cp866", "OEM Russian; Cyrillic (DOS)"}},
{, new[] {"ibm869", "OEM Modern Greek; Greek, Modern (DOS)"}},
{, new[] {"IBM870", "IBM EBCDIC Multilingual/ROECE (Latin 2); IBM EBCDIC Multilingual Latin 2"}},
{, new[] {"windows-874", "ANSI/OEM Thai (ISO 8859-11); Thai (Windows)"}},
{, new[] {"cp875", "IBM EBCDIC Greek Modern"}},
{, new[] {"shift_jis", "ANSI/OEM Japanese; Japanese (Shift-JIS)"}},
{, new[] {"gb2312", "ANSI/OEM Simplified Chinese (PRC, Singapore); Chinese Simplified (GB2312)"}},
{, new[] {"ks_c_5601-1987", "ANSI/OEM Korean (Unified Hangul Code)"}},
{, new[] {"big5", "ANSI/OEM Traditional Chinese (*; * SAR, PRC); Chinese Traditional (Big5)"}},
{, new[] {"IBM1026", "IBM EBCDIC Turkish (Latin 5)"}},
{, new[] {"IBM01047", "IBM EBCDIC Latin 1/Open System"}},
{, new[] {"IBM01140", "IBM EBCDIC US-Canada (037 + Euro symbol); IBM EBCDIC (US-Canada-Euro)"}},
{, new[] {"IBM01141", "IBM EBCDIC Germany (20273 + Euro symbol); IBM EBCDIC (Germany-Euro)"}},
{, new[] {"IBM01142", "IBM EBCDIC Denmark-Norway (20277 + Euro symbol); IBM EBCDIC (Denmark-Norway-Euro)"}},
{, new[] {"IBM01143", "IBM EBCDIC Finland-Sweden (20278 + Euro symbol); IBM EBCDIC (Finland-Sweden-Euro)"}},
{, new[] {"IBM01144", "IBM EBCDIC Italy (20280 + Euro symbol); IBM EBCDIC (Italy-Euro)"}},
{, new[] {"IBM01145", "IBM EBCDIC Latin America-Spain (20284 + Euro symbol); IBM EBCDIC (Spain-Euro)"}},
{, new[] {"IBM01146", "IBM EBCDIC United Kingdom (20285 + Euro symbol); IBM EBCDIC (UK-Euro)"}},
{, new[] {"IBM01147", "IBM EBCDIC France (20297 + Euro symbol); IBM EBCDIC (France-Euro)"}},
{, new[] {"IBM01148", "IBM EBCDIC International (500 + Euro symbol); IBM EBCDIC (International-Euro)"}},
{, new[] {"IBM01149", "IBM EBCDIC Icelandic (20871 + Euro symbol); IBM EBCDIC (Icelandic-Euro)"}},
{, new[] {"utf-16", "Unicode UTF-16, little endian byte order (BMP of ISO 10646); available only to managed applications"}},
{, new[] {"unicodeFFFE", "Unicode UTF-16, big endian byte order; available only to managed applications"}},
{, new[] {"windows-1250", "ANSI Central European; Central European (Windows)"}},
{, new[] {"windows-1251", "ANSI Cyrillic; Cyrillic (Windows)"}},
{, new[] {"windows-1252", "ANSI Latin 1; Western European (Windows)"}},
{, new[] {"windows-1253", "ANSI Greek; Greek (Windows)"}},
{, new[] {"windows-1254", "ANSI Turkish; Turkish (Windows)"}},
{, new[] {"windows-1255", "ANSI Hebrew; Hebrew (Windows)"}},
{, new[] {"windows-1256", "ANSI Arabic; Arabic (Windows)"}},
{, new[] {"windows-1257", "ANSI Baltic; Baltic (Windows)"}},
{, new[] {"windows-1258", "ANSI/OEM Vietnamese; Vietnamese (Windows)"}},
{, new[] {"Johab", "Korean (Johab)"}},
{, new[] {"macintosh", "MAC Roman; Western European (Mac)"}},
{, new[] {"x-mac-japanese", "Japanese (Mac)"}},
{, new[] {"x-mac-chinesetrad", "MAC Traditional Chinese (Big5); Chinese Traditional (Mac)"}},
{, new[] {"x-mac-korean", "Korean (Mac)"}},
{, new[] {"x-mac-arabic", "Arabic (Mac)"}},
{, new[] {"x-mac-hebrew", "Hebrew (Mac)"}},
{, new[] {"x-mac-greek", "Greek (Mac)"}},
{, new[] {"x-mac-cyrillic", "Cyrillic (Mac)"}},
{, new[] {"x-mac-chinesesimp", "MAC Simplified Chinese (GB 2312); Chinese Simplified (Mac)"}},
{, new[] {"x-mac-romanian", "Romanian (Mac)"}},
{, new[] {"x-mac-ukrainian", "Ukrainian (Mac)"}},
{, new[] {"x-mac-thai", "Thai (Mac)"}},
{, new[] {"x-mac-ce", "MAC Latin 2; Central European (Mac)"}},
{, new[] {"x-mac-icelandic", "Icelandic (Mac)"}},
{, new[] {"x-mac-turkish", "Turkish (Mac)"}},
{, new[] {"x-mac-croatian", "Croatian (Mac)"}},
{, new[] {"utf-32", "Unicode UTF-32, little endian byte order; available only to managed applications"}},
{, new[] {"utf-32BE", "Unicode UTF-32, big endian byte order; available only to managed applications"}},
{, new[] {"x-Chinese_CNS", "CNS *; Chinese Traditional (CNS)"}},
{, new[] {"x-cp20001", "TCA *"}},
{, new[] {"x_Chinese-Eten", "Eten *; Chinese Traditional (Eten)"}},
{, new[] {"x-cp20003", "IBM5550 *"}},
{, new[] {"x-cp20004", "TeleText *"}},
{, new[] {"x-cp20005", "Wang *"}},
{, new[] {"x-IA5", "IA5 (IRV International Alphabet No. 5, 7-bit); Western European (IA5)"}},
{, new[] {"x-IA5-German", "IA5 German (7-bit)"}},
{, new[] {"x-IA5-Swedish", "IA5 Swedish (7-bit)"}},
{, new[] {"x-IA5-Norwegian", "IA5 Norwegian (7-bit)"}},
{, new[] {"us-ascii", "US-ASCII (7-bit)"}},
{, new[] {"x-cp20261", "T.61"}},
{, new[] {"x-cp20269", "ISO 6937 Non-Spacing Accent"}},
{, new[] {"IBM273", "IBM EBCDIC Germany"}},
{, new[] {"IBM277", "IBM EBCDIC Denmark-Norway"}},
{, new[] {"IBM278", "IBM EBCDIC Finland-Sweden"}},
{, new[] {"IBM280", "IBM EBCDIC Italy"}},
{, new[] {"IBM284", "IBM EBCDIC Latin America-Spain"}},
{, new[] {"IBM285", "IBM EBCDIC United Kingdom"}},
{, new[] {"IBM290", "IBM EBCDIC Japanese Katakana Extended"}},
{, new[] {"IBM297", "IBM EBCDIC France"}},
{, new[] {"IBM420", "IBM EBCDIC Arabic"}},
{, new[] {"IBM423", "IBM EBCDIC Greek"}},
{, new[] {"IBM424", "IBM EBCDIC Hebrew"}},
{, new[] {"x-EBCDIC-KoreanExtended", "IBM EBCDIC Korean Extended"}},
{, new[] {"IBM-Thai", "IBM EBCDIC Thai"}},
{, new[] {"koi8-r", "Russian (KOI8-R); Cyrillic (KOI8-R)"}},
{, new[] {"IBM871", "IBM EBCDIC Icelandic"}},
{, new[] {"IBM880", "IBM EBCDIC Cyrillic Russian"}},
{, new[] {"IBM905", "IBM EBCDIC Turkish"}},
{, new[] {"IBM00924", "IBM EBCDIC Latin 1/Open System (1047 + Euro symbol)"}},
{, new[] {"EUC-JP", "Japanese (JIS 0208-1990 and 0212-1990)"}},
{, new[] {"x-cp20936", "Simplified Chinese (GB2312); Chinese Simplified (GB2312-80)"}},
{, new[] {"x-cp20949", "Korean Wansung"}},
{, new[] {"cp1025", "IBM EBCDIC Cyrillic Serbian-Bulgarian"}},
{, new[] {"", "(deprecated)"}},
{, new[] {"koi8-u", "Ukrainian (KOI8-U); Cyrillic (KOI8-U)"}},
{, new[] {"iso-8859-1", "ISO 8859-1 Latin 1; Western European (ISO)"}},
{, new[] {"iso-8859-2", "ISO 8859-2 Central European; Central European (ISO)"}},
{, new[] {"iso-8859-3", "ISO 8859-3 Latin 3"}},
{, new[] {"iso-8859-4", "ISO 8859-4 Baltic"}},
{, new[] {"iso-8859-5", "ISO 8859-5 Cyrillic"}},
{, new[] {"iso-8859-6", "ISO 8859-6 Arabic"}},
{, new[] {"iso-8859-7", "ISO 8859-7 Greek"}},
{, new[] {"iso-8859-8", "ISO 8859-8 Hebrew; Hebrew (ISO-Visual)"}},
{, new[] {"iso-8859-9", "ISO 8859-9 Turkish"}},
{, new[] {"iso-8859-13", "ISO 8859-13 Estonian"}},
{, new[] {"iso-8859-15", "ISO 8859-15 Latin 9"}},
{, new[] {"x-Europa", "Europa 3"}},
{, new[] {"iso-8859-8-i", "ISO 8859-8 Hebrew; Hebrew (ISO-Logical)"}},
{, new[] {"iso-2022-jp", "ISO 2022 Japanese with no halfwidth Katakana; Japanese (JIS)"}},
{, new[] {"csISO2022JP", "ISO 2022 Japanese with halfwidth Katakana; Japanese (JIS-Allow 1 byte Kana)"}},
{, new[] {"iso-2022-jp", "ISO 2022 Japanese JIS X 0201-1989; Japanese (JIS-Allow 1 byte Kana - SO/SI)"}},
{, new[] {"iso-2022-kr", "ISO 2022 Korean"}},
{, new[] {"x-cp50227", "ISO 2022 Simplified Chinese; Chinese Simplified (ISO 2022)"}},
{, new[] {"", "ISO 2022 Traditional Chinese"}},
{, new[] {"", "EBCDIC Japanese (Katakana) Extended"}},
{, new[] {"", "EBCDIC US-Canada and Japanese"}},
{, new[] {"", "EBCDIC Korean Extended and Korean"}},
{, new[] {"", "EBCDIC Simplified Chinese Extended and Simplified Chinese"}},
{, new[] {"", "EBCDIC Simplified Chinese"}},
{, new[] {"", "EBCDIC US-Canada and Traditional Chinese"}},
{, new[] {"", "EBCDIC Japanese (Latin) Extended and Japanese"}},
{, new[] {"euc-jp", "EUC Japanese"}},
{, new[] {"EUC-CN", "EUC Simplified Chinese; Chinese Simplified (EUC)"}},
{, new[] {"euc-kr", "EUC Korean"}},
{, new[] {"", "EUC Traditional Chinese"}},
{, new[] {"hz-gb-2312", "HZ-GB2312 Simplified Chinese; Chinese Simplified (HZ)"}},
{, new[] {"GB18030", "Windows XP and later: GB18030 Simplified Chinese (4 byte); Chinese Simplified (GB18030)"}},
{, new[] {"x-iscii-de", "ISCII Devanagari"}},
{, new[] {"x-iscii-be", "ISCII Bangla"}},
{, new[] {"x-iscii-ta", "ISCII Tamil"}},
{, new[] {"x-iscii-te", "ISCII Telugu"}},
{, new[] {"x-iscii-as", "ISCII Assamese"}},
{, new[] {"x-iscii-or", "ISCII Odia"}},
{, new[] {"x-iscii-ka", "ISCII Kannada"}},
{, new[] {"x-iscii-ma", "ISCII Malayalam"}},
{, new[] {"x-iscii-gu", "ISCII Gujarati"}},
{, new[] {"x-iscii-pa", "ISCII Punjabi"}},
{, new[] {"utf-7", "Unicode (UTF-7)"}},
{, new[] {"utf-8", "Unicode (UTF-8)"}}
}; #endregion private static bool HasConsole => GetConsoleWindow() != IntPtr.Zero; private delegate bool ConsoleCtrlDelegate(int dwCtrlType); //定义处理程序委托 #region Method /// <summary>
/// 禁用关闭按钮
/// </summary>
private static void CloseButtonRemove()
{
var windowHandler = FindWindow(null, Process.GetCurrentProcess().MainModule.FileName); //与控制台标题名一样的路径,根据控制台标题找控制台
var closeMenu = GetSystemMenu((IntPtr) windowHandler, IntPtr.Zero); //找关闭按钮
var scClose = 0xF060;
RemoveMenu(closeMenu, scClose, 0x0); //关闭按钮禁用
} private static void InvalidateOutAndError()
{
var type = typeof(Console);
var _out = type.GetField("_out", BindingFlags.Static | BindingFlags.NonPublic);
var error = type.GetField("_error", BindingFlags.Static | BindingFlags.NonPublic);
var initializeStdOutError = type.GetMethod("InitializeStdOutError", BindingFlags.Static | BindingFlags.NonPublic);
Debug.Assert(_out != null);
Debug.Assert(error != null);
Debug.Assert(initializeStdOutError != null);
_out.SetValue(null, null);
error.SetValue(null, null);
initializeStdOutError.Invoke(null, new object[] {true});
} private static void SetOutAndErrorNull()
{
Console.SetOut(TextWriter.Null);
Console.SetError(TextWriter.Null);
} //当关闭Console时,系统会发送下面的消息
private const int CtrlCEvent = ; //无论是从键盘输入或由GenerateConsoleCtrlEvent功能信号产生的一个CTRL + C接收信号
private const int CtrlBreakEvent = ; //无论是从键盘输入或由GenerateConsoleCtrlEvent信号产生的一个CTRL + BREAK信号接收。
private const int CtrlCloseEvent = ; //信号系统,当用户关闭控制台(通过单击控制台窗口菜单上的关闭按钮,或通过从任务管理器结束任务)
private const int CtrlLogoffEvent = ; //用户注销时系统发送到所有控制台进程的信号。此信号不指示哪个用户正在注销,因此不能进行任何假设。请注意,此信号仅由服务接收。交互式应用程序在注销时终止,因此当系统发送此信号时,它们不存在。
private const int CtrlShutdownEvent = ; //系统关闭时系统发送的信号。在系统发送此信号时,交互式应用程序不存在,因此在这种情况下它只能被服务接收。服务还有自己的关闭事件的通知机制。这个信号还可以通过使用应用程序生成的GenerateConsoleCtrlEvent。 /// <summary>
/// 处理程序例程,在这里编写对指定事件的处理程序代码
/// 注意:在VS中调试执行时,在这里设置断点,但不会中断;会提示:无可用源;
/// </summary>
/// <param name="ctrlType"></param>
/// <returns></returns>
private static bool HandlerRoutine(int ctrlType)
{
switch (ctrlType)
{
case CtrlCEvent:
OnCtrlCPressed(null, null);
Console.WriteLine("Ctrl+C按下,阻止");
return true; //这里返回true,表示阻止响应系统对该程序的操作成功
case CtrlBreakEvent:
Console.WriteLine("Ctrl+BREAK按下,阻止");
return true;
case CtrlCloseEvent:
Console.WriteLine("CLOSE");
break;
case CtrlLogoffEvent:
Console.WriteLine("LOGOFF");
break;
case CtrlShutdownEvent:
Console.WriteLine("SHUTDOWN");
break;
}
return true; //true 表示阻止响应系统对该程序的操作 //false 忽略处理,让系统进行默认操作
} #endregion #region 导入API函数 /// <summary>
/// 添加或删除从调用进程处理函数列表中的应用definedhandlerroutinefunction。如果没有指定的事件处理函数,函数集的可继承的属性,确定是否调用过程忽略了Ctrl + C信号。
/// </summary>
/// <param name="handlerRoutine">指向要添加或删除的程序定义HandlerRoutine函数的指针。 此参数可以为NULL。</param>
/// <param name="add">
/// 如果这个参数为TRUE,处理程序被添加; 如果是FALSE,则处理程序被删除。如果HandlerRoutine参数为NULL,一个TRUE值会导致调用进程忽略CTRL +
/// C输入,以及一个FALSE值恢复CTRL + C输入的正常处理。忽略或处理CTRL + C的此属性由子进程继承。
/// </param>
/// <returns>如果函数成功,返回值为非零。如果函数失败,返回值为零。</returns>
[DllImport("kernel32.dll")]
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate handlerRoutine, bool add); /// <summary>
/// 为当前进程分配一个新控制台
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll")]
private static extern bool AllocConsole(); /// <summary>
/// 使调用进程从其控制台分离
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll")]
private static extern bool FreeConsole(); /// <summary>
/// 检索与调用进程相关联的控制台窗口句柄
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow(); /// <summary>
/// 检取与调用进程有关的控制台所用的输出代码页的等价内容,以便将输出函数所写入的内容转换成显示图象
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll")]
private static extern int GetConsoleOutputCP(); /// <summary>
/// 获得一个顶层窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数不查找子窗口。
/// </summary>
/// <param name="lpClassName">
/// 指向一个指定了类名的空结束字符串,或一个标识类名字符串的成员的指针。如果该参数为一个成员,则它必须为前次调用theGlobafAddAtom函数产生的全局成员。该成员为16位,必须位于IpClassName的低
/// 16位,高位必须为 0。
/// </param>
/// <param name="lpWindowName">指向一个指定了窗口名(窗口标题)的空结束字符串。如果该参数为空,则为所有窗口全匹配。</param>
/// <returns>返回值:如果函数成功,返回值为具有指定类名和窗口名的窗口句柄;如果函数失败,返回值为NULL。</returns>
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName); /// <summary>
/// 该函数允许应用程序为复制或修改而访问窗口菜单(系统菜单或控制菜单)。
/// 任何没有用GetSystemMenu函数来生成自己的窗口菜单拷贝的窗口将接受标准窗口菜单。
/// 窗口菜单最初包含的菜单项有多种标识符值,如SC_CLOSE,SC_MOVE和SC_SIZE。
/// 窗口菜单上的菜单项发送WM_SYSCOMMAND消息。
/// </summary>
/// <param name="hWnd">拥有窗口菜单拷贝的窗口的句柄。</param>
/// <param name="bRevert">指定将执行的操作。如果此参数为FALSE,GetSystemMenu返回当前使用窗口菜单的拷贝的句柄。该拷贝初始时与窗口菜单相同,但可以被修改。如果此参数为TRUE,GetSystemMenu重置窗口菜单到缺省状态。如果存在先前的窗口菜单,将被销毁。</param>
/// <returns>如果参数bRevert为FALSE,返回值是窗口菜单的拷贝的句柄:如果参数bRevert为TRUE,返回值是NULL。</returns>
[DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert); /// <summary>
/// 删除指定的菜单项或弹出式菜单
/// </summary>
/// <param name="hMenu"></param>
/// <param name="nPos"></param>
/// <param name="flags"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "RemoveMenu")]
private static extern int RemoveMenu(IntPtr hMenu, int nPos, int flags); #endregion #region Event #region CtrlCPressed public delegate void CtrlCPressedHandler(object sender, ConsoleCancelEventArgs e); public static event CtrlCPressedHandler CtrlCPressed; private static void OnCtrlCPressed(object sender, ConsoleCancelEventArgs e)
{
CtrlCPressed?.Invoke(sender, e);
} #endregion CtrlCPressed #endregion #region 对外开放的方法 /// Creates a new console instance if the process is not attached to a console already.
public static void Show()
{
//#if DEBUG
try
{
if (!HasConsole)
{
AllocConsole();
InvalidateOutAndError();
//Console.CancelKeyPress += OnCtrlCPressed;
if (SetConsoleCtrlHandler(ConsoleCtrlDelegateHandlerRoutine, true))
Console.Write("成功阻止窗口关闭-");
CloseButtonRemove();
var getConsoleOutputCpInfo = GetConsoleOutputCP();
if (DicCodePageIdentifiers.ContainsKey(getConsoleOutputCpInfo))
Console.WriteLine($"当前窗口信息:{DicCodePageIdentifiers[getConsoleOutputCpInfo][0]},[{DicCodePageIdentifiers[getConsoleOutputCpInfo][1]}]");
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
//#endif
} /// If the process has a console attached to it, it will be detached and no longer visible. Writing to the System.Console is still possible, but no output will be shown.
public static void Hide()
{
//#if DEBUG
try
{
if (HasConsole)
{
SetOutAndErrorNull();
FreeConsole();
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
//#endif
} /// <summary>
/// 切换触发
/// </summary>
public static void Toggle()
{
try
{
if (HasConsole)
Hide();
else
Show();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
} #endregion
}
在项目中添加上述代码文件。
需要调用显示控制台的时候调用
ConsoleManager.Show(); //打开控制台窗口
方法打开系统控制台显示信息。
需要关闭显示控制台的时候调用
ConsoleManager.Hide(); //关闭控制台窗口
方法关闭已经打开的系统控制台。
为了方便切换,还封装了
ConsoleManager.Toggle(); //切换显示、隐藏窗口
在显示/隐藏间切换。
Ctrl+C按键事件
ConsoleManager.CtrlCPressed += (o, args) =>{}; // 触发Ctrl+C按键事件