NEWS

[C#] Bắt sự kiện thay đổi clipboard windows

[C#] Bắt sự kiện thay đổi clipboard windows
Đăng bởi: Thảo Meo - Lượt xem: 4279 08:28:35, 28/08/2020PHẦN MỀM

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#:

sharp_clipboard_csharp

Ở 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.

sharpclipboard-usage-01

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!

DOWNLOAD SOURCE

Tags: trigger event changed clipboard c#event changed clipboard c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Bắt sự kiện thay đổi clipboard windows
Đăng bởi: Thảo Meo - Lượt xem: 4279 08:28:35, 28/08/2020PHẦN MỀM