วิธีการล้างข้อมูลหน้าต่างคอนโซลโดยทางโปรแกรม โดยใช้ Visual c#

หมายเลขบทความ (Article ID): 319257 - ผลิตภัณฑ์ที่เกี่ยวข้องในบทความนี้
ขยายทั้งหมด | ยุบทั้งหมด

เนื้อหาบนหน้านี้

สรุป

บทความที่มีการทีละขั้นตอนนี้อธิบายวิธีการล้างข้อมูลหน้าต่างคอนโซลโดยทางโปรแกรม โดยใช้ Visual c#

โปรแกรมตัวอย่าง

  1. เริ่ม Microsoft Visual Studio
  2. สร้างโครงการ Visual c# คอนโซลแอพพลิเคชันใหม่
  3. วางต่อไปนี้การใช้คำสั่งที่ด้านบนของคลาเริ่มต้น:
       using nsClearConsole;
    					
  4. วางรหัสต่อไปนี้ในการหลักขั้นตอนของแอพลิเคชันคอนโซล:
    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. ในการProjectเมนู คลิกเพิ่มระดับชั้น.
  6. ในการเพิ่มรายการใหม่กล่องโต้ตอบ เลือกคลาสแล้ว คลิกadd.
  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)]
             struct	CONSOLE_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) ต่อไปนี้:
ฟังก์ชันของคอนโซล
http://msdn2.microsoft.com/en-us/library/ms682073.aspx

คุณสมบัติ

หมายเลขบทความ (Article ID): 319257 - รีวิวครั้งสุดท้าย: 13 มกราคม 2554 - Revision: 5.0
ใช้กับ
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005 Express Edition
Keywords: 
kbsweptvs2008 kbhowtomaster kbmt KB319257 KbMtth
แปลโดยคอมพิวเตอร์
ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ
ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:319257

ให้ข้อเสนอแนะ