windows - 为什么某些应用程序会更改其 Windows 剪贴板格式代码的值?
问题描述
我有一个应用程序可以将一些 HTML 格式的富文本写入 Windows 剪贴板。文本旨在手动粘贴到其他应用程序中,例如浏览器、MS Word、Thunderbird 电子邮件客户端等。将内容添加到剪贴板需要指定格式代码。
我使用该SetClipboardData(UINT uFormat, HANDLE hMem)
功能。我uFormat
通过转储剪贴板的原始内容并检查其格式代码来凭经验得出结论。
问题在于,对于某些应用程序,格式代码似乎每天都在变化。我观察到它是:0xC10E、COFD、C0FE、C0FB、C10D、C11F、C0FC、C104 等。MS Word 接受任何东西;Firefox 和 Thunderbird 将拒绝除一个特定代码之外的任何内容。
有没有办法从程序中确定格式代码du jour是什么?
我已经查看了SetClipboardData、剪贴板格式和使用剪贴板
这是代码片段:
OpenClipboard();
EmptyClipboard();
int length; // text length gets set elsewhere
HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, length + 1);
LPSTR lpstr = (LPSTR)GlobalLock(hglbCopy);
memcpy(lpstr, source_text, length);
lpstr[length] = 0;
GlobalUnlock(hglbCopy);
// Place the handle on the clipboard.
HANDLE ret = SetClipboardData(0xC109, hglbCopy);
CloseClipboard();
这是一个典型的剪贴板转储。为了得到这个,我必须每次都明确地从应用程序中选择和复制文本,所以这不是找出应用程序格式代码的实用方法。
xC009
xC1B3
xC104
xC25F
xC269
CF_UNICODETEXT
CF_TEXT
xC26A
xC013
CF_LOCALE
CF_OEMTEXT
x0
--------------------------------
xC009:
u
--------------------------------
xC1B3:
C
--------------------------------
xC104:
Version:0.9
StartHTML:00000280
EndHTML:00000408
StartFragment:00000314
EndFragment:00000372
SourceURL:mailbox:///C:/Users/***/AppData/Roaming/Thunderbird/Profiles/5y3ow05s.default/Mail/Local%20Folders/Vendors.sbd/X.sbd/***?number=2454
<html><body>
<!--StartFragment-->Can your team be of assistance with the following problem?<!--EndFragment-->
</body>
</html>
--------------------------------
xC25F:
<
--------------------------------
xC269:
0
--------------------------------
CF_UNICODETEXT:
C
--------------------------------
CF_TEXT:
Can your team be of assistance with the following problem?
--------------------------------
xC26A:
m
--------------------------------
xC013:
--------------------------------
CF_LOCALE:
--------------------------------
CF_OEMTEXT:
Can your team be of assistance with the following problem?
--------------------------------
x0:
--------------------------------
解决方案
感谢@Hans Passant 简洁而精辟的评论,我发现了我的代码中缺少的内容:
// Retrieve or define "HTML Format": if already defined, returns the format code, else defines a new one
UINT HTMLformat = RegisterClipboardFormat("HTML Format");
我没有意识到剪贴板格式具有标准名称以及与之关联的数值。这些名称被各种应用程序识别。
还要感谢@IInspectable。
推荐阅读
- python - 检查行级别是否仅填充一列的简洁方法
- html - 为什么我可以将标题放在中心。但不能把标题孩子放在中心?
- vb.net - 从程序中查看此进程的任务管理器中的所有内容
- python - 在 Python 中的一行中分配变量
- postgresql - 通过从单个表中选择插入到两个引用表中
- php - laravel 与 cpanel 中的 mysql 数据库的连接被拒绝
- python - CMake 找到合适的 Python 版本,但无法使用解释器
- ios - iPhone 5C (iOS 10.3.3) 视图在使用时在浏览器上变白: setInput({ ...input})
- spring-boot - 使用 Google 和 API 访问的 Spring OAuth2 登录
- c++ - 来自 Ubuntu 21.10 的 GCC 11.2 和 Compiler Explorer 中的版本为用作迭代集合的 C 字符串产生不同的输出