Read/Write text on clipboard
Posted
by Randy More
on August 6th, 1998
CString source; //put your text in source if(OpenClipboard()) { HGLOBAL clipbuffer; char * buffer; EmptyClipboard(); clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1); buffer = (char*)GlobalLock(clipbuffer); strcpy(buffer, LPCSTR(source)); GlobalUnlock(clipbuffer); SetClipboardData(CF_TEXT,clipbuffer); CloseClipboard(); }How to get text off of the clipboard
This is easy really but here it is for completenesschar * buffer; if(OpenClipboard()) { buffer = (char*)GetClipboardData(CF_TEXT); //do something with buffer here //before it goes out of scope } CloseClipboard();

Comments
OpenClipboard
Posted by DevWin32 on 12/13/2006 03:43pmHi: First thank you for this article which had hepled me to learn how to deal with clipboard. I've a remark, the OpenClipboard function should take an argument (HWND) which it is not the case here.
Replyget data from applet window
Posted by Legacy on 04/17/2003 12:00amOriginally posted by: erhan
How can I get data from i.e explorer applet window..
input box element or list element text area.
thanks.
-
ReplyGet data from applet
Posted by Tscheulin on 10/16/2008 07:35amDear erhan, have you got an answer ? I am very interested about it. Thanks, Hansruedi Tscheulin tscheulin@bluewin.ch
ReplyModifying F2, F3 and F4 to work as Cut, Copy & Paste
Posted by Legacy on 04/01/2003 12:00amOriginally posted by: Timothy C. Alvord
I am looking for help in developing a SysTray app that will catch the pressing of the three Function keys F2, F3 and F4 and perform the same functions as CTRL-X, CTRL-C and CTRL-V. Any help would be greatly appreciated.
Thanks,
ReplyTim
How to paste from clipboard 2 bmp file?
Posted by Legacy on 03/27/2003 12:00amOriginally posted by: Sikachev Peter
If you have some ideas send'em 2 my mail, please.
Replymail_to: garret-lit@yandex.ru
Access Violation
Posted by Legacy on 01/15/2003 12:00amOriginally posted by: Radu
Hi,
1) At first sight the code worked fine. However after some copy operations I got an access violation on the line:
buffer = (char*)GlobalLock(clipbuffer);
2) Something else seems to be wrong here. The "buffer" is initialized with some crap in it. I had a "," at the beginning of the buffer and in some cases also some other characters.
Any idea why?
Thanks for your effort.
Replywhats the format used for ActiveX Control
Posted by Legacy on 10/17/2001 12:00amOriginally posted by: mercy
hi all,
ihave a ActiveX control in the clipboard.what is the Format that i should use in the fn GetClipboardData(UINT uFormat) to get that control.
thanks
Replymercy
Does not work in Windows CE
Posted by Legacy on 06/28/2001 12:00amOriginally posted by: YY
I tried this in Windows CE 3.0 with embdedded visual tool 3.0 and it does not seem to be putting stuff on the clipboard:
char source[100] = "Try This one";
Replyif ( OpenClipboard (NULL) )
{
HLOCAL clipbuffer;
char * buffer;
EmptyClipboard ();
clipbuffer = LocalAlloc (0, sizeof(source) + 1);
buffer = (char*) LocalLock (clipbuffer);
strcpy (buffer, LPSTR (source) );
LocalUnlock (clipbuffer);
SetClipboardData (CF_TEXT, clipbuffer);
CloseClipboard ();
}
return (0);
Checking the clipboard format before copying the text data
Posted by Legacy on 05/14/2001 12:00amOriginally posted by: S.K.Pradhan
Thanx a lot for this posting. Given below is the code which will check for TEXT data in clipboard before copying to the destination string.
UINT cbFormat=0;
bool bTextFormat=0;
char *myDestStr;
if(OpenClipboard())
{
while(cbFormat=EnumClipboardFormats(cbFormat))
{
if (cbFormat==CF_TEXT) bTextFormat=1;
}
if(bTextFormat)
{
myDestStr=(char*)GetClipboardData(CF_TEXT);
AfxMessageBox(myDestStr);
}
else AfxMessageBox("NO Text data in clipboard");
}
else AfxMessageBox("Could not open clipboard, some other task might be in use.");
Regards
ReplySKP
Binary data in the clipboard.
Posted by Legacy on 04/15/2001 12:00amOriginally posted by: Michel Bouchet
Here is the way I solved the problem (mentioned by myself in the previous article) :
1) I used RegisterClipboardFormat to create my own format, since none of the standard (CF_TEXT ...) seems to correspond to my needs :
UINT CF_BINFormat;
CF_BINFormat=RegisterClipboardFormat("CF_MYBINFORMAT");
.....
2) then I changed :
SetClipboardData(CF_TEXT,....
to become :
SetClipboardData(CF_BINFormat,......
and :
buffer = (char*)GetClipboardData(CF_TEXT);
to become :
buffer =(unsigned char*)GetClipboardData(CF_BINFormat);
Then I basically followed the example using memcpy() instead of strcpy() since my data are not necessarily NULL terminated.
If somebody thinks this is not the proper way to deal with the clipboard. Please let me know.
ReplyClipboard (Set & Get Data)
Posted by Legacy on 04/02/2001 12:00amOriginally posted by: Michel Bouchet
Thanks for posting this example.
It helped me to understand the basic way to use a clipboard in an application.
But what if I need to store arbitrary data in the clipboard. I mean not only text but any string of bytes which might also of course include 0x00 or any other values.
Thanks for any clue.
ReplyLoading, Please Wait ...