클립보드에 문자열을 저장하는 팁입니다.

 

OpenClipboard 로 클립보드를 오픈하고 SetClipboardData 함수로 클립보드에 문자열을 저장합니다.

 

BOOL CUtility::SetClipBoardString(CString data)

{

 

           if ( !OpenClipboard(NULL) )

           {

                     m_errMessage = L"Cannot open the Clipboard";

                     return FALSE;

           }

          

           if( !EmptyClipboard() )

           {

                     m_errMessage = _T("Cannot empty the Clipboard");

                     return FALSE;

           }

          

           wchar_t *wcBuffer = 0;

           HGLOBAL hglbCopy = GlobalAlloc( GMEM_MOVEABLE, ( data.GetLength() + 1 ) * sizeof (wchar_t) );

           wcBuffer = ( wchar_t* )GlobalLock( hglbCopy );

           wcscpy( wcBuffer,  data );

 

           GlobalUnlock( hglbCopy ); 

          

           if ( ::SetClipboardData( CF_UNICODETEXT, hglbCopy ) == NULL )

           {

                     m_errMessage.Format(L"Unable to set Clipboard data, error: %d", GetLastError());

                     CloseClipboard();           

                     return FALSE;

           }

           CloseClipboard();

           return TRUE;