Solved

Windows Cut/Copy/Paste Event Handle using .NET

Posted on 2010-09-22
9
2,161 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:Amitava_Mukherjee
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:roxviper
ID: 33741683
0
 

Author Comment

by:Amitava_Mukherjee
ID: 33742010
The solution seems for a grid not to trap the Windows Paste event.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33746012
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.
0
 

Author Comment

by:Amitava_Mukherjee
ID: 33752131
I'm using .NEt 3.5, so can you provide some framework 3.5 code.
Thank You
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33755056
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

0
 

Author Comment

by:Amitava_Mukherjee
ID: 33760004
How to handle the Paste event through code.
Thank you.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 33760746
With SetClipboardViewer()?...you can't.  All that does is give you a way to determine when something has been copied to the clipboard...
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 250 total points
ID: 33760831
If you want to kow if something's been pasted into one of your windows you can override WndProc and catch the WM_PASTE message.  If you want to know if something's been pasted anywhere, you would need to use a native C++ DLL and the SetWindowsHookEx() API to be notified of a WM_PASTE message posted to any window.
0
 

Author Closing Comment

by:Amitava_Mukherjee
ID: 33767134
Thanx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now