C # Implementation of the Windows Clipboard Monitor feature example "with demo source download" _c# Tutorial

Source: Internet
Author: User

The example in this article describes the Windows Clipboard Monitor feature implemented by C #. Share to everyone for your reference, specific as follows:

Windows clipboard

The Clipboard (ClipBoard) is an area of memory, a very useful tool built into Windows, with a small clipboard that sets up a color bridge, making it possible to pass and share information between applications. In the ointment, however, the Clipboard retains only one piece of data, and whenever new data is introduced, the old ones are overwritten.

Related Windows APIs

The main thing is setclipboardviewer, which, whenever the contents of the Clipboard changes, adds the window to the chain of windows that are notified by Wm_drawclipboard messages.

Because the handle of the next window in the Clipboard watcher chain has not returned, the application should not pass it in the SetClipboardViewer

The Wm_drawclipboard message received during the call.

If you want to remove the window chain from the Clipboard watcher 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 a left-click and A-button lift, and the application will eventually submit the message to the window procedure (wndproc[English full name Windows process]) to a pointer to an application-defined window procedure via GetMessage.

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 ()); Displays the text information in the Clipboard if (Clipboard.containstext ()) {Label1.
        Text = Clipboard.gettext (); //Show picture information in Clipboard if (Clipboard.containsimage ()) {pictureBox1.Image = Clipboard.getimage (
          );
        Picturebox1.update ();
      } break; Case Wm_changecbchain: {//another Clipboard viewer has removed itself ... if (M.wparam = = (Int
     PTR) Mnextclipboardviewerhwnd)   {Mnextclipboardviewerhwnd = m.LParam;
        else {SendMessage (Mnextclipboardviewerhwnd, M.msg, M.wparam.toint32 (), M.lparam.toint32 ());
      } break; } base.
WndProc (ref m);

 } #endregion

Effect Chart:

Full instance code click here to download the site.

Read more about C # Interested readers can view the site topics: "C # string Operation Tips Summary", "C # array Operation tips Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "C # Programming Thread Usage Skills summary", "C # Summary of Operation Excel skills, summary of WinForm control usage, C # tutorial on data structure and algorithms, and Introduction to C # object-oriented programming

I hope this article will help you with C # programming.

Related Article

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.