Windows clipboard
The Clipboard (ClipBoard) is an area in memory that is a very useful tool built into Windows, with a small clipboard that allows you to transfer and share information between applications by building a color bridge. However, the drawback is that the Clipboard can only keep one copy of the data, whenever new data is passed in, the old will be overwritten.
Related Windows APIs
The main thing is setclipboardviewer, whenever the contents of the Clipboard change, the function uses the Wm_drawclipboard message to add the window to the window chain being notified.
Because the handle to the next window in the Clipboard viewer chain has not been returned, the application should not pass it on SetClipboardViewer
The Wm_drawclipboard message received during the call.
If you want to remove the window chain from the Clipboard Viewer chain, the application must call the Changeclipboard member function.
#region definitions //constants for API Calls ... Private Const int wm_drawclipboard = 0x308; Private Const int wm_changecbchain = 0x30d; Handle for next clipboard viewer ... Private IntPtr Mnextclipboardviewerhwnd; API declarations ... [DllImport ("user32.dll", CharSet = CharSet.Auto)] static public extern IntPtr SetClipboardViewer (IntPtr hwndnewviewer); [DllImport ("user32.dll", CharSet = CharSet.Auto)] static public extern bool Changeclipboardchain (IntPtr HWnd, IntPtr hwndnext); [DllImport ("user32.dll", CharSet = CharSet.Auto)] public static extern int SendMessage (IntPtr hWnd, int msg, int wParam, int lParam); #endregion
WndProc function
The operating system sends a series of messages to the application, such as the left button press and the button lift, and the application will eventually submit the message to the window procedure (wndproc[full name, Windows process]) to a pointer to an application-defined window procedure, such as GetMessage.
We need to override this function to handle clipboard content change events:
#region Message Process//override WndProc to get messages ... protected Override void WndProc (ref Me Ssage m) {switch (m.msg) {case Wm_drawclipboard: { The Clipboard has changed ...//################################################# #########################//Process Clipboard here:) ............... ########################################################################## SendMessage (MNEXTCL Ipboardviewerhwnd, M.msg, M.wparam.toint32 (), M.lparam.toint32 ()); Displays the text information in the Clipboard if (Clipboard.containstext ()) {Lab Gls. Text = Clipboard.gettext (); }//Displays the picture information on the Clipboard if (Clipboard.containsimage ()) { pictureBox1.Image = Clipboard.getimage (); Picturebox1.update (); } break; } case Wm_changecbchain: {//another Clipboard viewer has removed itself ... if (M.wparam = = (INTPTR) mnextclipboardviewerhwnd) { Mnextclipboardviewerhwnd = m.LParam; } else {SendMessage (Mnextclipboardviewerhwnd, M. MSG, M.wparam.toint32 (), M.lparam.toint32 ()); } break; }} base. WndProc (ref m); } #endregion
Effect: