For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!
Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard
by Chuck » Thu Aug 05, 2004 2:44 pm
i can simulate the computer highlighting, right-clicking, and choosing copy over some text but how do i assign that contents of the clipboard to a variable in the program? this is probably a windows question but it probably deserves to be in the beginners forum.
-
Chuck
-
- Posts: 50
- Joined: Thu Jun 24, 2004 7:11 pm
by Siver (Not logged in) » Thu Aug 05, 2004 2:59 pm
I'm probably far off but what about this?
- Code: Select all
#include <windows.h>
void Paste( );
You must call that function to paste but you must have something inserted into the clipboard.
Hope it works.
-
Siver (Not logged in)
-
by Siver » Thu Aug 05, 2004 3:00 pm
Siver (Not logged in) wrote:I'm probably far off but what about this?
- Code: Select all
#include <windows.h>
void Paste( );
You must call that function to paste but you must have something inserted into the clipboard.
Hope it works.
Continuing with that. Open a text file, use that function to paste it into a text file then read in whatever was copied and store it in a string.
Siver
-
Siver
-
- Posts: 144
- Joined: Thu Mar 04, 2004 8:28 pm
by Chuck » Thu Aug 05, 2004 5:31 pm
(another reason why i put this in beginners forum)
i'm calling the function but having troubles actually using it. just void Paste() does little.
-
Chuck
-
- Posts: 50
- Joined: Thu Jun 24, 2004 7:11 pm
by Siver » Thu Aug 05, 2004 5:50 pm
- Code: Select all
// The pointer to my edit.
extern CEdit* pmyEdit;
// Replace all of the text with the text in the clipboard.
pmyEdit->SetSel(0, -1);
pmyEdit->Paste();
Paste the text into a text file, read the text into a string and wala you will now have the clipboard in whichever string variable you desire. That should work! The function SetSel will have it start at the index of the clipboard.
-
Siver
-
- Posts: 144
- Joined: Thu Mar 04, 2004 8:28 pm
by Chuck » Thu Aug 05, 2004 8:28 pm
many errors
i know that's from MSDN cuz i saw it there

but examples from that site never usually work for me.
-
Chuck
-
- Posts: 50
- Joined: Thu Jun 24, 2004 7:11 pm
by Dante Shamest » Thu Aug 05, 2004 8:56 pm
[..edit..] Someone should really make code text appear in a monospaced font.
- Code: Select all
#include <stdio.h>
#include <windows.h>
int GetClipboardSize()
{
OpenClipboard(NULL) ; // Obtain clipboard
HGLOBAL handle = GetClipboardData (CF_TEXT) ; // get handle to data
CloseClipboard() ; // Close clipboard
return GlobalSize( handle ) ; // return size of data
}
void GetClipboardText( TCHAR * buffer )
{
OpenClipboard(NULL) ; // Obtain clipboard
HGLOBAL handle = GetClipboardData (CF_TEXT) ; // get handle to data
char* szClipboard = (char*)GlobalLock(handle); // lock data
lstrcpy( buffer, szClipboard ) ; // copy clipboard text
GlobalUnlock (handle) ; // unlock data
CloseClipboard () ; // close data
}
int main()
{
// Allocate enough memory to save text in clipboard.
char * szClipboardText = new char[ GetClipboardSize() + 1 ] ;
// Get the clipboard text.
GetClipboardText( szClipboardText ) ;
// Print clipboard text.
printf( "Clipboard Text: \"%s\"", szClipboardText );
// Delete the memory.
delete[] szClipboardText ;
getchar();
}
Last edited by
Dante Shamest on Thu Aug 05, 2004 9:42 pm, edited 1 time in total.
-

Dante Shamest
- Moderator
-
- Posts: 3131
- Joined: Wed Oct 22, 2003 10:29 pm
- Location: Malaysia
-
by Chuck » Thu Aug 05, 2004 9:39 pm
thx a lot!

works great
-
Chuck
-
- Posts: 50
- Joined: Thu Jun 24, 2004 7:11 pm
Return to For Beginners
Who is online
Users browsing this forum: No registered users and 0 guests