MFC - 设置剪切板内容

 1 // 设置剪切板内容
 2 void CLogic::fnCopyStrToClipboard(CString szStr)
 3 {
 4     if ( ::OpenClipboard(NULL) ) 
 5     {
 6         if ( EmptyClipboard() )
 7         {     
 8             size_t cbStr = (szStr.GetLength()+1)*sizeof(TCHAR);    
 9             HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE, cbStr);
10             memcpy_s(GlobalLock(hData), cbStr, szStr.LockBuffer(), cbStr);
11             szStr.UnlockBuffer();
12             GlobalUnlock(hData);
13             UINT nFormat = (sizeof(TCHAR) == sizeof(WCHAR) ? CF_UNICODETEXT:CF_TEXT);
14             if (NULL == ::SetClipboardData(nFormat, hData))
15             {
16                 CloseClipboard();
17                 return;
18             }
19         }
20         CloseClipboard();        //关闭剪切板
21     }
22 }

 

posted @ 2016-05-03 15:37  C/C++/Python/Java  阅读(631)  评论(0编辑  收藏  举报