Read/Write text on clipboard


	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 completeness
	char * 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:43pm

    Hi: 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.

    Reply
  • get data from applet window

    Posted by Legacy on 04/17/2003 12:00am

    Originally posted by: erhan


    How can I get data from i.e explorer applet window..
    input box element or list element text area.

    thanks.

    • Get data from applet

      Posted by Tscheulin on 10/16/2008 07:35am

      Dear erhan, have you got an answer ? I am very interested about it. Thanks, Hansruedi Tscheulin tscheulin@bluewin.ch

      Reply
    Reply
  • Modifying F2, F3 and F4 to work as Cut, Copy & Paste

    Posted by Legacy on 04/01/2003 12:00am

    Originally 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,
    Tim

    Reply
  • How to paste from clipboard 2 bmp file?

    Posted by Legacy on 03/27/2003 12:00am

    Originally posted by: Sikachev Peter

    If you have some ideas send'em 2 my mail, please.
    mail_to: garret-lit@yandex.ru

    Reply
  • Access Violation

    Posted by Legacy on 01/15/2003 12:00am

    Originally 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.

    Reply
  • whats the format used for ActiveX Control

    Posted by Legacy on 10/17/2001 12:00am

    Originally 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
    mercy

    Reply
  • Does not work in Windows CE

    Posted by Legacy on 06/28/2001 12:00am

    Originally 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";
    if ( 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);

    Reply
  • Checking the clipboard format before copying the text data

    Posted by Legacy on 05/14/2001 12:00am

    Originally 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
    SKP

    Reply
  • Binary data in the clipboard.

    Posted by Legacy on 04/15/2001 12:00am

    Originally 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.

    Reply
  • Clipboard (Set & Get Data)

    Posted by Legacy on 04/02/2001 12:00am

    Originally 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.

    Reply
  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds