Windows Develop Bookmark and Share   
 index > Windows Forms General > Clipboard Clear
 

Clipboard Clear

Hi all,

I recently try to write a program that clear all the clipboard contents if exist. The program will run in the background and monitor the clipboard, if any change with the clipboard, the program will clear it without notify the user. I have some ideas to work with the WM_DRAWCLIPBOARD (in this case, if WM_DRAWCLIPBOARD was sent, the program will clear the clipboard but it's only works if my form receives the focus. After that, I do some search and find out about WM_CLIPBOARDUPDATE but not much info about this.

Could anyone give me some help please?
I'm very appreciate this.
mylove69  Friday, November 16, 2007 4:34 AM

Hi, mylove69,

Based on my understanding, you want to clear the ClipBoard when the content is changed, don't you?

Well, I don't think WM_CLIPBOARDUPDATE notification is suitable for you.

Please read this article

http://msdn2.microsoft.com/En-US/library/ms649021.aspx

Its minimum operating systems isWindows Vista, so you may probably not be able to use it on other computers.

And I think WM_DRAWCLIPBOARD could work in your situation, but you should learn some more about ClipBoard in Windows.

In fact, there is a ClipboardChain in Windows and once the content in ClipBoard is changed, the first window of the chainwill get the WM_DRAWCLIPBOARD notification. So all you have to do is to set your window to the first window.

Please check this sample.

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);

}

http://msdn2.microsoft.com/en-us/library/ms649025.aspx

Hope this helps,

Regards

Yu Guo â€?MSFT  Wednesday, November 21, 2007 8:01 AM

Hi, mylove69,

Based on my understanding, you want to clear the ClipBoard when the content is changed, don't you?

Well, I don't think WM_CLIPBOARDUPDATE notification is suitable for you.

Please read this article

http://msdn2.microsoft.com/En-US/library/ms649021.aspx

Its minimum operating systems isWindows Vista, so you may probably not be able to use it on other computers.

And I think WM_DRAWCLIPBOARD could work in your situation, but you should learn some more about ClipBoard in Windows.

In fact, there is a ClipboardChain in Windows and once the content in ClipBoard is changed, the first window of the chainwill get the WM_DRAWCLIPBOARD notification. So all you have to do is to set your window to the first window.

Please check this sample.

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);

}

http://msdn2.microsoft.com/en-us/library/ms649025.aspx

Hope this helps,

Regards

Yu Guo â€?MSFT  Wednesday, November 21, 2007 8:01 AM
Oh, thank you so much.
After all, I figure that I dont SendMessage to the nextViewer so that the reason why it just clears the clipboard when get the focus.
Thank you so much.
mylove69  Friday, November 23, 2007 9:30 AM

You can use google to search for other answers

Custom Search

More Threads

• BackgroundWorker
• Create tree like Spy++ using Active Accessibility
• Getting the State of a Mouse Button
• inherited toolstrip events question
• databinding List<Int32> with wpf listview, how to get list index?
• Maximize\minimize a child forum inside its parent
• very new and confussed sorry if wrong place to post??
• *GDI+* Change the DPI?
• Windows usercontrol is not accepting any child controls
• Binding DataGrid to multi-dimensional array