Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Các hàm clrscr, gotoxy, textcolor, textBKcolor, ... trong lập trình C#

    Buổi chiều nay ngồi giải mấy bài toán của các bạn đưa lên diễn đàn, mới học được chút C# mọn, và nghĩ đến việc viết mấy Hàm như: clrscr, gotoxy, textcolor, textBKcolor, ... nghịch chơi trên Console của C#.

    Nói thật, làm xong chỉnh đi, chỉnh lại. Bây giờ cảm thấy thoả mãn lắm rồi. Up lên chia sẻ cho bà con nè.

    Để sử dụng được, các bạn phải thêm một Class vào Project của mình, sau đó copy đoạn code sau vào đó, rồi lưu lại với file có tên là ConsoleExt.cs. Lần sau sử dụng chỉ cần add cái class này vào Project Solution của mình thôi. (Hướng dẫn sử dụng và demo xem ở dưới nhé)


    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é:
    Khi sử dụng nhớ phải cho dòng sau đây vào nhé:
    using Dr.LearnCSharp.DrConsole;

    Demo

    Mã:
    using System;using Dr.LearnCSharp.DrConsole;namespace Dr.LearnCSharp.TestConsoIO{    class Program    {        static void Main(string[] args)        {            ConsoleExt MyConOI = new ConsoleExt();            Console.WriteLine("THIS TEXT IS ORIGINAL COLOR");            MyConOI.TextColor((int)ConsoleExt.Foreground.Red +                              (int)ConsoleExt.Foreground.Intensity);            Console.WriteLine("THIS TEXT IS RED");            MyConOI.TextColor((int)ConsoleExt.Foreground.Red +                              (int)ConsoleExt.Foreground.Blue + (int)ConsoleExt.Foreground.Intensity);            Console.WriteLine("NOW THE TEXT PINK");            MyConOI.TextColor((int)ConsoleExt.Foreground.Blue +                              (int)ConsoleExt.Foreground.Intensity +                              (int)ConsoleExt.Background.Green +                              (int)ConsoleExt.Background.Intensity);            Console.WriteLine("NOW THE TEXT IS BLUE AND BACKGROUND OF IT IS GREEN");            Console.WriteLine("Hit Enter to Clear");            Console.ReadLine();  // Wait for user input            MyConOI.ResetColor(); // Reset the text color back to normal            MyConOI.ClearScreen(); // Clear the screen            MyConOI.gotoxy(3, 3); // move the cursor to (3,3)            Console.WriteLine("THE CONSOLE WAS CLEARED");            Console.WriteLine("Hit Enter to Terminate");            Console.ReadLine();  // Wait for user input        }    }}
    Dùng thử đi, đảm bảo tuyệt vời lắm đó, và chỉ duy nhất ở diễn đàn cộng đồng C Việt có thôi. (Đoán thế)
    Có điều cái này nó không dịch qua MSIL đâu nhá. Không biết làm bằng MSIL thì phải làm cách nào? Ai biết tham gia góp vui nhé!

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    - Thứ nhất, cậu đang dùng dao mổ trâu để giết gà. Tất cả các method trên có thể viết lại trong khoảng 10 dòng.

    - Thứ hai, các method phải là static. Cậu để ý trong .net, nó có khi nào Console myCon = new Console() hay ko ?

    - Thứ ba,

    Có điều cái này nó không dịch qua MSIL đâu nhá. Không biết làm bằng MSIL thì phải làm cách nào?
    nghĩa là sao ?
    - Thứ tư, ...

    Thôi ko nhận xét nữa, mất công cậu lại bảo thử viết một cái xem nào =_="

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •