如何使用視覺化的 C#,以程式設計方式清除主控台視窗

這份文件的 Visual Basic 版本,請參閱。下列的 Microsoft.NET Framework 類別庫命名空間會參照這份文件:

  • 系統

  • System.Runtime.InteropServices

本文內容

摘要

本文將逐步示範如何使用視覺化的 C#,以程式設計方式清除主控台視窗。

範例程式

  1. 啟動 Microsoft Visual Studio。

  2. 建立新的視覺 C# 主控台應用程式專案。

  3. 貼上下列的using陳述式在頂端的預設類別:

       using nsClearConsole;
  4. 在主控台應用程式的Main程序中,貼上下列程式碼:

    static void Main(string[] args){   ClearConsole ClearMyConsole = new ClearConsole();   Console.WriteLine("THIS IS FIRST LINE");  // Some text   Console.WriteLine("THIS IS SECOND LINE"); // Some text    Console.WriteLine("THIS IS THIRD LINE");  // Some text   Console.WriteLine("THIS IS FOURTH LINE"); // Some text     Console.WriteLine("THIS IS FIFTH LINE");  // Some text   Console.WriteLine("Hit Enter to Clear");  // Some text   Console.ReadLine();  // Wait for user input   ClearMyConsole.Clear(); // Clear the screen   Console.WriteLine("THE CONSOLE WAS CLEARED");  // Some text to clear console   Console.WriteLine("Hit Enter to Terminate");  //Some text   Console.ReadLine();  // Wait for user input}
  5. 在 [專案] 功能表上按一下 [加入類別]。

  6. 在 [加入新項目] 對話方塊中,選取類別,然後再按一下 [新增。

  7. 所有的新類別中現有的程式碼取代下列程式碼中:

       using System;   using System.Runtime.InteropServices;   namespace nsClearConsole   {      /// <summary>      /// Summary description for ClearConsole.      /// </summary><BR/>      public class ClearConsole      {         private const int STD_OUTPUT_HANDLE  = -11; private const byte EMPTY = 32;         [StructLayout(LayoutKind.Sequential)]         struct COORD         {            public short x;            public short y;         }         [StructLayout(LayoutKind.Sequential)]         struct SMALL_RECT         {            public short Left;            public short Top;            public short Right;            public short Bottom;         }         [StructLayout(LayoutKind.Sequential)]         structCONSOLE_SCREEN_BUFFER_INFO         {            public COORD dwSize;            public COORD dwCursorPosition;            public int wAttributes;            public SMALL_RECT srWindow;            public COORD dwMaximumWindowSize;         }         [DllImport("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]         private static extern int GetStdHandle(int nStdHandle);         [DllImport("kernel32.dll", EntryPoint="FillConsoleOutputCharacter", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]         private static extern int FillConsoleOutputCharacter(int hConsoleOutput, byte cCharacter, int nLength, COORD dwWriteCoord, ref int lpNumberOfCharsWritten);         [DllImport("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]         private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);         [DllImport("kernel32.dll", EntryPoint="SetConsoleCursorPosition", SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]         private static extern int SetConsoleCursorPosition(int hConsoleOutput, COORD dwCursorPosition);         private int hConsoleHandle;         public ClearConsole()         {            //             // TODO: Add constructor logic here.            //             hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);         }         public void Clear()         {            int hWrittenChars = 0;            CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO();            COORD Home;            Home.x = Home.y = 0;            GetConsoleScreenBufferInfo(hConsoleHandle, ref strConsoleInfo);            FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, ref hWrittenChars);            SetConsoleCursorPosition(hConsoleHandle, Home);         }      }   }
  8. 請按 F5 執行應用程式。請注意,主控台視窗中的文字被清除。

參考

如需有關主控台函式的詳細資訊,請造訪下列 Microsoft 開發人員網路 (MSDN) 網站:

主控台函式

需要更多協助?

擴展您的技能
探索訓練
優先取得新功能
加入 Microsoft 測試人員

這項資訊有幫助嗎?

感謝您的意見反應!

感謝您的意見反應! 我們將協助您與我們的其中一個 Office 支援專員連絡以深入了解您的意見。

×