复制文本到剪贴板

复制文本到剪贴板

BOOL PutTextToClipboard(LPCTSTR pTxtData)
{
 BOOL bRet = FALSE;

 if (OpenClipboard(AfxGetMainWnd()->GetSafeHwnd()))
 {
  EmptyClipboard();
  HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (lstrlen(pTxtData) + 1) * sizeof(TCHAR));
  
  if (hData != NULL)
  {
   LPTSTR pszData = (LPTSTR)::GlobalLock(hData);
   lstrcpy(pszData, pTxtData);
   GlobalUnlock(hData);
   
#ifdef _UNICODE
   bRet = (SetClipboardData(CF_UNICODETEXT, hData) != NULL);
#else
   bRet = (SetClipboardData(CF_TEXT, hData) != NULL);
#endif // _UNICODE   
  }

  CloseClipboard();
 }

 return bRet;
}

你可能感兴趣的:(复制文本到剪贴板)