C # implement the Windows clipboard monitor,

Source: Internet
Author: User

C # implement the Windows clipboard monitor,
Windows clipboard

ClipBoard is an area in memory and a very useful tool built into Windows. It uses a small ClipBoard to build a color bridge between various applications, it is possible to transmit and share information. However, in the United States, the clipboard can only retain one copy of data. When new data is imported, the old one will be overwritten.

Related Windows APIs

The most important thing is SetClipboardViewer. Whenever the content of the clipboard changes, this function adds the window to the notified window chain through the WM_DRAWCLIPBOARD message.
Because the handle of the next window in the clipboard observer chain has not been returned, the application should not pass it in SetClipboardViewer
The WM_DRAWCLIPBOARD message received during the call.
If you want to remove the window chain from the clipboard observer chain, the application must call the changyunboard 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 left-click and left-click lifting. The application finally submits the messages to the window through GetMessage (WndProc [windows process]). pointer to the window process defined by an application.

We need to rewrite this function to handle the clipboard content change event:

# Region Message Process // Override WndProc to get messages... protected override void WndProc (ref Message m) {switch (m. msg) {case WM_DRAWCLIPBOARD: {// The clipboard has changed... //###################################### #####################################// Process Clipboard here :)........................ //###################################### ################################### SendMessage (mNextClipBoardViewerHWnd, m. msg, m. WParam. toInt32 (), m. LParam. toInt32 (); // display the text information in the Clipboard if (Clipboard. containsText () {label1.Text = Clipboard. getText ();} // display the image information in 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:



Download source code: c # implement Windows clipboard Monitor

Related Articles: VB. NET implements Windows clipboard Monitor


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.