". Net deep breathing" Listening for clipboard updates (System after Vista)

Source: Internet
Author: User

The method for XP and previous monitoring of the Clipboard changes is not spoken, because XP is heavily outdated. This old-week approach is designed for systems above Vista.

Listening to clipboard update behavior in a managed application must use the Win + API, let me just say it first.

First, call the Addclipboardformatlistener function to register the listener behavior with the window, which requires a window handle as an incoming parameter, and the window that the handle refers to is the window that listens for the Clipboard update.

Then, when the contents of the Clipboard are updated, the handler receives a wm_clipboardupdate message. As soon as we receive this message in our application, the contents of the Clipboard have been updated.

The macro definition for the Wm_clipboardupdate message is as follows:

#define Wm_clipboardupdate              0x031d

The wParam and lParam parameters of this message are not used, so we don't have to ignore the two parameter values. If the user has processed the message, it should return 0.

The Addclipboardformatlistener function is prototyped as follows:

BOOL WINAPI Addclipboardformatlistener (  _in_ hwnd hwnd);

It is called in managed code to be imported first.

        [DllImport ("User32.dll")]         Static extern BOOL Addclipboardformatlistener (INTPTR hwnd);

Well, the basic theory is over, let's see how to listen to clipboard updates in a WPF program.

Because this functionality is actually an interaction between WPF and Win32, this class exposes a Addhook method, which is called by the HwndSource class, which can be used to add a HwndSourceHook delegate instance, which is invoked when the window receives the message.

The delegate is defined as follows.

Delegate int ref bool handled);

You will suddenly discover that this delegate is much like the WinProc function pointer. The MSG parameter is the message that is intercepted. In the method that binds to the delegate, we can filter the received message because we only care about the wm_clipboardupdate message here, the rest of us don't care.

        Private int ref BOOL handled)        {            if (msg = = wm_clipboardupdate)            {                clipboardupdated?. Invoke (this, eventargs.empty);                 return IntPtr.Zero;            }             return IntPtr.Zero;        }

The clipboardupdated event was defined when I encapsulated it for ease of triggering.

   Public Event EventHandler clipboardupdated;

One more thing, you'll find that HwndSource instances are created with a handle bound to a window, so how to get a handle to the instance of Windows, which requires a helper class--windowinterophelper. With it, it's easy to get a handle to a window.

            New windowinterophelper (window);             = Hwndsource.fromhwnd (helper. Handle);

Be sure to call the Addclipboardformatlistener function to register the listener behavior for the window before adding the hook processing.

bool r = Addclipboardformatlistener (_hwndsource.handle);

If the monitoring behavior is registered successfully, you can add hooks.

            if (R)            {                _hwndsource.addhook (new  HwndSourceHook (onhooked));            }

So, how do we encapsulate this code in the window code? Window has a sourceinitialized event that occurs when the handle initialization is complete. We can rewrite the onsourceinitialized method and then use the code we encapsulated above in the method.

Clipboardhooker M_clipboardhooker; protected Override voidonsourceinitialized (EventArgs e) {Base.            Onsourceinitialized (e); M_clipboardhooker=NewClipboardhooker ( This); M_clipboardhooker.clipboardupdated+=onclipboardupdated; }        Private voidOnclipboardupdated (Objectsender, EventArgs e) {TB. Text="boss, someone has modified the Clipboard. "; IDataObject Data=Clipboard.getdataobject (); string[] fs =data.            GetFormats (); Tb. Text+= $"\ n Data format: {string. Join ("、", FS)}"; }

As soon as you hear the Clipboard being updated, it's easy to get the data on the Clipboard, because System.Windows has a Clipboard class that has a bunch of static methods to read and write directly to the contents of the Clipboard.

After running the program, just copy the point to the Clipboard, you will see the program is responding. As shown in HD uncensored below.

Okay, here's the article.

This article sample source code download

". Net deep breathing" Listening for clipboard updates (System after Vista)

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.