copy unicode HTML to clipboard
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
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的更多相关文章
- 使用clipboard.js实现复制内容至剪贴板
下载插件 clipboard.js是不依赖flash,实现复制内容至剪贴板的js插件.下载clipboard.js的压缩包,根据需要选择dist目录下的压缩或未压缩版. github地址:https: ...
- 关于clipboard插件的使用问题
概述: clipboard.js是一款轻量级的实现复制文本到剪贴板功能的JavaScript插件.通过该插件可以将输入框,文本域,DIV元素中的文本等文本内容复制到剪贴板中 clipboard.js ...
- clipboard.js小说明
github主页 clipboard.js是一个github上的开源项目,可以实现纯 JavaScript (无 Flash)的浏览器内容复制到系统剪贴板的功能. 用法 <script type ...
- clipboard兼容各个浏览器,不需要设置权限
解决方案:用clipboard.js插件 注意要引用clipboard.js文件 案例: 将文本赋值给剪切板 <button class="btn">Copy</ ...
- Js 之复制到剪贴板 clipboard.js
一.下载 https://github.com/zenorocha/clipboard.js/archive/master.zip 二.Demo示例 <!DOCTYPE html> < ...
- vue中使用剪切板插件 clipboard.js
vue中使用剪切板需要借助一个插件,clipboard,使用方法还是很简单的,先下载,然后引入: npm i clipboard -S //引入 import Clipboard from 'clip ...
- SpaceVim - 让你的vim变得更加高效和强大
SpaceVim 中文手册 项 目 主 页: https://spacevim.org Github 地址 : https://github.com/SpaceVim/SpaceVim SpaceVi ...
- 看到了必须要Mark啊,最全的编程中英文词汇对照汇总(里面有好几个版本的,每个版本从a到d的顺序排列)
java: 第一章: JDK(Java Development Kit) java开发工具包 JVM(Java Virtual Machine) java虚拟机 Javac 编译命令 java ...
- WEB APPLICATION PENETRATION TESTING NOTES
此文转载 XXE VALID USE CASE This is a nonmalicious example of how external entities are used: <?xml v ...
随机推荐
- PYTHON-流程控制之if/while/for-练习
# 1 练习题## 简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型# 编译型:C, 谷歌翻译,一次翻译后结果后重复使用# 解释型:Python, 同声传译,边执行边 ...
- webpack文件笔记
webpack.prod.conf.js里面的ExtractTextPlugin,把css文件提取出来,专门进行打包minify :压缩 依赖的第三方库打包到vendor.js里面 每次项目打包的时候 ...
- js事件驱动函数
输入框 获得光标的这个行为叫做获取焦点 失去光标的这个行为叫做失去焦点 blur 失去焦点 1.获取标签的时候,一定要先等页面加载完成,再去获取这个标签. 可以将整个script代码写在body的下面 ...
- Java列表、数组、字符串
列表(list) list中添加,获取,删除元素 添加方法是:.add(e): 获取方法是:.get(index): 删除方法是:.remove(index), 按照索引删除: .remove(Obj ...
- IntelliJ IDEA 下的SVN使用
最近公司的很多同事开始使用IntelliJ Idea,便尝试了一下,虽然快捷键与eclipse 有些不同,但是强大的搜索功能与“漂亮的界面”(个人认为没有eclipse好看 ),还是值得我们去使用的. ...
- ERP完善合同起草(二十八)
前端的代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CRMCont ...
- Oracle分区表删除分区数据时导致索引失效解决
https://blog.csdn.net/e_wsq/article/details/80896258
- [CodeChef]GERALD07/[JZOJ4739]Ztxz16学图论
题解: 考虑从小到大枚举右端点 对于每个点,令它的权值等于它的编号 那么我们可以用lct维护出一颗最大生成树 维护方法是每次插入一条判断他们在不在一颗树上 若不在直接加,若在就找到链上的最小值 之后看 ...
- Flume+Kafka整合
脚本生产数据---->flume采集数据----->kafka消费数据------->storm集群处理数据 日志文件使用log4j生成,滚动生成! 当前正在写入的文件在满足一定的数 ...
- POJ 3304 Segments (叉乘判断线段相交)
<题目链接> 题目大意: 给出一些线段,判断是存在直线,使得该直线能够经过所有的线段.. 解题思路: 如果有存在这样的直线,过投影相交区域作直线的垂线,该垂线必定与每条线段相交,问题转化为 ...