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

时间秒杀一切 提交于 2019-12-21 05:36:06

问题


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!


回答1:


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.




回答2:


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;


来源:https://stackoverflow.com/questions/25570276/how-to-track-clipboard-changes-in-the-background-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!