Windows API OpenClipboard---Clipboard

Source: Internet
Author: User

Translated from http://www.cnblogs.com/wind-net/archive/2012/11/01/2749558.html

Clipboard: A global public memory area maintained by the system. Only one process is allowed to access it at a time.

The clipboard operation is as follows: (MSDN search Clipboard Operations)

1. Open the shear plate Bool OpenClipboard (HWND hwndnewowner); Specifies the window handle associated to the open clipboard, passing in null to associate to the current task. Only one process is allowed to open and access at a time.

Shut down every time you open or else the Clipboard will not be accessible to other processes.

2. Empty the Clipboard Bool emptyclipboard (void)

Must be emptied before writing to get the Clipboard possession

3. Allocating memory hglobal GlobalAlloc (UINT uflags, size_t dwbytes); Dynamically allocates memory regions in bytes on the heap. Success points to the memory, failing null. Parameters: 1. Allocating memory properties, 2. Allocated Size

4. Lock Memory lpvoid GlobalLock (hglobal hmem); Locks the memory allocated by GlobalAlloc and locks counter +1 on the memory object, successfully returning a pointer to the start address of the memory object. Failed null

The system maintains a lock counter for each global memory object, initially 0,globallock the counter +1,globalunlock counter-1. Once the counter value is greater than 0,

This area of memory will not be allowed to be moved or deleted, but only if it is 0 o'clock, the lock on this block of memory is unlocked. If the Gmem_fixed property is assigned, the counter has been 0

5. Set the shear plate HANDLE setclipboarddata (UINT uformat, HANDLE hmem);

Execution succeeds, returns a data handle, otherwise returns null

6. Unlock the BOOL globalunlock (hglobal hmem); The GlobalAlloc is assigned a property of Gmem_moveable memory Object counter -1.

7. Turn off the shear plate Bool closeclipboard (void);

You must close the Clipboard other processes to use the Clipboard, and the current process cannot write to the data after it is closed.

8. Obtain the Clipboard data HANDLE getclipboarddata (UINT uformat);

Execution succeeds, returns a handle to the data handle, otherwise returns a null data format, specifying the format of the data handle

One: UINT uformate format Description: standard ClipBook data format

Windows supports different pre-defined ClipBook formats, which are defined in WINUSER.H as identifiers prefixed with CF.

Three kinds of text data types that can be stored on a scrapbook:

①cf_text A null-terminated ANSI character set string. It contains a carriage return and linefeed character at the end of each line, which is the simplest form of ClipBook data.

②Cf_oemtext A block of memory containing literal data (similar to cf_text). However, it is using the OEM character set.

③Cf_unicodetext A block of memory containing Unicode text. Similar to Cf_text, it contains a carriage return and linefeed character at the end of each line, and a null character (two 0 bytes) to indicate the end of the data. Cf_unicodetext only supports Windows NT.

Two additional ClipBook formats, but they do not need to be null-terminated, because the format already defines the end of the data.

①cf_sylk contains microsoft"symbolic link" data format of the whole memory block. This format is used to exchange data between Microsoft's Multiplan, chart, and Excel programs, which is an ASCII format.

②Cf_dif a monolithic block of memory that contains data Interchange Format (DIF) data. Used to send data to the VISICALC spreadsheet program. This is also an ASCII code format

The following three types of ClipBook formats are related to bitmaps. The so-called bitmap is a rectangular array of data bits

①Cf_bitmap The device-related bitmap format. Bitmaps are routed to the ClipBook through a bitmap handle.

②Cf_dib defines a block of memory for a device-independent bitmap.

③Cf_palette color palette handle.

Here are two metafile formats, metafile is a set of drawing commands stored in binary format

①cf_metafilepict A "picture" stored in the old metafile format.

②Cf_enhmetafile Enhanced Metafile (32-bit Windows supported) handle.

Finally, several mixed-style scrapbook formats are introduced:

Cf_pendata is used in conjunction with the PEN Input expansion feature of Windows.

Cf_wave Sound (waveform) file.

