Copy HTML format to clipboard HTML clipboard format by VC

Source: Internet
Author: User

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 2

    References:
    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 ++

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.