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.
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.
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??
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; }
Hmmm, it's giving me this error:
I'm using XP and MSVC++ if that helps ...Code:error C2601: 'SetClipboardText' : local function definitions are illegal
and it points to the opening brace for "BOOL SetClipboardText(LPCTSTR pszText) {"
Are you trying to embed my function in a different function? Also make sure you include windows.h and tchar.h
Also make sure all your open braces have a matching closing brace.
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 )
with the headers. So does SetClipboardText copy to the clipboard "thing"? if so, is there an alternative function to paste it?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; }
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.
yesSo does SetClipboardText copy to the clipboard "thing"?
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; }
Sorry, but what parameters does this take??Code:BOOL GetClipboardText(LPTSTR buff, UINT iBuffSize)
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
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.LPTSTR, is IIRC, Long Pointer To STRing
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