22

I'm still a C++ newbie who has only recently learned some file manipulation. I looked it up online and the codes given are way beyond my current skill. Is there a simple way to do this, or are there any good tutorials that can explain this from the very basics?

  • 4
    On what operating system? In what framework? – Georg Fritzsche Jun 22 '11 at 07:17
  • Which operating system are you using? – iceaway Jun 22 '11 at 07:18
  • What operating system, and, in the case of Linux what clipboard? – R. Martinho Fernandes Jun 22 '11 at 07:19
  • 3
    Interaction with the clipboard is not part of the (C++)-language standard. You have to use API functions of your operating system or the usually simplified access functions if you use a framework. You can use the Qt framework, delivered with huge amount of tutorials, but you have to learn about the usage of Qt. Hope that will help – Uhli Jun 22 '11 at 07:25
  • Ah, I didn't know it was OS dependent. I'm using 32-bit Windows 7. –  Jun 22 '11 at 07:40
  • For a cross-platform way, using Qt, we can see http://stackoverflow.com/a/15742175 – Ganton Sep 10 '13 at 00:30

5 Answers5

28

In windows look at the following API:

An extensive discussion can be found here. Obviously this topic is strongly operating system related. And if you are using some framework (ie MFC/ATL) you generally find some helper infrastructure. This reply refer to the lowest API level in WIndows. If you are planning to use MFC have a look here, if you prefer ATL look here.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • 2
    And don't forget about [IsClipboardFormatAvailable](http://msdn.microsoft.com/en-ca/library/windows/desktop/ms649047%28v=vs.85%29.aspx). – Dave Jan 03 '15 at 08:31
  • for simple `wstring` clipboard copy, see https://stackoverflow.com/questions/40664890/copy-unicode-string-to-clipboard-isnt-working/68077157#68077157 – yu yang Jian Jun 22 '21 at 10:55
10

There is no cross-platform way to do this in C++


Now that we have that out of the way, Felice Pollano's answer provides the Windows API so you can manipulate the clipboard in Windows.

Apple provides an example application named ClipboardViewer and an entire reference to the NSPasteBoard and the functionality it provides.

As for Linux, it depends on what windowing manager you are running.

Community
  • 1
  • 1
X-Istence
  • 16,324
  • 6
  • 57
  • 74
5

There is a cross platform way to do this in C++, provided you are willing to use the Qt Library.

A solution for this is provided here:

https://stackoverflow.com/a/40437290/2158002

Anon
  • 2,267
  • 3
  • 34
  • 51
5

You can use ClipboardXX library for copy and pasting simple texts. Just download clipboardXX.hpp from github and copy it to your project path. Then follow its examples:

#include "clipboard.hpp"
#include <string>

int main() {
    clipboardxx::clipboard clipboard;

    // copy
    clipboard << "text you wanna copy";

    // paste
    std::string paste_text;
    clipboard >> paste_text;
}

-8

If you are looking for a simle way to do this : simulate the keyboard combination ctrl + v and you are done with it. On all platforms.

  • Not every application supports `Ctrl`+`C`/`V` even on platforms where that combination is common for copy/paste. The reason I ended up at this question was because I need to *implement* copy/paste functionality for `Ctrl`+`C`/`V` in a Windows application. – Stjepan Bakrac Jul 05 '18 at 15:21