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?
- Clipboardchangenotifier Clipchange = new Clipboardchangenotifier ();
- clipchange.clipboardchanged + = new EventHandler (clipchange_clipboardchanged);
- Clipchange.assignhandle (This. Handle);
- 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?
- 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?
- [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;
[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?
- Addclipboardformatlistener (This. Handle);
Addclipboardformatlistener (this. Handle);
Remove the monitor from the Clipboard when the form is closed:
[CSharp]View Plaincopyprint?
- Removeclipboardformatlistener (This. Handle);
Removeclipboardformatlistener (this. Handle);
When you receive a message to the Clipboard update, read the Clipboard contents:
[CSharp]View Plaincopyprint?
- Protected override void Defwndproc (ref Message m)
- {
- if (m.msg = = wm_clipboardupdate)
- {
- Updateclipvaluelist ();
- }
- Else
- {
- base. Defwndproc (ref m);
- }
- }
WIN7 using. NET (C #) to monitor the Clipboard (GO)