Discussion:
Unable to use SetClipboardData.
(too old to reply)
s***@gmail.com
2009-02-05 15:25:56 UTC
Permalink
i want to cpture the cilpboard data, and modify it then want to set
it into Ciplboard back.How can i do?
Thnaks
------------------------------------------------------------------------------------------------------------------
HGLOBAL hglb;
WCHAR* wszDomain = NULL;
char* pData;
if (!IsClipboardFormatAvailable(CF_TEXT))
return;
OpenClipboard();
pData = (char*) ::GetClipboardData(CF_TEXT);
int n = strlen(pData);
CloseClipboard();
HGLOBAL HMem = NULL;
TCHAR* pMem = NULL;
HMem = (TCHAR*) ::GlobalAlloc((GMEM_MOVEABLE | GMEM_DDESHARE ), (n *
2) + 1 );
if(HMem == NULL) { return;}
pMem = (TCHAR*) ::GlobalLock(HMem);
if(pMem == NULL)
{
::GlobalFree(HMem); return;
}
n = strlen(pData);
if(n > 0 )
{
wszDomain = new WCHAR[(n * 2) + 1];
wmemset(wszDomain,0,( n * 2 ) + 1);
MultiByteToWideChar( CP_UTF8, 0, (const char*)pData,n ,
wszDomain, (n * 2));
}
_tcscpy(pMem,wszDomain);
::GlobalUnlock(HMem);
if(!OpenClipboard())
{
::GlobalFree(HMem); return;
}
SetClipboardData(CF_UNICODETEXT,HMem);
CloseClipboard();

if( wszDomain ) delete wszDomain; wszDomain = NULL;
Jonathan Wood
2009-02-05 16:20:02 UTC
Permalink
There's a bit of code there. The first thing I'd do is step through with the
debugger. Are any of your error checks finding problems? Does the data
appear to be what you expect?

Since you didn't even say what result you are getting, trying to resolve
this myself would mean creating a test application and starting from
scratch.
--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
http://www.softcircuits.com/blog/
Post by s***@gmail.com
i want to cpture the cilpboard data, and modify it then want to set
it into Ciplboard back.How can i do?
Thnaks
------------------------------------------------------------------------------------------------------------------
HGLOBAL hglb;
WCHAR* wszDomain = NULL;
char* pData;
if (!IsClipboardFormatAvailable(CF_TEXT))
return;
OpenClipboard();
pData = (char*) ::GetClipboardData(CF_TEXT);
int n = strlen(pData);
CloseClipboard();
HGLOBAL HMem = NULL;
TCHAR* pMem = NULL;
HMem = (TCHAR*) ::GlobalAlloc((GMEM_MOVEABLE | GMEM_DDESHARE ), (n *
2) + 1 );
if(HMem == NULL) { return;}
pMem = (TCHAR*) ::GlobalLock(HMem);
if(pMem == NULL)
{
::GlobalFree(HMem); return;
}
n = strlen(pData);
if(n > 0 )
{
wszDomain = new WCHAR[(n * 2) + 1];
wmemset(wszDomain,0,( n * 2 ) + 1);
MultiByteToWideChar( CP_UTF8, 0, (const char*)pData,n ,
wszDomain, (n * 2));
}
_tcscpy(pMem,wszDomain);
::GlobalUnlock(HMem);
if(!OpenClipboard())
{
::GlobalFree(HMem); return;
}
SetClipboardData(CF_UNICODETEXT,HMem);
CloseClipboard();
if( wszDomain ) delete wszDomain; wszDomain = NULL;
Joseph M. Newcomer
2009-02-05 19:54:50 UTC
Permalink
See below...
Post by s***@gmail.com
i want to cpture the cilpboard data, and modify it then want to set
it into Ciplboard back.How can i do?
Thnaks
------------------------------------------------------------------------------------------------------------------
HGLOBAL hglb;
WCHAR* wszDomain = NULL;
char* pData;
****
Not clear you need a char * here; a TCHAR* or WCHAR* should work as well, and would be a
better choice
****
Post by s***@gmail.com
if (!IsClipboardFormatAvailable(CF_TEXT))
return;
****
I usually do this as
#ifdef _UNICODE
if(!IsClipboardFormatAvailable(CF_UNICODETEXT))
return;
#else
if(!IsClipboardFormatAvailable(CF_TEXT))
return;
#endif

Note that the clipboard will intrinsically convert an 8-bit CF_TEXT to a CF_UNICODETEXT or
a piece of Unicode text to CF_TEXT, so you don't need to do the conversions yourself
****
Post by s***@gmail.com
OpenClipboard();
pData = (char*) ::GetClipboardData(CF_TEXT);
int n = strlen(pData);
****
I'd use the same technique, or I'd define MY_CF_TEXT as either CF_UNICODETEXT or CF_TEXT
based on the _UNICODE/UNICODE symbols (I never remember which supports Windows and which
is for the C library, but if they are not both set or both unset chaos rules).

