How to copy unicode HTML code to the clipboard in html format, so it can be pasted into Writer, Word etc.

Several articles show working code to copy an html structure to the clipboard registering as HTML Format (so it can be pasted in a word processor - rendered - instead of pasting the html code).

This is my small contribution, just changing slightly the works you find in references, to also support fully unicode text, i.e. cyrillic copy and paste.

procedure TPHTML.CopyHTMLToClipBoardUnicode(const str, htmlStr: string);

/// Attempting to get real unicode to work.
/// Альма матер, альма матер, легкая лаDдья. /// Works fine with Delphi 2010+ and accepts unicode text. But if you try to copy Unicode
/// characters which do not have an ASCII representation, they will not be copied. /// Riccardo Zorn www.tmg.it
/// //The second parameter is optional and is put into the clipboard as CF_HTML.
//Function can be used standalone or in conjunction with the VCL clipboard so long as
//you use the USEVCLCLIPBOARD conditional define
//($define USEVCLCLIPBOARD}
//(and clipboard.open, clipboard.close).
//Code from http://www.lorriman.com
// http://it.w3support.net/index.php?db=so&id=1114883 // If you've ever tried sticking html into the clipboard using the usual CF_TEXT
// format then you might have been disappointed to discover that wysiwyg html
// editors paste your offering as if it were just text,
// rather than recognising it as html. For that you need the CF_HTML format.
// CF_HTML is entirely text format and uses the transformation format UTF-8.
// It includes a description, a context, and within the context, the fragment.
//
// As you may know one can place multiple items of data onto the clipboard for
// a single clipboard entry, which means that the same data can be pasted in a
// variety of different formats in order to cope with target
// applications of varying sophistocation.
//
// The following example shows how to stick CF_TEXT (and CF_HTML)
// into the clipboard. //The second parameter is optional and is put into the clipboard as CF_HTML.
//Function can be used standalone or in conjunction with the VCL clipboard so long as
//you use the USEVCLCLIPBOARD conditional define
//($define USEVCLCLIPBOARD}
//(and clipboard.open, clipboard.close).
//Code from http://www.lorriman.com function FormatHTMLClipboardHeader(HTMLText: string): string;
const
CrLf = #13#10;
begin
Result := 'Version:0.9' + CrLf;
Result := Result + 'StartHTML:-1' + CrLf;
Result := Result + 'EndHTML:-1' + CrLf; Result := Result + 'StartFragment:^^^^^^' + CrLf;
Result := Result + 'EndFragment:°°°°°°' + CrLf; Result := StringReplace(Result, '^^^^^^', Format('%.6d', [Length(Result)*SizeOf(Char)]), []); Result := Result + HTMLText + CrLf;
Result := StringReplace(Result, '°°°°°°', Format('%.6d', [Length(Result)*SizeOf(Char)]), []);
end; var
gMem: HGLOBAL;
lp: PChar;
Strings: array[0..1] of UTF8String;
Formats: array[0..1] of UINT;
i: Integer;
begin
gMem := 0;
{$IFNDEF USEVCLCLIPBOARD}
Win32Check(OpenClipBoard(0));
{$ENDIF}
try
//most descriptive first as per api docs
Strings[0] := FormatHTMLClipboardHeader(htmlStr);
Strings[1] := str;
Formats[0] := RegisterClipboardFormat('HTML Format');
Formats[1] := CF_UNICODETEXT;
{$IFNDEF USEVCLCLIPBOARD}
Win32Check(EmptyClipBoard);
{$ENDIF}
for i := 0 to High(Strings) do
begin
if Strings[i] = '' then Continue;
//an extra "1" for the null terminator
gMem := GlobalAlloc(GMEM_DDESHARE + GMEM_MOVEABLE, (Length(Strings[i])+1)*sizeOf(Char));
{Succeeded, now read the stream contents into the memory the pointer points at}
try
Win32Check(gmem <> 0);
lp := GlobalLock(gMem);
Win32Check(lp <> nil);
CopyMemory(lp, PChar(Strings[i]), (Length(Strings[i])+1)*sizeOf(Char));
finally
GlobalUnlock(gMem);
end;
Win32Check(gmem <> 0);
SetClipboardData(Formats[i], gMem);
Win32Check(gmem <> 0);
gmem := 0;
end;
finally
{$IFNDEF USEVCLCLIPBOARD}
Win32Check(CloseClipBoard);
{$ENDIF}
end;
end;

References:

Torry, working version in non-unicode Delphis

http://www.swissdelphicenter.ch/torry/showcode.php?id=1391

StackOverflow, working version in D2009 with no unicode support

http://stackoverflow.com/questions/1114883/how-do-i-put-some-formatted-text-into-the-clipboard

Using the clipboard (MSDN)

http://msdn.microsoft.com/en-us/library/ms649016%28v=VS.85%29.aspx#_win32_Registering_a_Clipboard_Format

CF_HTML

