Detect & Differentiate Clipboard Events (Cut,Copy

2019-08-15 08:02发布

问题:

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();
    }

}

回答1:

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.



回答2:

The clipboard does not differentiate between cut and copy. It's a semantic difference in the way that the source application treats the data (or files).