MFC clipboard Application
How to copy data to a clipboard:
1: Open the clipboard ()
2: emptyclipboard () clears the clipboard so that the current process has a clipboard
3: globalalloc () allocates global heap memory.
4: globallock () locks the memory and returns the first address of the memory.
5: strcpy () assigns a value to the memory.
6: globalunlock () Unlock memory
7: setclipboarddata () is set to clipboard
8: closeclipboard () Close the clipboard
<textarea cols="50" rows="15" name="code" class="cpp">Void cclipboarddlg: onbnclickedbutton2 () <br/>{< br/> If (openclipboard () // open the clipboard <br/>{< br/> emptyclipboard (); // clear the clipboard so that the current window process has a clipboard <br/> cstring STR; <br/> getdlgitemtext (idc_edit_send, STR); // obtain data <br/> handle hclip; <br/> hclip = globalalloc (gmem_moveable, str. getlength () + 1); // address returned by memory allocation object (this function allocates memory from the global heap) <br/> char * pbuf; <br/> pbuf = (char *) globallock (hclip); // lock the memory block specified in the global memory and return an address value, point it to the starting position of the memory block <br/> strcpy (pbuf, STR); // copy the data in the STR object to the memory space <br/> globalunlock (hclip ); // unlock the global memory block <br/> setclipboarddata (cf_text, hclip); // set the Clipboard data <br/> closeclipboard (); // close <br/>}< br/>}</textarea>
Void cclipboarddlg: onbnclickedbutton1 () <br/>{< br/> // todo: add the <SPAN class = 'wp _ keywordlink '> Program </span> <SPAN class = 'wp _ keywordlink'> Code </span> <br/> If (isclipboardformatavailable (cf_text )) // obtain whether the format in the clipboard can be processed <br/>{< br/> If (openclipboard () <br/>{< br/> handle hclip; <br/> char * pbuf; <br/> hclip = getclipboarddata (cf_text); // retrieves Clipboard data from the specified format, return a clipboard object handle <br/> pbuf = (char *) globallock (hclip); // lock the memory block specified in the global memory and return an address value, return the Data Pointer header address <br/> globalunlock (hclip); // unlock <br/> setdlgitemtext (idc_edit_recv, pbuf); <br/> closeclipboard (); <br/>}< br/>}
From: http://blog.sina.com.cn/s/blog_613bf01c0100h0o4.html