简体   繁体   English

在c ++中以原始格式获取剪贴板数据

[英]Getting Clipboard data in raw format in c++

I've been playing around with the windows clipboard. 我一直在玩Windows剪贴板。 I noticed that you can only view the clipboard if you supply a format. 我注意到,如果您提供格式,则只能查看剪贴板。 I've seen programs that can dump the raw contents of the clipboard. 我见过程序可以转储剪贴板的原始内容。 Look at http://www.autohotkey.com/docs/misc/Clipboard.htm#ClipboardAll for an example of what I mean. 请查看http://www.autohotkey.com/docs/misc/Clipboard.htm#ClipboardAll ,了解我的意思。

Is there a way to do something similar, what I want to do is be able to back up the clipboard, manipulate it, then restore it when my program is done. 有没有办法做类似的事情,我想做的是能够备份剪贴板,操纵它,然后在我的程序完成后恢复它。

I'm looking for a non-.net solution if that's actually a thing 我正在寻找一个非网络解决方案,如果这实际上是一个东西

EDIT: 编辑:

I tried this so far: 到目前为止我试过这个:

struct clipData {
 vector<void*> data;
 vector<int> size;
};

struct clipData saveClipboard(int &size) {
 clipData ret;
 UINT currentFormat = 0;
 HGLOBAL hData;
 if (OpenClipboard(0)) {

  while(currentFormat = EnumClipboardFormats(currentFormat)) {
   hData = GetClipboardData(currentFormat);
   int currentClipboardFormatSize = GlobalSize(hData); //Only works with text formats. Help!
   char *savedClipboardData = new char[currentClipboardFormatSize];
   char *ptrToData = (char*) GlobalLock(hData);
   memcpy(savedClipboardData, ptrToData, currentClipboardFormatSize);
   ret.data.push_back(savedClipboardData);
   ret.size.push_back(currentClipboardFormatSize);
   }
  CloseClipboard();
 }
 return ret;
}

But the problem is theres no way to tell how big the clipboard is in each format 但问题是没有办法说出每种格式的剪贴板有多大

There's no "raw" data involved. 没有涉及“原始”数据。 Just enumerate all the formats currently on the clipboard , and fetch and save the contents of each format. 只需枚举剪贴板上当前的所有格式 ,然后获取并保存每种格式的内容。 But be careful of automatic format conversions. 但要小心自动格式转换。

If you carefully read the autohotkey documentation you linked, it even tells you that it's retrieving each format separately, and that it may only succeed in retrieving a subset of the formats. 如果您仔细阅读链接的autohotkey文档,它甚至会告诉您它正在单独检索每种格式,并且它只能成功检索格式的子集。

MSDN具有使用Clipboard API操作剪贴板数据所需的所有示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM