Mã:
//This is Class file//Save as ConsoleExt.cs using System;using System.Runtime.InteropServices; namespace Dr.LearnCSharp.DrConsole{ public class ConsoleExt { 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")] private static extern int FillConsoleOutputAttribute(int hConsoleOutput, int wAttribute, int nLength, COORD dwWriteCoord, ref int lpNumberOfAttrsWritten); [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); [DllImport("kernel32.dll", EntryPoint = "SetConsoleTextAttribute", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] private static extern bool SetConsoleTextAttribute(int hConsoleOutput, int Attributes); public enum Foreground { Blue = 0x00000001, Green = 0x00000002, Red = 0x00000004, Intensity = 0x00000008 } public enum Background { Blue = 0x00000010, Green = 0x00000020, Red = 0x00000040, Intensity = 0x00000080 } private int hConsoleHandle; private int OriginalColors; private CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo; public ConsoleExt() { hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); ConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO(); GetConsoleScreenBufferInfo(hConsoleHandle, ref ConsoleInfo); OriginalColors = ConsoleInfo.wAttributes; } public void ClearScreen() // Clear the Console window { 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); FillConsoleOutputAttribute(hConsoleHandle, 0, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, ref hWrittenChars); SetConsoleCursorPosition(hConsoleHandle, Home); } public void gotoxy(short col, short row) // Moves the cursor to the given position in the current console window { COORD Home; Home.x=col; Home.y=row; SetConsoleCursorPosition(hConsoleHandle, Home); } public void TextColor(int color) // Set the text color { SetConsoleTextAttribute(hConsoleHandle, color); } public void ResetColor() // Reset the text color back to normal { SetConsoleTextAttribute(hConsoleHandle, OriginalColors); } }}
Còn đây là cách sử dụng. Bạn chỉ cần copy nó vào file Project của mình và dịch nó rồi quan sát cách sử dụng nhé:
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...