标签:成功 ret col clipboard open hello cli tst edit
实现“复制”功能:
1 void C剪贴板Dlg::OnBnClickedButton1() 2 { 3 if (!OpenClipboard()) return; 4 if (!EmptyClipboard()) { 5 CloseClipboard(); 6 return; 7 } 8 //拷贝之前的准备工作 9 CString szStr = _T("Hello,World!"); 10 int cbStr = (szStr.GetLength() + 1) * sizeof(TCHAR);//获得字符串总长度 11 HGLOBAL hData = GlobalAlloc(GMEM_MOVEABLE, cbStr);//申请内存空间 12 memcpy_s(GlobalLock(hData), cbStr, szStr.LockBuffer(), cbStr);//锁定内存空间和字符串并拷贝字符串到申请的内存地址 13 GlobalUnlock(hData);//解锁内存空间锁定 14 szStr.UnlockBuffer();//解锁字符串锁定 15 16 UINT uiFormat = (sizeof(TCHAR) == sizeof(WCHAR))?CF_UNICODETEXT:CF_TEXT; 17 if (::SetClipboardData(uiFormat, hData) == NULL) { 18 MessageBox(_T("设置剪贴板数据失败")); 19 CloseClipboard(); 20 return; 21 } 22 CloseClipboard(); 23 MessageBox(_T("设置剪贴板数据成功")); 24 }
实现“粘贴”功能:
1 void C剪贴板Dlg::OnBnClickedButton2() 2 { 3 UINT uiFormat = (sizeof(TCHAR) == sizeof(WCHAR)) ? CF_UNICODETEXT : CF_TEXT; 4 if (!IsClipboardFormatAvailable(uiFormat)) return;//格式是否判断正确 5 if (!OpenClipboard()) return; 6 7 HANDLE hGlobal = GetClipboardData(uiFormat);//获得剪贴板数据 8 if (hGlobal) { 9 LPTSTR lptstr = (LPTSTR)GlobalLock(hGlobal);//锁定剪贴板数据 10 if (lptstr) { 11 SetDlgItemText(IDC_EDIT1, lptstr); 12 GlobalUnlock(hGlobal);//解锁剪贴板数据锁定 13 } 14 } 15 CloseClipboard(); 16 }
标签:成功 ret col clipboard open hello cli tst edit
原文地址:https://www.cnblogs.com/mktest123/p/12129021.html