C# FAQ: How monitor changes to Clipboard contents

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

How monitor changes to Clipboard contents?

How can changes to the Clipboard contents be monitored? For example, an application may need to enable or disable items on the Edit menu depending on the presence of a paste-able item.

Call the SetClipboardViewer function in the initialization code passing in the form handle. Then, the form will receive WM_DRAWCLIPBOARD messages that can be handled in WndProc.

using System.Runtime.InteropServices;
 
...
 
[DllImport("user32.dll")]
 
private const int WM_DRAWCLIPBOARD = 776;
 
public static extern int SetClipboardViewer (int windowHandle);
 
...
 
// within the initialization code
 
SetClipboardViewer (this.Handle.ToInt32());
 
protected override void WndProc (ref Message message)
{
   base.WndProc (ref message);    // Process the message 
   if (message.Msg == WM_DRAWCLIPBOARD)
   {
      // act on Clipboard changes
   }
}


Personal tools