Skip to content

Instantly share code, notes, and snippets.

@idiotandrobot
Created April 30, 2016 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idiotandrobot/c22549ba57fd68240c32d53a82121533 to your computer and use it in GitHub Desktop.
Save idiotandrobot/c22549ba57fd68240c32d53a82121533 to your computer and use it in GitHub Desktop.
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AddClipboardFormatListener(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool RemoveClipboardFormatListener(IntPtr hwnd);
private const int WM_CLIPBOARDUPDATE = 0x031D;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_CLIPBOARDUPDATE)
{
var data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Text))
{
string text = (string)data.GetData(DataFormats.Text);
}
else if (data.GetDataPresent(DataFormats.Bitmap))
{
Bitmap image = (Bitmap)data.GetData(DataFormats.Bitmap);
}
}
}
AddClipboardFormatListener(this.Handle);
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
RemoveClipboardFormatListener(this.Handle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment