How do you copy/paste from the clipboard in C++?

后端 未结 5 1838
情话喂你
情话喂你 2020-12-03 07:30

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 wa

相关标签:
5条回答
  • 2020-12-03 08:06

    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.

    0 讨论(0)
  • 2020-12-03 08:07

    You can use ClipboardXX.

    Just download ClipboardXX.hpp from github and copy it to your project path. Then you can do something like:

    #include "../ClipboardXX.hpp"
    #include <string>
    
    int main(){
        CClipboardXX clipboard;
    
        // copy
        clipboard‌ << "text you wanna copy";
    
        // paste
        std::string paste_text;
        clipboard >> paste_text;
    }
    

    This is cross platform too.

    0 讨论(0)
  • 2020-12-03 08:15

    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

    0 讨论(0)
  • 2020-12-03 08:20

    In windows look at the following API:

    • OpenClipBoard
    • EmptyClipboard
    • SetClipboardData
    • CloseClipboard
    • GetClipboardData

    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.

    0 讨论(0)
  • 2020-12-03 08:23

    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.

    0 讨论(0)
提交回复
热议问题