检测和区分剪贴板事件(剪切,复制和粘贴) [英] Detect & Differentiate Clipboard Events (Cut,Copy and Paste)

查看:215
本文介绍了检测和区分剪贴板事件(剪切,复制和粘贴)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检测和区分文件的剪切,复制或粘贴?我只能检测到剪贴板中的更改。

is it possible for it to detect and differentiate cut,copy, or paste of files? I only can detect a change in the clipboard to far.

public partial class Form1 : Form
{
    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SetClipboardViewer(IntPtr hWnd);

    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

    private IntPtr _ClipboardViewerNext;

    private const int WM_DRAWCLIPBOARD = 0x0308;
    //  private const int WM_CUT = 0x0301;

    public Form1()
    {
        InitializeComponent();
    }

    private void StartClipboardViewer()
    {
        _ClipboardViewerNext = SetClipboardViewer(this.Handle);
    }

    private void StopClipboardViewer()
    {
        ChangeClipboardChain(this.Handle, _ClipboardViewerNext);
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_DRAWCLIPBOARD)
        {
            MessageBox.Show("Copied");
            SendMessage(_ClipboardViewerNext, m.Msg, m.WParam, m.LParam);
        }
        else
        {
            base.WndProc(ref m);
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        StartClipboardViewer();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        StopClipboardViewer();
    }

}


推荐答案

否,但是您可以为剪贴板编写一个包装器(因为它是无法从其派生的密封类)来跟踪获取/设置操作。

No, but you could write a wrapper for the Clipboard (as it's a sealed class you can't derive from it) to keep track of the get/set operations.

这篇关于检测和区分剪贴板事件(剪切,复制和粘贴)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