The following is sample code for putting text into the clipboard. In this situation, the data in a CListCtrl is converted to tab-delimited text. You can modify this of course to place text in the clipboard from other sources.
CString Message, Text;
int ItemCount, i;
HGLOBAL hGlobal;
Text ="CM\tKey\tName\tValue\r\n";
ItemCount = m_ctlObjectsTable.GetItemCount();
for (i=0; i<ItemCount; ++i) {
Text += m_ctlObjectsTable.GetItemText(i, 0) + '\t';
Text += m_ctlObjectsTable.GetItemText(i, 1) + '\t';
Text += m_ctlObjectsTable.GetItemText(i, 2) + '\t';
Text += m_ctlObjectsTable.GetItemText(i, 3) + "\r\n";
}
if (!OpenClipboard()) {
MessageBox("The clipboard is temporarily unavailable");
return;
}
if (!EmptyClipboard()) {
CloseClipboard();
MessageBox("The clipboard cannot be emptied");
return;
}
hGlobal = GlobalAlloc(GMEM_MOVEABLE, Text.GetLength()+1);
if (!hGlobal) {
CloseClipboard();
GetErrorMessage(GetLastError(), Message);
MessageBox(CString("Memory allocation error: ")+Message);
return;
}
strcpy((char *)GlobalLock(hGlobal), Text);
GlobalUnlock(hGlobal);
if (!SetClipboardData(CF_TEXT, hGlobal)) {
GetErrorMessage(GetLastError(), Message);
MessageBox(CString("Error setting clipboard: ")+Message);
}
CloseClipboard();
See my Visual C++ Programmer Stuff page for more C++ stuff.