检测&区分剪贴板事件(剪切,复制和粘贴)

时间:2012-03-28 13:06:26

标签: c# events clipboard

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

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

}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

剪贴板不区分剪切和复制。这是源应用程序处理数据(或文件)的方式的语义差异。