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

来源:互联网 发布:江西淘宝大学电话 编辑:程序博客网 时间:2024/06/05 18:05
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//这个数值会错误.修正后错误解决

原创粉丝点击