NEWS

[C#] Hiệu ứng Typewriter effect trên console

[C#] Hiệu ứng Typewriter effect trên console
Đăng bởi: Thảo Meo - Lượt xem: 2472 13:42:28, 15/07/2021DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục chia sẽ đến các bạn tạo hiệu ứng Typewriter Effect ở cửa sổ dòng lệnh Console C#.

[C#] Typewriter Effect Console 

Dưới đây, là giao diện demo ứng dụng:

Do hình động gif, nên demo dưới chạy bạn cảm thấy giật, mở ứng dụng chạy mướt nhé các bạn.

text_typing_console

Đầu tiên, các bạn tạo cho mình 1 class TypeWriter.cs :

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TextEffectTyping
{
    class TypeWriter : System.IO.TextWriter
    {
        private TextWriter originalOut;

        public TypeWriter()
        {
            originalOut = Console.Out;
        }

        public override void WriteLine(string message)
        {
            foreach (char c in message)
            {
                originalOut.Write(c);

                if (!Console.KeyAvailable)
                {
                    Thread.Sleep(25);
                }
            }

            if (Console.KeyAvailable)
            {
                Console.ReadKey();
            }

            Animate();

            if (Console.KeyAvailable)
            {
                Console.ReadKey();
            }

            originalOut.WriteLine();
        }

        private void Animate()
        {
            Console.CursorVisible = false;

            for (int i = 0; i < 3; i++)
            {
                originalOut.Write(@"-");
                Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                if (AnimationInterrupted()) break;
                originalOut.Write(@"\");
                Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                if (AnimationInterrupted()) break;
                originalOut.Write(@"|");
                Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                if (AnimationInterrupted()) break;
                originalOut.Write(@"/");
                Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                if (AnimationInterrupted()) break;
            }

            originalOut.Write(" ");
            Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);

            Console.CursorVisible = true;
        }

        private bool AnimationInterrupted()
        {
            if (!Console.KeyAvailable)
            {
                Thread.Sleep(50);
                return false;
            }
            else
            {
                originalOut.Write(" ");
                Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                return true;
            }
        }

        public override Encoding Encoding
        {
            get { return Encoding.UTF8; }
        }
    }
    }

Và ở file program.cs các bạn sử dụng như sau để xuất nội dung text ra:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TextEffectTyping
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Read();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.OutputEncoding = Encoding.UTF8;
            TypeWriter typewriter = new TypeWriter();
            Console.SetOut(typewriter);
            Console.WriteLine("Thủ tướng sắp ban hành chỉ thị mới về phòng chống dịch Covid-19");
            Console.WriteLine("Trước bối cảnh dịch Covid-19 lây lan nhanh, Bộ Y tế vừa có tờ trình Thủ tướng về việc ban hành chỉ thị tập trung cao độ thực hiện hiệu quả các biện pháp phòng, chống dịch.");
            Console.WriteLine("Theo Bộ Y tế, từ đầu năm 2020, dịch Covid-19 ở Việt Nam đã trải qua 4 giai đoạn, với gần 30.000 ca mắc và 125 ca tử vong. Giai đoạn 4 từ ngày 27/4 đến nay với sự xuất hiện của biến chủng Delta đã lây lan rất nhanh, nguy hiểm hơn và làm tăng bệnh nặng hơn so với 3 đợt dịch trước.");
            Console.ReadKey();
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hiệu ứng Typewriter effect trên console
Đăng bởi: Thảo Meo - Lượt xem: 2472 13:42:28, 15/07/2021DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.