Then I'd use _tcslen to obtain the length. Strictly speaking you could do this *after*
you closed the clipboard
****
Post by s***@gmail.com
CloseClipboard();
HGLOBAL HMem = NULL;
TCHAR* pMem = NULL;
HMem = (TCHAR*) ::GlobalAlloc((GMEM_MOVEABLE | GMEM_DDESHARE ), (n *
2) + 1 );
if(HMem == NULL) { return;}
pMem = (TCHAR*) ::GlobalLock(HMem);
if(pMem == NULL)
{
::GlobalFree(HMem); return;
}
n = strlen(pData);
****
You already computed this
****
Post by s***@gmail.com
if(n > 0 )
{
wszDomain = new WCHAR[(n * 2) + 1];
****
I'm not sure whey you are using n*2+1 when you should be allocating n+1 WCHARs. Note that
you do NOT need to deal with the bytes-vs-chars issue because it *is* a WCHAR (had it been
a malloc call, you would need to allocate (n +1) * sizeof(WCHAR) bytes, so your factoring
of the computation would be erroneous (you'd be one byte short in a malloc). But new
doesn't need this
****
Post by s***@gmail.com
wmemset(wszDomain,0,( n * 2 ) + 1);
****
You're about to fill it in. You don't need to zero it. And again, the computation
(n*2)+1 is erroneous, because it doesn't zero the last character. In your case, you only
zero out about half the string you allocated. The correct computation for it would be (n
+ 1)*sizeof(WCHAR). But you don't need to do this at all.

Also, you are doing a premature allocation of space. You should be calling
MultiByteToWideChar once with the target buffer,size as NULL,0, and you will get the
correct size to allocate, expressed in characters (which includes space for the
terminating NUL character), do your allocation of that many WCHARs, and then do a MBTWC
with the real buffer pointer and the real length.
*****
Post by s***@gmail.com
MultiByteToWideChar( CP_UTF8, 0, (const char*)pData,n ,
wszDomain, (n * 2));
}
_tcscpy(pMem,wszDomain);
::GlobalUnlock(HMem);
if(!OpenClipboard())
{
::GlobalFree(HMem); return;
****
Why did you write two lines of code on a single line? Do you like creating completely
unreadable code?
****
Post by s***@gmail.com
}
SetClipboardData(CF_UNICODETEXT,HMem);
CloseClipboard();
if( wszDomain ) delete wszDomain; wszDomain = NULL;
****
Why are three lines of code written in one line? Just to make it impossible to read?

It appears that what you are actually trying to do here is convert an 8-bit character
string to a Unicode string in the clipboard, but why? This is automatically handled by
the clipboard mechanism!

Go to the description of SetClipboardData, hyperlink into the predefined clipboards format
page, and scroll down to the section on synthesized formats. You are doing a lot of work
that serves no useful purpose. If your only modification is to change from text to
Unicode text, throw ALL of this code away!
joe
****
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Giovanni Dicanio
2009-02-06 15:46:10 UTC
Permalink
Post by s***@gmail.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
s***@gmail.com
2009-02-14 05:50:09 UTC
Permalink
On Feb 6, 8:46 pm, "Giovanni Dicanio"
Post by Giovanni Dicanio
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.
 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
http://www.geocities.com/giovanni.dicanio/vc/MfcClipboard.zip
The relevant code for methods implementing copy and paste operations
<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
Thanks..
What i did .. 1) Get Data from CilpBoard and the Save it in Text File
and then Empty Cilpboard and Set this text file data in Cilpboard.I
did this because when user copy data from HTML Page with table , this
table get printed in Edit box , so i copy it in Text file and then
read this data from txt file and the set it again in Cilpboard.
Joseph M. Newcomer
2009-02-15 03:23:23 UTC
Permalink
See below...
Post by s***@gmail.com
On Feb 6, 8:46 pm, "Giovanni Dicanio"
Post by Giovanni Dicanio
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.
 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
http://www.geocities.com/giovanni.dicanio/vc/MfcClipboard.zip
The relevant code for methods implementing copy and paste operations
<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
Thanks..
What i did .. 1) Get Data from CilpBoard and the Save it in Text File
and then Empty Cilpboard and Set this text file data in Cilpboard.I
did this because when user copy data from HTML Page with table , this
table get printed in Edit box , so i copy it in Text file and then
read this data from txt file and the set it again in Cilpboard.
****
WHY? What possible value is there to copying the data to a file and then copying the file
back to the clipboard? This seems completely unnecessary. What do you do with the file
after you are done?

This seems to be even clumsier than the code you just showed above, whose sole purpose
seems to be to convert text from ANSI to Unicode, a transformation that is completely
without purpose because this is already handled by the clipboard mechanism!
joe
****
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
s***@gmail.com
2009-02-16 13:16:30 UTC
Permalink
Post by Joseph M. Newcomer
See below...
Post by s***@gmail.com
On Feb 6, 8:46 pm, "Giovanni Dicanio"
Post by Giovanni Dicanio
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.
 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
