- 注册时间
- 2021-4-16
- 最后登录
- 2024-7-3
- 在线时间
- 3 小时
编程入门
- 龙马币
- 48
|
网上好多教程讲如何复制到剪贴板,但是有可能复制的是乱码,为了方便,将CString类型的复制到剪贴板。
- CString source;
- if (OpenClipboard())
- {
- //防止非ASCII语言复制到剪切板为乱码
- int buff_size = source.GetLength();
- CStringW strWide = CStringW(source);
- int nLen = strWide.GetLength();
- //将剪切板置空
- ::EmptyClipboard();
- HANDLE clipbuffer = ::GlobalAlloc(GMEM_DDESHARE,(nLen+1)*2);
- if (!clipbuffer)
- {
- ::CloseClipboard();
- return;
- }
- char* buffer = (char*)::GlobalLock(clipbuffer);
- memset(buffer,0,(nLen+1)*2);
- memcpy_s(buffer,nLen*2,strWide.GetBuffer(0),nLen*2);
- strWide.ReleaseBuffer();
- ::GlobalUnlock(clipbuffer);
- ::SetClipboardData(CF_UNICODETEXT,clipbuffer);
- ::CloseClipboard();
- }
复制代码
|
|