MFC 拷贝ListBox单行的数据到剪切板

来源:互联网 发布:android开源项目源码 编辑:程序博客网 时间:2024/05/29 10:21

由于从控件上得到的数据是宽字符,所以分配空间的时候需要

注意的是单行数据长度要乘以2作为GlobalAlloc分配的大小

void Show::OnLbnDblclkListResult(){    int curSel = m_listResult.GetCurSel();    CString str;    m_listResult.GetText(curSel, str);    BOOL bret = OpenClipboard();    EmptyClipboard();    int len = str.GetLength() * 2;    HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, len + 2);    wchar_t *p = (wchar_t*)GlobalLock(hGlobal);    wmemset(p, 0, str.GetLength()+1);    wmemcpy(p, str.GetBuffer(), str.GetLength()+1);    bret = GlobalUnlock(hGlobal);    HANDLE hResult = SetClipboardData(CF_UNICODETEXT, hGlobal);    w_return_if_fail(NULL!=hResult);    CloseClipboard();    str.Format(L"拷贝数据到剪切板成功, 长度:%d", str.GetLength());    MessageBox(str);}


原创粉丝点击