在 c++ 中获取原始格式的剪贴板数据
我一直在玩Windows剪贴板。我注意到,如果您提供格式,则只能查看剪贴板。我见过可以转储剪贴板原始内容的程序。看看 http://www.autohotkey.com/docs/misc/Clipboard.htm #ClipboardAll 是我的意思的一个例子。
有没有办法做类似的事情,我想做的是能够备份剪贴板,操作它,然后在我的程序完成时恢复它。
我正在寻找一个非.net解决方案,如果这实际上是一件事
编辑:
到目前为止我尝试过:
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;
}
但问题是没有办法知道每种格式的剪贴板有多大
I've been playing around with the windows clipboard. 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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不涉及“原始”数据。只需枚举剪贴板上当前的所有格式,然后获取并保存每种格式的内容。但要小心自动格式转换。
如果您仔细阅读链接的自动热键文档,它甚至会告诉您它正在单独检索每种格式,并且它可能只能成功检索格式的子集。
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.
MSDN 拥有您需要的所有示例了解如何使用 Clipboard API 操作剪贴板数据。
MSDN has all the examples you need to know to manipulate clipboard data using Clipboard API.