监测打印Console到控制台输出之C#设计笔记(六)

一、修改项目输出类型:控制台应用程序
在这里插入图片描述
二、设计开关及输出信息类,详见代码
public class ConsoleShow
{
[DllImport(“kernel32.dll”)]
public static extern bool AllocConsole();
[DllImport("User32.dll ", EntryPoint = “FindWindow”)]
private static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport(“user32.dll “, EntryPoint = “GetSystemMenu”)]
extern static IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
[DllImport(“user32.dll “, EntryPoint = “RemoveMenu”)]
extern static int RemoveMenu(IntPtr hMenu, int nPos, int flags);
[DllImport(“kernel32.dll”)]
public static extern bool FreeConsole();
public static void CloseConsole()
{
FreeConsole();
}
public static void OpenConsole()
{
FreeConsole();
AllocConsole();
//根据控制台标题找控制台
int WINDOW_HANDLER = FindWindow(null, Console.Title);
//A找关闭按钮 次代码和B下的代码是禁用打开窗口的关闭按钮(同时也会关闭程序)
IntPtr CLOSE_MENU = GetSystemMenu((IntPtr)WINDOW_HANDLER, IntPtr.Zero);
int SC_CLOSE = 0xF060;
//B关闭按钮禁用 (A和B可根据情况选用)
RemoveMenu(CLOSE_MENU, SC_CLOSE, 0x0);
Console.WriteLine(“程序已启动”);
}
public static void WriteLine(string output)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine(@”[{0}]{1}”, DateTimeOffset.Now, output);
System.Diagnostics.Debug.WriteLine(@”[{0}]{1}”, DateTimeOffset.Now, output);
}
三、应用程序在启动时调用CloseConsole(),关闭控制台,在根据用户需求适时调用OpenConsole();
四、应用程序在操作时,调用WriteLine即可显示具体日志信息,如下图。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值