NEWS

[C#] Hiệu ứng gõ phím Smooth typing Effect RichTextBox giống MS Word 2013

[C#] Hiệu ứng gõ phím Smooth typing Effect RichTextBox giống MS Word 2013
Đăng bởi: Thảo Meo - Lượt xem: 4329 08:05:24, 02/06/2020DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ chia sẽ đến các bạn cách thực hiện hiệu ứng gõ phím Smooth Typing Effect trên RichTextBox like Microsoft Word 2013 trên C# winform.

[C#] Smooth Typing Effect in RichTextBox like MS Word 2013

Từ phiên bản office 2013, các bạn sẽ thấy Microsoft có tích hợp thêm hiệu ứng smooth typing khi chúng ta soạn thảo văn bản.

Khi gõ text, con nháy đơn sẽ di chuyển từ từ giống đồng hồ chạy tự động nhìn rất mượt.

Hình ảnh demo ứng dụng Smooth Typing Effect C#:

smooth_typing_csharp

Bạn nhìn hình demo trên, các bạn dễ dàng thấy con nháy đơn soạn thảo nó di chuyển từ từ.

Để chỉnh tốc độ nhanh hay chậm các bạn thay đổi thông số ở Thread.Sleep này nhé.

Thread.Sleep(20); // set speed effect slow or fast

Full source code Smooth Typing Effect C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SmoothTypingText
{
    public partial class Form1 : Form
    {       
        [DllImport("user32.dll")] static extern bool SetCaretPos(int x, int y);
        [DllImport("user32.dll")] static extern Point GetCaretPos(out Point point);
        public int speed = 20;
        public Form1()
        {
            InitializeComponent();
            Point targetCaretPos;
            GetCaretPos(out targetCaretPos);            
            richTextBox1.TextChanged += (s, e) =>
            {               
                Point temp;
                GetCaretPos(out temp);
                SetCaretPos(targetCaretPos.X, targetCaretPos.Y);
                targetCaretPos = temp;
            };
          
            Thread t = new Thread(() =>
            {
                Point current = targetCaretPos;
                while (true)
                {
                    if (current != targetCaretPos)
                    {
                       
                        if (Math.Abs(current.X - targetCaretPos.X) + Math.Abs(current.Y - targetCaretPos.Y) > 30)
                            current = targetCaretPos; 
                        else
                        {
                            current.X += Math.Sign(targetCaretPos.X - current.X);
                            current.Y += Math.Sign(targetCaretPos.Y - current.Y);
                        }

                      
                        richTextBox1.Invoke((Action)(() => SetCaretPos(current.X, current.Y)));
                    }                    
                    Thread.Sleep(20); // set speed effect slow or fast
                }
            });
            t.IsBackground = true; 
            t.Start();
        }
        
    }
}

Video demo ứng dụng smooth effect typing c#:

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hiệu ứng gõ phím Smooth typing Effect RichTextBox giống MS Word 2013
Đăng bởi: Thảo Meo - Lượt xem: 4329 08:05:24, 02/06/2020DEVEXPRESS   In bài viết

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

Đọc tiếp
.