VC + + Shear Board simple operation

Source: Internet
Author: User

It is fairly straightforward to see in the forums how some users are asking how to implement the Clipboard in Visual C + + in VC++/MFC. This article mainly introduces the following content: 1, the operation of the text content 2, the operation of the WMF data 3, the operation of the bitmap 4, the settings using custom format 5, the sense of the Clipboard content changes 6, automatically paste data into another application window
I. Operation of text content The following code demonstrates how to copy text content to the Clipboard:
Cstring source; The text content is saved in the source variable if (OpenClipboard ()) {hglobal clipbuffer; char * buffer; EmptyClipboard (); Clipbuffer = GlobalAlloc (Gmem_ddeshare, Dource. GetLength () +1); Buffer = (char*) globallock (Clipbuffer); Strcpy (buffer, LPCSTR (source)); GlobalUnlock (Clipbuffer); SetClipboardData (Cf_text,clipbuffer); CloseClipboard (); }
The following code shows how to get the text content from the Clipboard:
char * buffer = NULL; Open the Clipboard CString Fromclipboard; if (OpenClipboard ()) {HANDLE Hdata = GetClipboardData (cf_text); char * buffer = (char*) globallock (hdata); Fromclipboard = buffer; GlobalUnlock (Hdata); CloseClipboard (); }
Ii. operation of WMF data
Reading and writing image data on the Clipboard is a very useful feature and is easy to implement. The following code shows how to copy an extended metafile to the clipboard:
if (OpenClipboard ()); {EmptyClipboard ();
Creating metafile DC CMetaFileDC * CDC = new CMetaFileDC (); Cdc->createenhanced (GetDC (), Null,null, "the_name");
Calling drawing routines
Close the CMetaFileDC and get its handle henhmetafile handle = cdc->closeenhanced ();
Copy to Clipboard SetClipboardData (Cf_enhmetafile,handle); CloseClipboard ();
Delete the DC delete CDC; }
The following code demonstrates getting a metafile from the Clipboard and drawing it onto the client DC:
if (OpenClipboard ()) {//Get clipboard data henmetafile handle = (henmetafile) getclipboarddata (cf_enhmetafile);
Display CCLIENTDC DC (this); CRect client (0,0,200,200); dc. PlayMetaFile (handle,client);
Close clipboard CloseClipboard (); } third, the operation of the bitmap
The operation of the bitmap is slightly more complicated, and the following example shows how to save the bitmap on the Clipboard:
if (OpenClipboard ()) {EmptyClipboard (); CBITMAP * junk = new CBitmap (); CClientDC CDC (this); CDC DC; dc. CreateCompatibleDC (&CDC); CRect client (0,0,200,200); Junk->createcompatiblebitmap (&cdc,client. Width (), client. Height ()); dc. SelectObject (junk);
DrawImage (&dc,cstring ("Bitmap"));
Copy data to clipboard SetClipboardData (Cf_bitmap,junk->m_hobject); CloseClipboard ();
Delete junk; }
The following code shows how to get the bitmap data from the Clipboard:
if (OpenClipboard ()) {//Get clipboard data hbitmap handle = (HBITMAP) getclipboarddata (CF_BITMAP); CBitmap * BM = cbitmap::fromhandle (handle);
CClientDC CDC (this); CDC DC; dc. CreateCompatibleDC (&CDC); dc. SelectObject (BM); Cdc. BitBlt (0,0,200,200,&dc,0,0,srccopy);
CloseClipboard (); }
Iv. setting up and using custom formats
Using the RegisterClipboardFormat () function, you can copy and paste any data type you want. For example, we have one of the following data types:
struct Myformatdata {long val1; int val2;};
To copy it to the Clipboard, we can use the following code:
UINT format = RegisterClipboardFormat ("My_custom_format"); if (OpenClipboard ()) {myformatdata data; data.val1 = +; data.val2 = 200;
Hglobal Clipbuffer; EmptyClipboard (); Clipbuffer = GlobalAlloc (gmem_ddeshare, sizeof (Myformatdata)); Myformatdata * buffer = (myformatdata*) globallock (Clipbuffer);
Save to memory *buffer = data;
Save to Clipboard GlobalUnlock (Clipbuffer); SetClipboardData (Format,clipbuffer); CloseClipboard (); }
Read the data using the following code:
UINT format = RegisterClipboardFormat ("My_custom_format"); Myformatdata data; if (OpenClipboard ()) {HANDLE hdata =getclipboarddata (format); Myformatdata * buffer = (myformatdata*) globallock (hdata);
data = *buffer;
GlobalUnlock (Hdata); CloseClipboard (); }
V. Perception of clipboard content changes
With Windows messages, you can sense whether the contents of the Clipboard have changed and the code is as follows:
In your initialization code call:setclipboardviewer (); Add our program to the Clipboard Watch chain
In your message map add:on_message (Wm_drawclipboard, Onclipchange)//Add message handle
Which is declared as:afx_msg void Onclipchange ();
Finally implement:void Cdetectclipboardchangedlg::onclipchange () {CTime time = Ctime::getcurrenttime (); Setdlgitemtext (Idc_changed_date,time. Format ("%a,%b%d,%Y-%h:%m:%s"));
Displayclipboardtext (); }
Vi. pasting data into another application window automatically
Just get the handle to the corresponding window and send a message to it:
SendMessage (M_htextwnd, wm_paste, 0, 0);
to put it simply:

First, open the shear plate

OpenClipboard ();

Second, get the global pointer variable pointing to the shear plate

HANDLE Hclipmemory=::getclipboarddata (CF_TEXT);

Three, locking shear plate

Lpbyte lpclipmemory = (lpbyte) globallock (hclipmemory);

Iv. Getting the Clipboard contents

CString m_smessage = CString (lpclipmemory);

Five, unlock the shear plate

GlobalUnlock (hclipmemory);

Six, close the shear plate

:: CloseClipboard ();

Reading the Clipboard contents must first lock the Clipboard, otherwise an exception will occur. The above can only read text-formatted content and cannot read bitmaps, please consult the MSDN Help documentation.

VC + + Shear Board simple operation

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.