Windows clipboard operation Simple Small example

Source: Internet
Author: User

1. Copy Text to Clipboard

CString StrText = L "text to be copied to clipboard", if (:: OpenClipboard (m_hwnd)) {if (:: EmptyClipboard ())//empty clipboard data {int nSize = strtext.ge Tlength () *2+1;//Here is the number of bytes, Unicode encoding each character occupies 2 bytes, followed by an end character. Hglobal Hmem =:: GlobalAlloc (GHND, nSize); byte* pdata= (byte*):: GlobalLock (HMEM); memcpy_s (PData, NSize, Strdata.getdata (), nSize-1);pD ata[nsize-1]= ';:: GlobalUnlock (HMEM);:: SetClipboardData (Cf_unicodetext, Hmem);:: CloseClipboard ();:: GlobalFree (HMEM);}}

2. Copy the bitmap handle to the Clipboard

Save screenshot Area bitmap to Clipboard if (:: OpenClipboard (m_hwnd)) {if (:: EmptyClipboard ()) {Hbitmap hbmp = Getbitmaprect (G_hscreenbmp, M_rect );:: SetClipboardData (Cf_bitmap, hbmp);D eleteobject (hbmp);:: CloseClipboard ();}}
3. In general, you must follow this order to manipulate the Clipboard: Open----Empty----setdata-----close. (Open-empty-set-off)

A number of other specific references are available for the MSDN instructions

Remarksopenclipboard fails if another window has the the Clipboard Open. An application should call the CloseClipboard function after every successful call to OpenClipboard.  The window identified by the Hwndnewowner parameter does not become the Clipboard owner unless the EmptyClipboard function is called. If an application calls OpenClipboard with HWND set to NULL, EmptyClipboard sets the clipboard owner to null; This causes SetClipboardData to fail.



Windows clipboard Operations Simple small example

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.