WinForm程式啟動控制台視窗Console

來源:互聯網
上載者:User

標籤:winform   style   blog   http   color   io   使用   ar   strong   

本文轉載: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編譯時間不會出現。 這是不是你想要的調試方法呢?

WinForm程式啟動控制台視窗Console

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.