http://msdn.microsoft.com/en-us/library/aa767917%28v=vs.85%29.aspx

Delphi Unicode migration tips

http://edn.embarcadero.com/article/38693

copy unicode HTML to clipboard的更多相关文章

  1. 使用clipboard.js实现复制内容至剪贴板

    下载插件 clipboard.js是不依赖flash,实现复制内容至剪贴板的js插件.下载clipboard.js的压缩包,根据需要选择dist目录下的压缩或未压缩版. github地址:https: ...

  2. 关于clipboard插件的使用问题

    概述: clipboard.js是一款轻量级的实现复制文本到剪贴板功能的JavaScript插件.通过该插件可以将输入框,文本域,DIV元素中的文本等文本内容复制到剪贴板中  clipboard.js ...

  3. clipboard.js小说明

    github主页 clipboard.js是一个github上的开源项目,可以实现纯 JavaScript (无 Flash)的浏览器内容复制到系统剪贴板的功能. 用法 <script type ...

  4. clipboard兼容各个浏览器,不需要设置权限

    解决方案:用clipboard.js插件 注意要引用clipboard.js文件 案例: 将文本赋值给剪切板 <button class="btn">Copy</ ...

  5. Js 之复制到剪贴板 clipboard.js

    一.下载 https://github.com/zenorocha/clipboard.js/archive/master.zip 二.Demo示例 <!DOCTYPE html> < ...

  6. vue中使用剪切板插件 clipboard.js

    vue中使用剪切板需要借助一个插件,clipboard,使用方法还是很简单的,先下载,然后引入: npm i clipboard -S //引入 import Clipboard from 'clip ...

  7. SpaceVim - 让你的vim变得更加高效和强大

    SpaceVim 中文手册 项 目 主 页: https://spacevim.org Github 地址 : https://github.com/SpaceVim/SpaceVim SpaceVi ...

  8. 看到了必须要Mark啊,最全的编程中英文词汇对照汇总(里面有好几个版本的,每个版本从a到d的顺序排列)

    java:  第一章: JDK(Java Development Kit) java开发工具包 JVM(Java Virtual Machine) java虚拟机 Javac  编译命令 java   ...

  9. WEB APPLICATION PENETRATION TESTING NOTES

    此文转载 XXE VALID USE CASE This is a nonmalicious example of how external entities are used: <?xml v ...

随机推荐

  1. Android动画分类

    动画分类 View动画(补间动画).帧动画.属性动画 View动画(补间动画)包括:平移.旋转.缩放.透明度,View动画是一种渐近式动画 帧动画:图片切换动画 属性动画:通过动态改变对象的属性达到动 ...

  2. 洛谷 P4427 求和

    传送门啦 思路: 开始不肿么容易想到用倍增,但是想到需要求 $ Lca $ ,倍增这种常数小而且快的方法就很方便了.求 $ Lca $ 就是一个最普通的板子.那现在考虑怎么求题目中的结果. 树上差分可 ...

  3. LeetCode(43):字符串相乘

    Medium! 题目描述: 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = &quo ...

  4. 【ES】学习2-搜索

    1.空搜索 返回所有索引下的所有文档 GET /_search 设置超时.timeout 不是停止执行查询,它仅仅是告知正在协调的节点返回到目前为止收集的结果并且关闭连接.在后台,其他的分片可能仍在执 ...

  5. JQUery利用Uploadify插件实现文件异步上传(十一)

    一:简介: Uploadify是JQuery的一个上传插件,实现的效果非常好,带进度显示 ,且Ajax异步,能一次性上传多个文件,功能强大,使用简单 1.支持单文件或多文件上传,可控制并发上传的文件数 ...

  6. Could not find com.android.tools.build:aapt2:3.2.0-alpha14-4748712.

    https://blog.csdn.net/lx6101989/article/details/80334232 android studio 升级到了3.0 取消了apt 报了这个错 在最上级的bu ...

  7. [颜色知识] 潘通色卡、CMYK、RGB、 ARGB...

    潘通色卡C结尾的色号都是RGB色系的,而CMYK是印刷系的,两者不能通用的,所以,不能完全对等进行转换 Pantone Colors [色卡]PANTONE潘通色卡C面颜色http://blog.si ...

  8. tomcat常用的配置

    这里我们使用tomcat版本:apache-tomcat-7.0.77-windows-x64.zip 为例:下载链接地址为:https://archive.apache.org/dist/tomca ...

  9. poj 3415

    对拍没错..莫名wa了 利用容斥求每个串的重复子串 其实就是找到每个元素能扩展到的最大元素 即(rr-i)*(i-lr)*(w[i]-kk) 就可以了 然后处理这个先离散化再搞 另外是x y要清空 # ...

  10. Dockerfile中ENTRYPOINT 和 CMD的区别

    一.dockerfile中的 CMD 1.每个dockerfile中只能有一个CMD如果有多个那么只执行最后一个. 2.CMD 相当于启动docker时候后面添加的参数看,举个简单例子: docker ...