Code Block
[DllImport("User32.dll")]
protected static extern int SetClipboardViewer(int hWndNewViewer);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
IntPtr nextViewer;
const int WM_DRAWCLIPBOARD = 0x308;
private void Form1_Load(object sender, EventArgs e)
{
nextViewer = (IntPtr)SetClipboardViewer((int)this.Handle); //Here we set to the first window.
this.Disposed += new EventHandler(Form1_Disposed);
}
void Form1_Disposed(object sender, EventArgs e)
{
ChangeClipboardChain(this.Handle, nextViewer); //Give the control to next viewer
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_DRAWCLIPBOARD)
{
Console.Write("Changed");
SendMessage(nextViewer, m.Msg, m.WParam, m.LParam);
Clipboard.Clear();
}
base.WndProc(ref m);
}