Thread: ClipBoard ...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273

    ClipBoard ...

    Is there a way to copy a string onto the clipboard for paste-ing?? also is there a way to paste it?

    I did a quick google search of the FAQ and didn't find anything.

  2. #2

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Cool! Can you do that for strings too?? Like I mean can I select a string and directly copy that to the clipboard, or does it have to be selected??

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Here's the code that I use:
    Code:
    BOOL SetClipboardText(LPCTSTR pszText)
    {
    	HGLOBAL			hGlobal;
    	int				nBytes;
    	LPTSTR			pClipboardData;
    	unsigned int	format;
    
    #ifdef UNICODE
    	format = CF_UNICODETEXT;
    #else
    	format = CF_TEXT;
    #endif
    
    	if(!OpenClipboard(NULL))
    		return FALSE;
    
    	EmptyClipboard();
    
    	nBytes = (_tcslen(pszText) + 1) * sizeof(TCHAR);
    
    	hGlobal = GlobalAlloc(GMEM_MOVEABLE,nBytes);
    	if(!hGlobal)
    	{
    		CloseClipboard();
    		return FALSE;
    	}
    
    	pClipboardData = GlobalLock(hGlobal);
    	if(!pClipboardData)
    	{
    		CloseClipboard();
    		return FALSE;
    	}
    
    	memcpy(pClipboardData,pszText,nBytes);
    
    	GlobalUnlock(hGlobal);
    
    	SetClipboardData(format,hGlobal);
    
    	CloseClipboard();
    
    	return TRUE;
    }

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Hmmm, it's giving me this error:

    Code:
    error C2601: 'SetClipboardText' : local function definitions are illegal
    I'm using XP and MSVC++ if that helps ...

    and it points to the opening brace for "BOOL SetClipboardText(LPCTSTR pszText) {"

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Are you trying to embed my function in a different function? Also make sure you include windows.h and tchar.h

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Also make sure all your open braces have a matching closing brace.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Whoops, I pasted it inside the main brace by accident ... I'm still kind of confused though. How do you use it??


    I have (mainly thanks to you )

    Code:
    BOOL SetClipboardText(LPCTSTR pszText)
    {
    	HGLOBAL			hGlobal;
    	int				nBytes;
    	LPTSTR			pClipboardData;
    	unsigned int	format;
    
    #ifdef UNICODE
    	format = CF_UNICODETEXT;
    #else
    	format = CF_TEXT;
    #endif
    
    	if(!OpenClipboard(NULL))
    		return FALSE;
    
    	EmptyClipboard();
    
    	nBytes = (_tcslen(pszText) + 1) * sizeof(TCHAR);
    
    	hGlobal = GlobalAlloc(GMEM_MOVEABLE,nBytes);
    	if(!hGlobal)
    	{
    		CloseClipboard();
    		return FALSE;
    	}
    
    	pClipboardData =(char*) GlobalLock(hGlobal);
    	if(!pClipboardData)
    	{
    		CloseClipboard();
    		return FALSE;
    	}
    
    	memcpy(pClipboardData,pszText,nBytes);
    
    	GlobalUnlock(hGlobal);
    
    	SetClipboardData(format,hGlobal);
    
    	CloseClipboard();
    
    	return TRUE;
    }
    
    int main()
    {
    	char thing[6]="thing";
    	SetClipboardText(thing);
    	return 0;
    }
    with the headers. So does SetClipboardText copy to the clipboard "thing"? if so, is there an alternative function to paste it?

    EDIT:

    PS - It rocks the way it copies the string to the clipboard, and it stays there after the program has finished ;P cool!! is there a way to paste it from the program??
    Last edited by twomers; 01-12-2006 at 02:28 PM.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    So does SetClipboardText copy to the clipboard "thing"?
    yes

    if so, is there an alternative function to paste it?
    Code:
    BOOL GetClipboardText(LPTSTR buff, UINT iBuffSize)
    {
    	HANDLE hClip;
    	TCHAR* pszClipboard;
    	unsigned int	format;
    
    #ifdef UNICODE
    	format = CF_UNICODETEXT;
    #else
    	format = CF_TEXT;
    #endif
    
    	if(OpenClipboard(NULL))
    	{
    		hClip = GetClipboardData(format);
    		if(hClip == NULL)
    		{
    			CloseClipboard();
    			return FALSE;
    		}
    		pszClipboard = (TCHAR*)GlobalLock(hClip);
    		if(!pszClipboard)
    		{
    			GlobalUnlock(hClip);
    			return FALSE;
    		}
    		_tcsncpy(buff,pszClipboard,iBuffSize);	
    		buff[iBuffSize-1] = 0;
    		GlobalUnlock(hClip);	
    		CloseClipboard();	
    	}
    	else
    	{
    		return FALSE;
    	}
    
    	return TRUE;
    }

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    BOOL GetClipboardText(LPTSTR buff, UINT iBuffSize)
    Sorry, but what parameters does this take??

  11. #11
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    buff is a pointer to a buffer of type char in to which to store the clipboard contents (LPTSTR, is IIRC, Long Pointer To STRing), and iBuffSize is the size (number of bytes) of the buffer pointed to by buff.

    This is not really a C question; it's a Windows programming question.
    Insert obnoxious but pithy remark here

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    LPTSTR, is IIRC, Long Pointer To STRing
    Well, not really. There is no such thing as long pointers anymore since 32 bit windows came out. A long pointer is the same as a regular pointer. LPTSTR is defined as a pointer to a TCHAR. A TCHAR is defined as either a char, or a wchar_t depending on whether unicode is defined. iBuffSize is not the number of bytes, but rather the number of TCHARs that the buffer could hold.

  13. #13
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    I guess I didn't recall correctly, then. It's been a while since I programmed in the Windows environment. I stand corrected.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying to clipboard
    By bballzone in forum Windows Programming
    Replies: 24
    Last Post: 09-30-2004, 03:24 PM
  2. Clipboard and Custom Types
    By McClamm in forum C# Programming
    Replies: 1
    Last Post: 09-16-2004, 04:43 PM
  3. Clipboard Modifier
    By Korhedron in forum Windows Programming
    Replies: 2
    Last Post: 01-03-2004, 02:32 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM