Detect & Differentiate Clipboard Events (Cut,Copy and Paste)

南笙酒味 提交于 2020-01-06 02:49:04

问题


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



来源:https://stackoverflow.com/questions/9908156/detect-differentiate-clipboard-events-cut-copy-and-paste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!