在这里找到了些文章,COPY了一些代码可以实现了将自己EDIT中的内容COPY到剪贴板,但再从剪贴板COPY出来就成了乱码,很郁闷,各位大侠看看能不能帮着解决一下。谢谢。if (::OpenClipboard(m_TestEdtTest.m_hWnd))
{
CString strText = _T("");
m_TestEdtTest.GetWindowText(strText);
int iTextLength = strText.GetLength(); ::EmptyClipboard(); HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, iTextLength + 1);
char *pc = (char*)GlobalLock(hGlobal);
memcpy(pc, (LPCSTR)strText, iTextLength);
pc[iTextLength] = 0;
GlobalUnlock(hGlobal);
::SetClipboardData(CF_TEXT, hGlobal);
::CloseClipboard();
GlobalFree(hGlobal);
}其中m_TestEdtTest是CEDIT,也就是EDIT控件对应的变量。
{
CString strText = _T("");
m_TestEdtTest.GetWindowText(strText);
int iTextLength = strText.GetLength(); ::EmptyClipboard(); HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, iTextLength + 1);
char *pc = (char*)GlobalLock(hGlobal);
memcpy(pc, (LPCSTR)strText, iTextLength);
pc[iTextLength] = 0;
GlobalUnlock(hGlobal);
::SetClipboardData(CF_TEXT, hGlobal);
::CloseClipboard();
GlobalFree(hGlobal);
}其中m_TestEdtTest是CEDIT,也就是EDIT控件对应的变量。
::EmptyClipboard();
CString str;
m_edit.GetWindowText(str); int iLen = str.GetLength();
HGLOBAL hGlobal = ::GlobalAlloc(GMEM_MOVEABLE, iLen + 1);
LPVOID p = ::GlobalLock(hGlobal);
memcpy(p, (LPVOID)LPCTSTR(str), iLen + 1);
::GlobalUnlock(hGlobal);
::SetClipboardData(CF_TEXT, hGlobal);
::CloseClipboard();最最重要, 不要用 GlobalFree
strText = "";
int cch = strText.GetLength();
::OpenClipboard(NULL);
EmptyClipboard();
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cch + 1);
char *pc = (char*)GlobalLock(hGlobal);
memcpy(pc, (LPCSTR)strText, cch);
pc[cch] = 0;
GlobalUnlock(hGlobal);
SetClipboardData(CF_TEXT, hGlobal);
CloseClipboard();
// 调用这个函数转换字符 ----------- 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;
}
localalloc/localfree:win32 堆调用(为了与 microsoft windows nt 兼容),这些调用直接与每个进程的默认堆进行对话。