Cf_riff multimedia data using the Resource Interchange File format (Resource interchange, format).

Cf_hdrop a list of files related to drag-and-drop services.

Two: UINT uflags format Description: Memory properties

Gmem_fixed

Allocates a fixed area of memory that does not allow the system to move, when the return value is a pointer.

Gmem_moveable

Allocating a piece of removable memory area, in fact the memory block in physical memory is not removable, where the movable refers to the application's default logical heap can be moved. The return value is a handle to the memory object. You can convert a handle to a pointer by investigating the GlobalLock () function, which cannot be drunk gmem_fixed while using

Gmem_zeroint

Initializes the memory object to full 0, and if this flag is not used, the memory object will be an indeterminate content

Ghnd

Gmem_moveable and Gmem_zeroint block flags are used together to move and initialize to 0

Gptr

Gmem_fixed and gmem_zeroint flags are used together, i.e. cannot be moved and initialized to 0

1. Save the data to the Clipboard

voidCmfc_tabctrldlg::setclipboarddata_ (CString strText) {/*OpenClipboard Open Clipboard: Specifies the window handle associated to the open clipboard, and passing in NULL indicates that the current task is associated. Only one process is allowed to open and access at a time.    Shut down every time you open or else the Clipboard will not be accessible to other processes. EmptyClipboard emptying the Clipboard: must be emptied before writing to obtain possession*/    if(:: OpenClipboard (m_hwnd) &&:: EmptyClipboard ()) {        //get data length based on environment variablessize_t cbstr = (strtext.getlength () +1) *sizeof(TCHAR); //dynamically allocates a global memory region in bytes on the heap. Success points to the memory, failing null. Parameters: 1. Allocating memory properties, 2. SizeHglobal Hmem =GlobalAlloc (gmem_moveable, CBSTR); if(Hmem = =NULL) {            //closes the clipboard, releasing the Clipboard ownership, and cannot write data after closingCloseClipboard (); return; }        //locks the memory allocated by GlobalAlloc and locks the memory object by +1; Successfully returns a pointer to the start address of the memory object. Failed nullLPTSTR lpDest =(LPTSTR) GlobalLock (HMEM); /*The system maintains a lock counter for each global memory object, starting with 0,globallock to make counter +1,*/        //copy data to clipboard memory. memcpy_s (LpDest, Cbstr, Strtext.lockbuffer (), CBSTR);        Strtext.unlockbuffer (); //To unlock the memory, set the property to Gmem_moveable Memory Object counter -1.GlobalUnlock (HMEM); /*GlobalUnlock Counter-1. Once the counter value is greater than 0, the area of memory is not allowed to be moved or deleted, and the lock on this block of memory is unlocked only if it is 0 o'clock. If the Gmem_fixed property is assigned, the counter has been 0*/        //formatting data based on environment variablesUINT UIFormat = (sizeof(TCHAR) = =sizeof(WCHAR))?Cf_unicodetext:cf_text; //sets the data to the Clipboard. Execution succeeds, returns a data handle, otherwise returns null        if(SetClipboardData (UIFormat, hmem) = =NULL);            {CloseClipboard (); return;    } closeclipboard (); }}

2. Get data from clipboard memory

voidCmfc_tabctrldlg::getclipboarddata_ (void){    //if (isclipboardformatavailable (cf_unicodetext))//determine if data in a format is available    if(:: OpenClipboard (M_hwnd)) {UINT UIFormat= (sizeof(TCHAR) = =sizeof(WCHAR))?Cf_unicodetext:cf_text; ////Execution succeeds, returns a data handle, otherwise returns NULL. Parameters: 1. Data format, 2. Handle of data in specified formatHglobal Hmem =GetClipboardData (UIFormat); if(Hmem! =NULL) {             //gets the Unicode string. LPCTSTR lpStr =(LPCTSTR) GlobalLock (HMEM); if(LpStr! =NULL)            {Setdlgitemtext (idc_edit1, LPSTR);        } globalunlock (HMEM); }} closeclipboard ();}

Windows API OpenClipboard---clipboard (RPM)

Related Article

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.