我从MSDN里面拿的代码,稍微改了一下写了这样一段,目的是把str_tmp里的东西复制进剪贴板,但是现在只复制第一个字符,比如下面这个就只有a而不是预想的abcd,求解,多谢各位了~if (!OpenClipboard()) {
MessageBox(_T("Open Clipboard Failure."));
return;
}
EmptyClipboard();
str_tmp=_T("abcd"); LPTSTR chr_tmp = str_tmp.GetBuffer();
int cch = _tcslen(chr_tmp); HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE,
(cch + 1) * sizeof(TCHAR));
if (hglbCopy == NULL)
{
CloseClipboard();
return;// FALSE;
} // Lock the handle and copy the text to the buffer. LPTSTR lptstrCopy;
lptstrCopy =(LPTSTR) GlobalLock(hglbCopy);
_tcscpy_s(lptstrCopy, cch+1, chr_tmp);
GlobalUnlock(hglbCopy); // Place the handle on the clipboard. SetClipboardData(CF_TEXT, hglbCopy);
CloseClipboard();
MessageBox(_T("Open Clipboard Failure."));
return;
}
EmptyClipboard();
str_tmp=_T("abcd"); LPTSTR chr_tmp = str_tmp.GetBuffer();
int cch = _tcslen(chr_tmp); HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE,
(cch + 1) * sizeof(TCHAR));
if (hglbCopy == NULL)
{
CloseClipboard();
return;// FALSE;
} // Lock the handle and copy the text to the buffer. LPTSTR lptstrCopy;
lptstrCopy =(LPTSTR) GlobalLock(hglbCopy);
_tcscpy_s(lptstrCopy, cch+1, chr_tmp);
GlobalUnlock(hglbCopy); // Place the handle on the clipboard. SetClipboardData(CF_TEXT, hglbCopy);
CloseClipboard();
MessageBox(_T("Open Clipboard Failure."));
return;
}
EmptyClipboard();
str_tmp=_T("abcd"); HGLOBAL hglbCopy = GlobalAlloc(GHND | GMEM_SHARE, (str_tmp.GetLength() + 1) * sizeof(TCHAR));
if (hglbCopy == NULL)
{
CloseClipboard();
return;// FALSE;
} // Lock the handle and copy the text to the buffer. LPTSTR lptstrCopy;
lptstrCopy =(LPTSTR) GlobalLock(hglbCopy);
_tcscpy_s(lptstrCopy, cch+1, chr_tmp);
GlobalUnlock(hglbCopy); // Place the handle on the clipboard. SetClipboardData(CF_TEXT, hglbCopy);
CloseClipboard();
MessageBox(_T("Open Clipboard Failure."));
return;
}
EmptyClipboard();
str_tmp=_T("abcd"); HGLOBAL hglbCopy = GlobalAlloc(GHND | GMEM_SHARE, (str_tmp.GetLength() + 1) * sizeof(TCHAR));
if (hglbCopy == NULL)
{
CloseClipboard();
return;// FALSE;
} // Lock the handle and copy the text to the buffer. LPTSTR lptstrCopy = (LPTSTR) GlobalLock(hglbCopy);
_tcscpy(lptstrCopy, str_tmp);
GlobalUnlock(hglbCopy); // Place the handle on the clipboard. SetClipboardData(CF_TEXT, hglbCopy);
CloseClipboard();
LPTSTR chr_tmp = str_tmp.GetBuffer(); //没有指定缓冲区的大小,在VC6下无法编译。代码已经修改,并不是将LZ的代码COPY一遍。
int cch = _tcslen(chr_tmp);
把这个改为
char[100]=_T("abcd");
int cch = _tcslen(chr_tmp);
LPTSTR chr_tmp = str_tmp.GetBuffer(); 然后在项目属性里把unicode编码改为多字节的。应该就可以了