Link to home
Start Free TrialLog in
Avatar of Amitava_Mukherjee
Amitava_MukherjeeFlag for India

asked on

Windows Cut/Copy/Paste Event Handle using .NET

I want to develop a software which will monitor the file windows cut/copy/paste events like TeraCopy does. How to do this.
Please provide some sample code.
Thank you
Avatar of roxviper
roxviper
Flag of Romania image

Avatar of Amitava_Mukherjee

ASKER

The solution seems for a grid not to trap the Windows Paste event.
Avatar of Mike Tomlinson
You'd need to write a shell/namespace extension to do that...
http://blogs.msdn.com/b/codefx/archive/2010/09/14/writing-windows-shell-extension-with-net-framework-4-c-vb-net-part-1.aspx
http://msdn.microsoft.com/en-us/library/bb776905(VS.85).aspx

Not a "simple" thing by any means...and it's horribly un-documented...

*and I've never done it.
I'm using .NEt 3.5, so can you provide some framework 3.5 code.
Thank You
You can use the Windows API SetClipboardViewer to register to receive a Windows Message when something is copied to the clipboard; you can then use the .Net Clipboard class to retrieve it.
This simple example uses a class that derives from NativeWindow, registers itself as a "message-only" window, and overrides the WndProc method in order to handle the WM_DRAWCLIPBOARD and WM_CHANGECBCHAIN messages.  You could use it in your code a la:
ClipboardMonitor cbMonitor = new ClipboardMonitor();
cbMonitor.ClipboardCopyEvent += new EventHandler(cbMonitor_ClipboardCopyEvent);
private void cbMonitor_ClipboardCopyEvent(object sender, EventArgs e)
{
    MessageBox.Show("Something was copied to the clipboard.");
}

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ClipboardMonitor
{
	public enum WindowsMessages : int
	{
		DRAWCLIPBOARD = 0x308,
		CHANGECBCHAIN = 0x30d
	}

	public class ClipboardMonitor : NativeWindow, IDisposable
	{
		[DllImport("user32.dll")]
		private static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
		[DllImport("user32.dll")]
		private static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);

		private readonly IntPtr HWND_MESSAGE = new IntPtr(-3);

		private bool disposed = false;
		private IntPtr nextClipboardWindow;

		public event EventHandler ClipboardCopyEvent;

		public ClipboardMonitor()
		{
			this.CreateHandle(new CreateParams() { Parent = HWND_MESSAGE });
			nextClipboardWindow = SetClipboardViewer(this.Handle);
		}

		~ClipboardMonitor()
		{
			Dispose(false);
		}

		private void Dispose(bool disposing)
		{
			if (!disposed)
			{
				disposed = true;
				ChangeClipboardChain(this.Handle, nextClipboardWindow);
			}
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void OnClipboardCopyEvent(EventArgs e)
		{
			if (ClipboardCopyEvent != null)
				ClipboardCopyEvent(this, e);
		}

		protected override void WndProc(ref Message m)
		{
			switch ((WindowsMessages)m.Msg)
			{
				case WindowsMessages.DRAWCLIPBOARD:
					if (nextClipboardWindow != null)
						SendMessage(nextClipboardWindow, (uint)m.Msg, m.WParam, m.LParam);
					OnClipboardCopyEvent(new EventArgs());
					break;
				case WindowsMessages.CHANGECBCHAIN:
					if (m.LParam != this.Handle)
					{
						if (m.LParam == nextClipboardWindow)
							nextClipboardWindow = m.LParam;
						else
							SendMessage(nextClipboardWindow, (uint)m.Msg, m.WParam, m.LParam);
					}
					m.Result = IntPtr.Zero;
					break;
			}
			base.WndProc(ref m);
		}
	}
}

Open in new window

How to handle the Paste event through code.
Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanx