2012-03-28 101 views
1

是否有可能檢測並區分剪切,複製或粘貼文件?我只能在剪貼板中檢測到更改。檢測和區分剪貼板事件(剪切,複製和粘貼)

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

不,這是一個程序實現細節,你不能用剪貼板查看器看到。您只能看到剪貼板的更改。 – 2012-03-28 13:17:40

回答

1

沒有,但你可以寫剪貼板的包裝(因爲它是你無法從中獲得一個密封類)來跟蹤了get/set操作。

0

剪貼板不區分剪切和複製。這是源應用程序處理數據(或文件)的方式的語義差異。