Cstring source;
//文本内容保存在source变量中
if(openClipboard())
{
HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, dource.GetLength()+1);
buffer = (char*)GlobalLock(clipbuffer);
Strcpy(buffer, LPCSTR(source));
GlobalUnlock(clipbuffer);
SetClipboardData(CF_TEXT,clipbuffer);
CloseClipboard();
}
下面的代码显示了如何从剪贴板上获得文本内容:
char * buffer = NULL;
//打开剪贴板
CString fromClipboard;
if ( OpenClipboard() )
{
HANDLE hData = GetClipboardData(CF_TEXT);
char * buffer = (char*)GlobalLock(hData);
fromClipboard = buffer;
GlobalUnlock(hData);
CloseClipboard();
}
if(OpenClipboard());
{
EmptyClipboard();
//创建图元文件DC
CMetaFileDC * cDC = new CMetaFileDC();
cDC->CreateEnhanced(GetDC(),NULL,NULL,"the_name");
//调用绘图例程
//关闭CMetafileDC并获得它的句柄
HENHMETAFILE handle = cDC->CloseEnhanced();
//复制到剪贴板
SetClipBoardData(CF_ENHMETAFILE,handle);
CloseClipboard();
//删除dc
delete cDC;
}
下面的代码演示了从剪贴板获得图元文件并将其绘制到client DC上:
if(OpenClipboard())
{
//获得剪贴板数据
HENMETAFILE handle = (HENMETAFILE)GetClipboardData(CF_ENHMETAFILE);
//显示
CClientDC dc(this);
CRect client(0,0,200,200);
dc.PlayMetaFile(handle,client);
//关闭剪贴板
CloseClipboard();
}
if(OpenClipboard())
{
//获得剪贴板数据
HENMETAFILE handle = (HENMETAFILE)GetClipboardData(CF_ENHMETAFILE);
//显示
CClientDC dc(this);
CRect client(0,0,200,200);
dc.PlayMetaFile(handle,client);
//关闭剪贴板
CloseClipboard();
}
if(OpenClipboard())
{
EmptyClipboard();
CBitmap * junk = new CBitmap();
CClientDC cdc(this);
CDC dc;
dc.CreateCompatibleDC(&cdc);
CRect client(0,0,200,200);
junk->CreateCompatibleBitmap(&cdc,client.Width(),client.Height());
dc.SelectObject(junk);
DrawImage(&dc,CString("Bitmap"));
//复制数据到剪贴板
SetClipboardData(CF_BITMAP,junk->m_hObject);
CloseClipboard();
delete junk;
}
下面的代码显示了如何从剪贴板上获得位图数据:
if(OpenClipboard())
{
//获得剪贴板数据
HBITMAP handle = (HBITMAP)GetClipboardData(CF_BITMAP);
CBitmap * bm = CBitmap::FromHandle(handle);
CClientDC cdc(this);
CDC dc;
dc.CreateCompatibleDC(&cdc);
dc.SelectObject(bm);
cdc.BitBlt(0,0,200,200,&dc,0,0,SRCCOPY);
CloseClipboard();
}
struct MyFormatData
{
long val1;
int val2;
};
我们要把它复制到剪贴板,可以使用如下的代码:
UINT format = RegisterClipBoardFormat("MY_CUSTOM_FORMAT");
if(OpenClipboard())
{
MyFormatData data;
data.val1 = 100;
data.val2 = 200;
HGLOBAL clipbuffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, sizeof(MyFormatData));
MyFormatData * buffer = (MyFormatData*)GlobalLock(clipbuffer);
//保存到内存
*buffer = data;
//保存到剪贴板
GlobalUnlock(clipbuffer);
SetClipboardData(format,clipbuffer);
CloseClipboard();
}
UINT format = RegisterClipboardFormat("MY_CUSTOM_FORMAT");
MyFormatData data;
if(Openclipboard())
{
HANDLE hData =GetClipboardData(format);
MyFormatData * buffer = (MyFormatData*)GlobalLock(hData);
data = *buffer;
GlobalUnlock(hData);
CloseClipboard();
}
SetClipboardViewer(); //将我们的程序添加到剪贴板观察链
In your message map add:
ON_MESSAGE(WM_DRAWCLIPBOARD, OnClipChange) //添加Message handle
Which is declared as:
afx_msg void OnClipChange();
Finally implement:
void CDetectClipboardChangeDlg::OnClipChange()
{
CTime time = CTime::GetCurrentTime();
SetDlgItemText(IDC_CHANGED_DATE,time.Format("%a, %b %d, %Y -- %H:%M:%S"));
DisplayClipboardText();
}
::SendMessage(m_hTextWnd, WM_PASTE, 0, 0);
一个应用例子:
void CChatDlg::OnFace()
{
// TODO: Add your control notification handler code here
int index=m_BmpPicker.GetCurrentBitmapIndex();
if(OpenClipboard())
{
EmptyClipboard();
CBitmap * junk =m_BmpPicker.GetBitmapAt(index);
//复制数据到剪贴板
SetClipboardData(CF_BITMAP,junk->m_hObject);
CloseClipboard();
}
//MessageBox("已选择表情!请在需要插入表情处进行粘贴");
CWnd *pWnd = GetDlgItem(IDC_RICHTEXT_SEND); // 取得控件的指针
HWND hWnd = pWnd->GetSafeHwnd(); // 取得控件的句柄
::SendMessage(hWnd, WM_PASTE, 0, 0);
}
简单的说:
一、打开剪切板
OpenClipboard();
二、获得指向剪切板的全局指针变量
HANDLE hClipMemory=::GetClipboardData(CF_TEXT);
三、锁定剪切板
LPBYTE lpClipMemory = (LPBYTE)GlobalLock(hClipMemory);
四、获取剪切板内容
CString m_sMessage = CString(lpClipMemory);
五、解锁剪切板
GlobalUnlock(hClipMemory);
六、关闭剪切板
::CloseClipboard();
读取剪切板内容必须先锁定剪切板,否则会发生异常。以上只能读取文本格式的内容,无法读取位图,具体请查阅MSDN帮助文档。