Author: Wuhuan
Test environment: Windows 7
Development Environment: Visual Studio 2008 SP1
Refer to msdn and help to test the code in VC as follows:
C ++ Language: Bool cbasedialogdlg: copyhtmltoclip (const cstring & strhtml)
{
// 1) convert to UTF-8
Cw2a pszu8 (ct2w (strhtml), cp_utf8 );
Int nhtmlsrclen = strlen (pszu8 );
// 2) form a clipboard clip
Cstringa strhtmlclip;
Strhtmlclip. Format ("version: 0.9 \ r \ n"
"Starthtml: % 08u \ r \ n"
"Endhtml: % 08u \ r \ n"
"Startfragment: % 08u \ r \ n"
"Endfragment: % 08u \ r \ n"
"<HTML> <body> \ r \ n"
"<! -- Startfragment --> \ r \ n"
"% S \ r \ n"
"<! -- Endfragment --> \ r \ n"
"</Body> 97,172 + nhtmlsrclen, 111,136 + nhtmlsrclen, pszu8 );
// 3) clipboard operation
Static int CFID = 0;
If (! CFID)
{
CFID = registerclipboardformat (_ T ("HTML format "));
}
If (! Openclipboard ())
{
Return false;
}
If (! Emptyclipboard ())
{
Closeclipboard ();
Return false;
}
Hglobal hclipbuffer = globalalloc (gmem_ddeshare, strhtmlclip. getlength () + 1 );
If (hclipbuffer = NULL)
{
Closeclipboard ();
Return false;
}
Char * lpexpbuffer = (char *) globallock (hclipbuffer );
If (lpexpbuffer = NULL)
{
Globalfree (hclipbuffer );
Closeclipboard ();
Return false;
}
Memcpy (lpexpbuffer, strhtmlclip, strhtmlclip. getlength ());
Lpexpbuffer [strhtmlclip. getlength ()] = '\ 0 ';
Globalunlock (hclipbuffer );
Setclipboarddata (CFID, hclipbuffer );
Closeclipboard ();
Return true;
}
Call method:C ++ Language: Void cbasedialogdlg: onbnclickedbutton1 ()
{
Cstring STR = _ T ("<B> This is a test. This is a test. </B> <HR> <li> Entry 1 <li> entry 2 ");
Copyhtmltoclip (STR );
}
You can use a clipboard viewer to view the content of the clipboard at this time:
Then, press Ctrl + V to paste it into the blog editor. The effect is as follows:
This is a test. This is a test.
Entry 1
Entry 2References:
1. html clipboard format http://msdn.microsoft.com/en-us/library/aa767917.aspx
2. How to add an HTML code http://support.microsoft.com/kb/274308 to the clipboard using Visual C ++