MFC 复制文本到剪贴板

BOOL CopyStringToClipboard(const CStringW text)
{
    if (!::OpenClipboard(NULL))
    {
        return FALSE;
    }

    CStringW src = text;
    ::EmptyClipboard();
    int len = src.GetLength();
    int size = (len + 1) * 2;
    HGLOBAL clipbuffer = GlobalAlloc(GMEM_DDESHARE, size);
    if (!clipbuffer)
    {
        ::CloseClipboard();
        return FALSE;
    }
    char *buffer = (char*)::GlobalLock(clipbuffer);
    memcpy(buffer, src.GetBuffer(), size);
    src.ReleaseBuffer();
    ::GlobalUnlock(clipbuffer);
    ::SetClipboardData(CF_UNICODETEXT, clipbuffer);
    ::CloseClipboard();
    return TRUE;
}

 

你可能感兴趣的:(MFC)