Using. NET (C #) to monitor the Clipboard under WIN7

Source: Internet
Author: User

Recently need to do a small program, need to be resident backstage, monitor clipboard changes and extract content, on-line check some information, first adopted the SetClipboardViewer method to achieve, the specific principle can refer to http://www.cnblogs.com/jht/archive/ 2006/03/20/354088.html, my program is the use of http://code.google.com/p/clipboardviewer/provided ClipboardChangeNotifier.cs class, more convenient, The class code is shown in the attachment (downloaded or http://www.kuaipan.com.cn/file/id_22823997376823393.htm in Google Code) using the following method: When the main form is initialized:

[CSharp]View Plaincopyprint?
    1. Clipboardchangenotifier Clipchange = new Clipboardchangenotifier ();
    2. clipchange.clipboardchanged + = new EventHandler (clipchange_clipboardchanged);
    3. Clipchange.assignhandle (This.  Handle);
    4. Clipchange.install ();
    Clipboardchangenotifier Clipchange = new Clipboardchangenotifier ();    Clipchange.clipboardchanged + = new EventHandler (clipchange_clipboardchanged);    Clipchange.assignhandle (this. Handle);    Clipchange.install ();

When the main form exits:

[CSharp]View Plaincopyprint?
    1. Clipchange.uninstall ();
    Clipchange.uninstall ();

This program runs normally under XP, but later in the use of WIN7 encountered some problems, often after the screen saver or system hibernation, no longer monitor the Clipboard in real-time, the specific reasons unknown, and later looked up the information, It has been mentioned that the SetClipboardViewer function is not very stable in the version above Vista, it is recommended to use Addclipboardformatlistener, the new API function, MSDN (HTTP/ Msdn.microsoft.com/en-us/library/windows/desktop/ms649033%28v=vs.85%29.aspx) mentioned that the API function can only be used for Vista and above, tested, the program is working properly
The code is simpler than the SetClipboardViewer method: first declaring the API function

[CSharp]View Plaincopyprint?
  1. [DllImport ("user32.dll")]
  2. public static extern bool Addclipboardformatlistener (INTPTR hwnd);
  3. [DllImport ("user32.dll")]
  4. public static extern bool Removeclipboardformatlistener (INTPTR hwnd);
  5. private static int wm_clipboardupdate = 0x031d;
    [DllImport ("User32.dll")]        public static extern bool Addclipboardformatlistener (INTPTR hwnd);        [DllImport ("User32.dll")]        public static extern bool Removeclipboardformatlistener (INTPTR hwnd);        private static int wm_clipboardupdate = 0X031D;

Add monitoring to the Clipboard when the form is initialized:

[CSharp]View Plaincopyprint?
    1. Addclipboardformatlistener (This.  Handle);
Addclipboardformatlistener (this. Handle);

Remove the monitor from the Clipboard when the form is closed:

[CSharp]View Plaincopyprint?
    1. Removeclipboardformatlistener (This.  Handle);
  Removeclipboardformatlistener (this. Handle);

When you receive a message to the Clipboard update, read the Clipboard contents:

[CSharp]View Plaincopyprint?
  1. Protected override void Defwndproc (ref Message m)
  2. {
  3. if (m.msg = = wm_clipboardupdate)
  4. {
  5. Updateclipvaluelist ();
  6. }
  7. Else
  8. {
  9. base.  Defwndproc (ref m);
  10. }
  11. }

WIN7 using. NET (C #) to monitor the Clipboard (GO)

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.