How to track clipboard changes in the background using C ++

I need to process the contents of the clipboard in a background application.

How can i do this?

I need an event that will be called every time the clipboard changes. It doesn't matter where the app is copied from.

I know a read and write function like GetClipboardData()

and SetClipboardData()

.

Any ideas how to do this in C ++?

Thank you in advance!

+3


source to share


2 answers


Starting with Windows Vista, the correct method is to use clipboard format listeners:

case WM_CREATE: 
  // ...
  AddClipboardFormatListener(hwnd);
  // ...
  break;

case WM_DESTROY: 
  // ...
  RemoveClipboardFormatListener(hwnd);
  // ...
  break;

case WM_CLIPBOARDUPDATE:
  // Clipboard content has changed
  break;

      




See Monitoring Clipboard Contents :

There are three ways to track changes to the clipboard. The oldest way is to create a clipboard viewer. Windows 2000 added the ability to query the clipboard sequence number, and Windows Vista added support for clipboard format listeners . Clipboard viewers are supported for backward compatibility with earlier versions of Windows. New programs must use clipboard format listeners or clipboard ordinal.

+5


source


Take a look at Monitoring Clipboard Contents :

The clipboard viewer displays the current contents of the clipboard and receives when the contents of the clipboard change. To create a clipboard viewport, your application must do the following:

Add the window to the clipboard viewer chain.
Process the WM_CHANGECBCHAIN message.
Process the WM_DRAWCLIPBOARD message.
Remove the window from the clipboard viewer chain before it is destroyed.

      

Adding a window to the clipboard viewer chain:



case WM_CREATE: 

    // Add the window to the clipboard viewer chain. 

    hwndNextViewer = SetClipboardViewer(hwnd); 
    break;

      

Processing the WM_CHANGECBCHAIN ​​message:

case WM_CHANGECBCHAIN: 

    // If the next window is closing, repair the chain. 

    if ((HWND) wParam == hwndNextViewer) 
        hwndNextViewer = (HWND) lParam; 

    // Otherwise, pass the message to the next link. 

    else if (hwndNextViewer != NULL) 
        SendMessage(hwndNextViewer, uMsg, wParam, lParam); 

    break;

      

+2


source







All Articles