C # Implementing the Code for Windows Clipboard Monitor in detail

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.