Re: Unable to use SetClipboardData.
<shrikant.gurav11@gmail.com> ha scritto nel messaggio
news:cef1969e-54b2-4c51-9dfe-1b421f077fa9@p36g2000prp.googlegroups.com...
i want to cpture the cilpboard data, and modify it then want to set
it into Ciplboard back.How can i do?
You may want to develop a couple of methods, one for retrieving text from
the clipboard, and the other one to copy the text to clipboard.
So, your worflow can be:
1. call the method to get clipboard text data
2. modify the text
3. call the method to copy the new text to the clipboard
You might find useful a simple MFC application I developed to test these
concepts here:
http://www.geocities.com/giovanni.dicanio/vc/MfcClipboard.zip
The relevant code for methods implementing copy and paste operations
follows:
<code>
//////////////////////////////////////////////////////////////////////////
// Copies input string to clipboard.
// Returns TRUE on success, FALSE on error.
//////////////////////////////////////////////////////////////////////////
BOOL CMfcClipboardDlg::CopyStringToClipboard( IN const CString & str )
{
// Open the clipboard
if ( !OpenClipboard() )
return FALSE;
// Empty the clipboard
if ( !EmptyClipboard() )
{
CloseClipboard();
return FALSE;
}
// Number of bytes to copy (consider +1 for end-of-string, and
// properly scale byte size to sizeof(TCHAR))
SIZE_T textCopySize = (str.GetLength() + 1) * sizeof(TCHAR);
// Allocate a global memory object for the text
HGLOBAL hTextCopy = GlobalAlloc( GMEM_MOVEABLE, textCopySize );
if ( hTextCopy == NULL )
{
CloseClipboard();
return FALSE;
}
// Lock the handle, and copy source text to the buffer
TCHAR * textCopy = reinterpret_cast< TCHAR *>( GlobalLock(
hTextCopy ) );
ASSERT( textCopy != NULL );
StringCbCopy( textCopy, textCopySize, str.GetString() );
GlobalUnlock( hTextCopy );
textCopy = NULL; // avoid dangling references
// Place the handle on the clipboard
#if defined( _UNICODE )
UINT textFormat = CF_UNICODETEXT; // Unicode text
#else
UINT textFormat = CF_TEXT; // ANSI text
#endif // defined( _UNICODE )
if ( SetClipboardData( textFormat, hTextCopy ) == NULL )
{
// Failed
CloseClipboard();
return FALSE;
}
// Release the clipboard
CloseClipboard();
// All right
return TRUE;
}
//////////////////////////////////////////////////////////////////////////
// Retrieves clipboard text content.
// Returns TRUE on success, FALSE on error.
//////////////////////////////////////////////////////////////////////////
BOOL CMfcClipboardDlg::RetreiveStringFromClipboard( OUT CString & str )
{
// Clear output parameter
str.Empty();
// Try opening the clipboard
if (! OpenClipboard())
{
// Error
return FALSE;
}
// Select clipboard text format basing on project Unicode building flag
#if defined( _UNICODE )
UINT textFormat = CF_UNICODETEXT; // Unicode text
#else
UINT textFormat = CF_TEXT; // ANSI text
#endif // defined( _UNICODE )
// Retrieve clipboard text data
HANDLE hClipboardData = GetClipboardData(textFormat);
if (hClipboardData == NULL)
{
// Error
CloseClipboard();
return FALSE;
}
// Get pointer to text
TCHAR * pszText = reinterpret_cast<TCHAR *>( GlobalLock(
hClipboardData ) );
if (pszText == NULL)
{
// Error
CloseClipboard();
return FALSE;
}
// Deep copy clipboard text in string instance
str = pszText;
// Release the lock on clipboard data handle
GlobalUnlock( hClipboardData );
// Release clipboard access
CloseClipboard();
// All right
return TRUE;
}
</code>
HTH,
Giovanni