- [C#] Giới thiệu Component WebView2 của Microsoft
- [C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
- [DATABASE] Hướng dẫn import và export hình ảnh image từ Sqlserver
- [DATABASE] Hướng dẫn sử dụng Hàm ASCII trong sqlserver
- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
[C#] Bắt sự kiện thay đổi clipboard windows
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn các bắt sự kiện thay đổi clipboard windows trong lập trình C# winform.
[C#] Trigger event changed clipboard windows
- Khi các bạn sử dụng tổ hợp phím
Ctrl + C
để copy một gì đó, thì chúng sẽ được đưa vào clipboard trong windows. - Trên windows 10, các bạn có thể bấm phím
Windows + V
để xem lịch sử clipboard
Ở bài này, mình sẽ sử dụng thư viện SharpClipboard để bắt sự kiện người dùng copy dữ liệu bất kỳ ở ứng dụng nào, thì tool của mình sẽ hiển thị ở đó.
Dưới đây là giao diện demo ứng dụng Clipboard C#:
Ở giao diện trên clipboard được chia là 3 loại chính: Text, Image và Link...
Ngoài ra thư viện còn support cho mình biết là mình copy vào clipboard từ ứng dụng nào trên Windows.
Full source code event changed clipboard C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WK.Libraries.SharpClipboardNS;
namespace ClipboardChanged
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
chkMonitorClipboard.Checked = sharpClipboard1.MonitorClipboard;
chkObserveTexts.Checked = sharpClipboard1.ObservableFormats.Texts;
chkObserveFiles.Checked = sharpClipboard1.ObservableFormats.Files;
chkObserveImages.Checked = sharpClipboard1.ObservableFormats.Images;
}
private void sharpClipboard1_MonitorClipboardChanged(object sender, EventArgs e)
{
chkMonitorClipboard.Checked = sharpClipboard1.MonitorClipboard;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
sharpClipboard1.MonitorClipboard = chkMonitorClipboard.Checked;
}
private void chkObserveTexts_CheckedChanged(object sender, EventArgs e)
{
sharpClipboard1.ObservableFormats.Texts = chkObserveTexts.Checked;
}
private void chkObserveImages_CheckedChanged(object sender, EventArgs e)
{
sharpClipboard1.ObservableFormats.Images = chkObserveImages.Checked;
}
private void chkObserveFiles_CheckedChanged(object sender, EventArgs e)
{
sharpClipboard1.ObservableFormats.Files = chkObserveFiles.Checked;
}
private void sharpClipboard1_ClipboardChanged(object sender, SharpClipboard.ClipboardChangedEventArgs e)
{
if (e.ContentType == SharpClipboard.ContentTypes.Text)
{
txtCopiedTexts.Text = sharpClipboard1.ClipboardText;
}
else if (e.ContentType == SharpClipboard.ContentTypes.Image)
{
pbCopiedImage.Image = sharpClipboard1.ClipboardImage;
}
else if (e.ContentType == SharpClipboard.ContentTypes.Files)
{
List<string> files = new List<string>();
foreach (string file in sharpClipboard1.ClipboardFiles)
{
files.Add(Path.GetFileName(file));
}
Debug.WriteLine(sharpClipboard1.ClipboardFiles.ToArray());
lstCopiedFiles.Items.Clear();
lstCopiedFiles.Items.AddRange(files.ToArray());
}
else if (e.ContentType == SharpClipboard.ContentTypes.Other)
{
txtCopiedTexts.Text = sharpClipboard1.ClipboardText?.ToString();
}
textBox2.Text =
$"Name: {e.SourceApplication.Name} \n" +
$"Title: {e.SourceApplication.Title} \n" +
$"ID: {e.SourceApplication.ID} \n" +
$"Handle: {e.SourceApplication.Handle} \n" +
$"Path: {e.SourceApplication.Path}";
}
}
}
Thanks for watching!