写一个C++,MFC在UNICODE下将HTML源码以可视化方式复制到剪切板的函数

void SentHTML2ClipBrd(CString html)
{
	static int clipformat = 0;   
    if(!clipformat)   clipformat   =   RegisterClipboardFormat(L"HTML Format");


	CString info =	L"Version:0.9\r\n"
					L"StartHTML:00000000\r\n"
					L"EndHTML:00000000\r\n"
					L"StartFragment:00000000\r\n"
					L"EndFragment:00000000\r\n"
					L"<html><body>\r\n"
					L"<!--StartFragment-->\r\n";
	info+=html;;
	info+=L"\r\n";
	info+=L"<!--EndFragment-->\r\n";
	info+=L"</body>\r\n";
	info+=L"</html>";


	int starthtml = info.Find(L"<");
	int endhtml = info.Find(L"</html>")+7;
	int startfragment = info.Find(L"<!--StartFragment-->")+21;
	int endfragment = info.Find(L"<!--EndFragment-->")-2+GetCHWordCount(html)*2;
	
	CString head;
	CString temp;


	head = L"StartHTML:";
	temp.Format(L"%08u",starthtml);
	head+=temp;
	info.Replace(L"StartHTML:00000000",head);


	head = L"EndHTML:";
	temp.Format(L"%08u",endhtml);
	head+=temp;
	info.Replace(L"EndHTML:00000000",head);


	head = L"StartFragment:";
	temp.Format(L"%08u",startfragment);
	head+=temp;
	info.Replace(L"StartFragment:00000000",head);


	head = L"EndFragment:";
	temp.Format(L"%08u",endfragment);
	head+=temp;
	info.Replace(L"EndFragment:00000000",head);


	head = L"StartSelection:";
	temp.Format(L"%08u",startfragment);
	head+=temp;
	info.Replace(L"StartSelection:00000000",head);


	head = L"EndSelection:";
	temp.Format(L"%08u",endfragment);
	head+=temp;
	info.Replace(L"EndSelection:00000000",head);


	if(::OpenClipboard(0))
	{
		info = ConvertCString2UTF8(info);
		EmptyClipboard();
		char* date = CStringToCharArray(info);


		HGLOBAL   hText   =   GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,strlen(date)+4);
		char * clipptr = (char*)GlobalLock(hText);
		strcpy(clipptr,date);SetClipboardData(clipformat,hText);
		GlobalUnlock(hText);
		CloseClipboard();
	}
	else
	{
		GlobalFree(hText);
	}
}
GetCHWordCount

就是获取传入html字符串内的中文数目,本代码为剪切板0.9版本,后续会续写1.0

 

2012年10月23日

ConvertCStringW2UTF8 返回后的 char* 用后应当释放,否则会变成野指针.

另外代码有一处漏洞. 

 

int endhtml = info.Find(L"</html>")+7;

一行,换成

 

 

 
int endhtml = info.Find(L"</html>")+7+GetCHWordCount(html)*2;

 

 

 

 

 

之前因为测试时向剪切板发送的网页源码较少,所以仅仅几个汉字是没关系的,

EndHTML:00000000//这个数值会错误.修正后错误解决

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Afterwards_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值