原文地址不详!
/************************************************************************/
/* Copy data to clipboard
/************************************************************************/
BOOL CTools::CopyDataToClipboard(HWND hWnd, CString strmsg)
{
if (::OpenClipboard(hWnd) && ::EmptyClipboard())
{
HGLOBAL hResult = GlobalAlloc(GMEM_MOVEABLE, (strmsg.GetLength() + 1) * sizeof(TCHAR));
if (hResult == NULL)
{
::CloseClipboard();
return FALSE;
}
LPTSTR lptstrCopy = (LPTSTR)GlobalLock(hResult);
memcpy(lptstrCopy, strmsg.GetBuffer(), strmsg.GetLength() * sizeof(TCHAR));
lptstrCopy[strmsg.GetLength()] = (TCHAR)0;
GlobalUnlock(hResult);
strmsg.ReleaseBuffer();
if ( ::SetClipboardData( CF_UNICODETEXT, hResult ) == NULL )
{
TRACE( "Unable to set Clipboard data" );
::CloseClipboard();
return FALSE;
}
::CloseClipboard();
return TRUE;
}
return FALSE;
}
CString CTools::GetDataFromClipboard(HWND hWnd, BOOL bEmptyClipboardAfter/* = TRUE*/)
{
CString strRst;
if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
{
return strRst;
}
if (!OpenClipboard(hWnd))
{
return strRst;
}
HGLOBAL hMem = GetClipboardData(CF_UNICODETEXT);
if (hMem != NULL)
{
LPTSTR lpStr = (LPTSTR)GlobalLock(hMem);
if (lpStr != NULL)
{
// OutputDebugString(lpStr);
strRst.Format(_T("%s"), lpStr);
GlobalUnlock(hMem);
}
}
if (bEmptyClipboardAfter)
::EmptyClipboard();
CloseClipboard();
return strRst;
}