首页 > 解决方案 > 为什么某些应用程序会更改其 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:
--------------------------------

标签: windowsclipboard

解决方案


感谢@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。


推荐阅读