[C#] Hiệu ứng gõ phím Smooth typing Effect RichTextBox giống 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#:
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#:
PASSWORD UNZIP: HUNG.PRO.VN
Chúc các bạn thành công với thủ thuật trên.
Theo LapTrinhVB.Net