http://www.geocities.com/giovanni.dicanio/vc/MfcClipboard.zip
The relevant code for methods implementing copy and paste operations
<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
Thanks..
What i did .. 1) Get Data from CilpBoard and the Save it in Text File
and then Empty Cilpboard and Set this text file data in Cilpboard.I
did this because when user copy data from HTML Page with table , this
table get printed in Edit box , so i copy it in Text file and then
read this data from txt file and the set it again in Cilpboard.
****
WHY?  What possible value is there to copying the data to a file and then copying the file
back to the clipboard?  This seems completely unnecessary.  What do you do with the file
after you are done?
This seems to be even clumsier than the code you just showed above, whose sole purpose
seems to be to convert text from ANSI to Unicode, a transformation that is completely
without purpose because this is already handled by the clipboard mechanism!
                                        joe
****
Joseph M. Newcomer [MVP]
Web:http://www.flounder.com
MVP Tips:http://www.flounder.com/mvp_tips.htm- Hide quoted text -
- Show quoted text -
Why i store the clipbord data first in text file and the againg reade
this data from text file and store this data into Cilpboard?

If i copy normal text from any text file and copy it in rich edit
control it is working fine.But in case i copy the data from HTML page
which have table (User copy any thing , user will copy text with tabel
or any image (image is restricted)) the hole table get inserted in
rich edit box.I am unable to remove the table.But i observed that if
this text i copy in text file the it will not inculde or remove the
table. So that i decide to cpoy the the data in text file first then
in again store it in clipbord again.
I m sorry if i did wrong, (i have to do this as early as possible so i
did it).

For Past - Context menu -- i only open the clipboard and get the TEXT
from it.It is working fine using ON_UPDATE_COMMAND_UI.
But for Ctrl+v it is not working fine.
Joseph M. Newcomer
2009-02-16 18:59:08 UTC
Permalink
See below...
Post by s***@gmail.com
Post by Joseph M. Newcomer
See below...
Post by s***@gmail.com
On Feb 6, 8:46 pm, "Giovanni Dicanio"
Post by Giovanni Dicanio
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.
 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
http://www.geocities.com/giovanni.dicanio/vc/MfcClipboard.zip
The relevant code for methods implementing copy and paste operations
<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
Thanks..
What i did .. 1) Get Data from CilpBoard and the Save it in Text File
and then Empty Cilpboard and Set this text file data in Cilpboard.I
did this because when user copy data from HTML Page with table , this
table get printed in Edit box , so i copy it in Text file and then
read this data from txt file and the set it again in Cilpboard.
****
WHY?  What possible value is there to copying the data to a file and then copying the file
back to the clipboard?  This seems completely unnecessary.  What do you do with the file
after you are done?
This seems to be even clumsier than the code you just showed above, whose sole purpose
seems to be to convert text from ANSI to Unicode, a transformation that is completely
without purpose because this is already handled by the clipboard mechanism!
                                        joe
****
Joseph M. Newcomer [MVP]
Web:http://www.flounder.com
MVP Tips:http://www.flounder.com/mvp_tips.htm- Hide quoted text -
- Show quoted text -
Why i store the clipbord data first in text file and the againg reade
this data from text file and store this data into Cilpboard?
If i copy normal text from any text file and copy it in rich edit
control it is working fine.But in case i copy the data from HTML page
which have table (User copy any thing , user will copy text with tabel
or any image (image is restricted)) the hole table get inserted in
rich edit box.I am unable to remove the table.But i observed that if
this text i copy in text file the it will not inculde or remove the
table. So that i decide to cpoy the the data in text file first then
in again store it in clipbord again.
I m sorry if i did wrong, (i have to do this as early as possible so i
did it).
****
But in this case, all you are trying to do is remove a rich format from the clipboard.
There is no need to create a file at all! You can just read CF_TEXT into a string, clear
the clipboard, and write the CF_TEXT out. No file need be involved at all.

Note that what is happening here is that the copy from the HTML page saves the data in a
rich format, and a rich edit control will suggest that it prefers this rich format, so it
is working as it is supposed to work. There is no way to make paste in a rich edit
control NOT use the richest format it can, so you need to do what you have done: read the
text format, then put it back into an empty clipboard. So that part is fine, but there is
no reason to create a file as part of the process.
****
Post by s***@gmail.com
For Past - Context menu -- i only open the clipboard and get the TEXT
from it.It is working fine using ON_UPDATE_COMMAND_UI.
But for Ctrl+v it is not working fine.
****
ON_UPDATE_COMMAND_UI? What are you doing in this? Typically, the only thing the
ON_UPDATE_COMMAND_UI does with the clipboard is determine if a suitable format is
available and enable/disable a menu item based on the availability of the format, but it
would not actually be modifying the contents of the clipboard.
joe
****
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Loading...