2

I am currently making a win32 console application in c++.

Now, I have to react on the user changing the clipboard content. Currently I am just checking for the user pressing ctrl+c, but obviously that's not enough since it won't track rightclick->copy, etc.

Sadly i cannot just use the winmessage, since i am developing a console application, hence my application does not have its own hwnd.

And i really do not want to copy the clipboard data 10 times a second or so. This is bound to cause problems with other programs since i have to lock and unlock the clipboard every time.

Any suggestions?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
L. Etienne
  • 77
  • 7
  • Duplicate of https://stackoverflow.com/q/1641182/107625 – Uwe Keim Dec 02 '18 at 19:48
  • 2
    you need register for this clipboard listener (vista+) via [`AddClipboardFormatListener`](https://learn.microsoft.com/en-us/windows/desktop/api/Winuser/nf-winuser-addclipboardformatlistener) and unregister at the end [`RemoveClipboardFormatListener`](https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-removeclipboardformatlistener). *hence my application does not have its own hwnd.* - of course this is wrong. nothing prevent you from create window – RbMm Dec 02 '18 at 20:03
  • 1
    you will got [`WM_CLIPBOARDUPDATE`](https://learn.microsoft.com/en-us/windows/desktop/dataxchg/wm-clipboardupdate) when the contents of the clipboard have changed. – RbMm Dec 02 '18 at 20:09
  • 1
    @uwe: That's an unrelated Q&A. – IInspectable Dec 02 '18 at 20:33

1 Answers1

5

To monitor clipboard changes, you can use the AddClipboardFormatListener API:

When a window has been added to the clipboard format listener list, it is posted a WM_CLIPBOARDUPDATE message whenever the contents of the clipboard have changed.

An application that doesn't have a GUI can create a message-only window to receive change notifications:

A message-only window enables you to send and receive messages. It is not visible [...]. The window simply dispatches messages.

IInspectable
  • 46,945
  • 8
  • 85
  • 181