如题,我的是这样实现的:
我想把列表中被选中的项的文本copy到剪贴板,GI_ListViewItem是我自定义的结构体。
GI_ListViewItem Item;
GetItemInfo(iSel, Item);
// Open Clipboard
if(FALSE == OpenClipboard())
{
ErrorHelper err;
MessageBox(err .GetLastErrorString(), APP_VER);
return;
}
// Empty Clipboard
if(FALSE == EmptyClipboard())
{
ErrorHelper err;
MessageBox(err .GetLastErrorString(), APP_VER);
CloseClipboard();
return;
}
// Allocate a global memory object for the text.
HGLOBAL hGlobal;
hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, 2 + sizeof(TCHAR) * Item .m_sFolderPath .GetLength());
if(hGlobal == NULL)
{
// Error
CloseClipboard();
return;
}
// Lock the handle and copy the text to the buffer.
LPBYTE pBuffer = (LPBYTE)GlobalLock(hGlobal);
memcpy(pBuffer, (LPCTSTR)Item .m_sFolderPath, sizeof(TCHAR) * Item .m_sFolderPath .GetLength());
// Unlock
GlobalUnlock(hGlobal);
// Set Clipboard Data
if(NULL == SetClipboardData(CF_TEXT, hGlobal))
{
ErrorHelper err;
MessageBox(err .GetLastErrorString(), APP_VER); GlobalUnlock(hGlobal);
CloseClipboard();
GlobalFree(hGlobal);
return;
} // Close Clipboard
CloseClipboard();
我想把列表中被选中的项的文本copy到剪贴板,GI_ListViewItem是我自定义的结构体。
GI_ListViewItem Item;
GetItemInfo(iSel, Item);
// Open Clipboard
if(FALSE == OpenClipboard())
{
ErrorHelper err;
MessageBox(err .GetLastErrorString(), APP_VER);
return;
}
// Empty Clipboard
if(FALSE == EmptyClipboard())
{
ErrorHelper err;
MessageBox(err .GetLastErrorString(), APP_VER);
CloseClipboard();
return;
}
// Allocate a global memory object for the text.
HGLOBAL hGlobal;
hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, 2 + sizeof(TCHAR) * Item .m_sFolderPath .GetLength());
if(hGlobal == NULL)
{
// Error
CloseClipboard();
return;
}
// Lock the handle and copy the text to the buffer.
LPBYTE pBuffer = (LPBYTE)GlobalLock(hGlobal);
memcpy(pBuffer, (LPCTSTR)Item .m_sFolderPath, sizeof(TCHAR) * Item .m_sFolderPath .GetLength());
// Unlock
GlobalUnlock(hGlobal);
// Set Clipboard Data
if(NULL == SetClipboardData(CF_TEXT, hGlobal))
{
ErrorHelper err;
MessageBox(err .GetLastErrorString(), APP_VER); GlobalUnlock(hGlobal);
CloseClipboard();
GlobalFree(hGlobal);
return;
} // Close Clipboard
CloseClipboard();
UNICODE
UTF-8 CODE编码错误
应该查什么资料呢?MSDN?
www.codeproject.com
中的"剪贴板"专栏.
我想应该有人遇到过同样的问题,不会没有吧?
把上面的代码作了以下修改:
// Lock the handle and copy the text to the buffer.
char* pBuffer = (char*)GlobalLock(hGlobal);
// 调用这个函数转换字符 ----------- 1
if(0 == MultiByteToWideChar(CP_ACP, 0,
(LPCTSTR)Item .m_sFolderPath , Item .m_sFolderPath .GetLength() + 1,
(WCHAR*)pBuffer, iBufferLenInBytes ))
{
GlobalUnlock(hGlobal);
CloseClipboard();
GlobalFree(hGlobal);
return;
}
然后是:
// Set Clipboard Data
// 用CF_UNICODETEXT,而不是CF_TEXT ----------------- 2
if(NULL == SetClipboardData(CF_UNICODETEXT, hGlobal))
{
GlobalUnlock(hGlobal);
CloseClipboard();
GlobalFree(hGlobal);
return;
}
希望对一些人有用 ^_^