How to track clipboard changes in the background u

2019-03-31 18:24发布

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

How can I do this?

I need an event that will be called each time when the clipboard is changed. It does not matter from which the application is copying.

I know the function for reading and writing, such as GetClipboardData() and SetClipboardData().

Got any ideas how to do this in C++?

Thanks in advance!

2条回答
2楼-- · 2019-03-31 18:46

Take a look at Monitoring Clipboard Contents:

A clipboard viewer window displays the current content of the clipboard, and receives messages when the clipboard content changes. To create a clipboard viewer window, 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;
查看更多
Explosion°爆炸
3楼-- · 2019-03-31 18:47

Since Windows Vista, the right 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 of monitoring changes to the clipboard. The oldest method is to create a clipboard viewer window. Windows 2000 added the ability to query the clipboard sequence number, and Windows Vista added support for clipboard format listeners. Clipboard viewer windows are supported for backward compatibility with earlier versions of Windows. New programs should use clipboard format listeners or the clipboard sequence number.

查看更多
登录 后发表回答