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?
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.
wstring
clipboard copy, see #40665390 –
Unhinge 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.
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;
}
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:
A demo example shown here:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QClipboard>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString originalText = "My name is khan";
ui->label_text_to_copied->setText(originalText);
connect(ui->pushButton, &QPushButton::clicked,this,&MainWindow::copy_stuff);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::copy_stuff()
{
QClipboard *clipboard = QGuiApplication::clipboard();
QString text_to_be_copied = ui->label_text_to_copied->text();
clipboard->setText(text_to_be_copied);
ui->label_copy_status->setText("Copied!");
}
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.
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. –
Sigridsigsmond © 2022 - 2024 — McMap. All rights reserved.