WinForm程序启动控制台窗口Console

本文转载:http://blog.csdn.net/oyi319/article/details/5753311

2.WinForm程序和控制台窗口Console

如果你调试过SharpDevelop的源程序,会发现它在DEBUG模式时会出现一个控制台窗口,以显示日志信息。或许我使用的方法与其不同,不过你可以试一试,写出我们自己的调试日志代码。

 

首先要解决的问题是如何在Debug模式时显示Console窗口。我确定,这是一个WinForm项目,也没有改过它的输出类型。我们需要在项目的入口点使用一些API函数将控制台显示出来:

它们是 AllocConsole 和 FreeConsole。

 

[c-sharp] view plaincopy
 
  1. [DllImport("kernel32.dll")]  
  2. public static extern Boolean AllocConsole();  
  3. [DllImport("kernel32.dll")]  
  4. public static extern Boolean FreeConsole();  

 

然后我们使它在Main()开始处判断DEBUG编译标记,调用AllocConsole方法显示控制台,然后在Main()的结束处判断DEBUG编译标记,调用FreeConsole方法关闭控制台。这样,我们就可以使用Console.Write等方法将调试信息显示在这个控制台窗口里。

 

为了达到更好的效果,我们写一个Shell类,用它来封装Console.WriteLine方法,输出个性化信息。我是这样做的,根据输出到控制台的文本的前几个字判断为“警告”、“错误”、“注意”时,输出带有黄色、红色、绿色的文字,其他输出信息输出控制台缺省的灰色文字,以起到区分效果,还要在每条信息前加上输出信息的当时时间。

 

这个Shell类是这样的:

 

[c-sharp] view plaincopy
 
  1. /// <summary>  
  2. /// 与控制台交互  
  3. /// </summary>  
  4. static class Shell  
  5. {  
  6.     /// <summary>  
  7.     /// 输出信息  
  8.     /// </summary>  
  9.     /// <param name="format"></param>  
  10.     /// <param name="args"></param>  
  11.     public static void WriteLine(string format, params object[] args)  
  12.     {  
  13.         WriteLine(string.Format(format, args));  
  14.     }  
  15.   
  16.     /// <summary>  
  17.     /// 输出信息  
  18.     /// </summary>  
  19.     /// <param name="output"></param>  
  20.     public static void WriteLine(string output)  
  21.     {  
  22.         Console.ForegroundColor = GetConsoleColor(output);  
  23.         Console.WriteLine(@"[{0}]{1}", DateTimeOffset.Now, output);  
  24.     }  
  25.   
  26.     /// <summary>  
  27.     /// 根据输出文本选择控制台文字颜色  
  28.     /// </summary>  
  29.     /// <param name="output"></param>  
  30.     /// <returns></returns>  
  31.     private static ConsoleColor GetConsoleColor(string output)  
  32.     {  
  33.         if (output.StartsWith("警告")) return ConsoleColor.Yellow;  
  34.         if (output.StartsWith("错误")) return ConsoleColor.Red;  
  35.         if (output.StartsWith("注意")) return ConsoleColor.Green;  
  36.         return ConsoleColor.Gray;  
  37.     }  
  38. }  

 

 

那么程序入口函数Main代码如下:

 

[c-sharp] view plaincopy
 
  1. /// <summary>  
  2. /// 应用程序的主入口点。  
  3. /// </summary>  
  4. [STAThread]  
  5. static void Main()  
  6. {  
  7. #if DEBUG  
  8.     AllocConsole();  
  9.     Shell.WriteLine("注意:启动程序...");  
  10.   
  11.     Shell.WriteLine("/tWritten by Oyi319");  
  12.     Shell.WriteLine("/tBlog: http://blog.csdn.com/oyi319");  
  13.     Shell.WriteLine("{0}:{1}", "警告", "这是一条警告信息。");  
  14.     Shell.WriteLine("{0}:{1}", "错误", "这是一条错误信息!");  
  15.     Shell.WriteLine("{0}:{1}", "注意", "这是一条需要的注意信息。");  
  16.     Shell.WriteLine("");  
  17. #endif  
  18.     Application.EnableVisualStyles();  
  19.     Application.SetCompatibleTextRenderingDefault(false);  
  20.     Application.Run(new Form1());  
  21. #if DEBUG  
  22.     Shell.WriteLine("注意:2秒后关闭...");  
  23.     Thread.Sleep(2000);  
  24.     FreeConsole();  
  25. #endif  
  26. }  

 

 

现在这个控制台窗口,只会在DEBUG模式时显示,而在Release编译时不会出现。 这是不是你想要的调试方法呢?

posted @ 2014-09-26 09:17  跟着阿笨一起玩.NET  阅读(22214)  评论(7编辑  收藏  举报