事實(shí)上,有兩種不同的剪貼板接口機(jī)制。第一種機(jī)制是使用Windows剪貼板API,第二種機(jī)制則是使用OLE。以為剪貼板API是迄今為止最常用的方法,一次本章中主要介紹使用Windows剪貼板API。
1、相關(guān)函數(shù)的介紹
1)為數(shù)據(jù)分配內(nèi)存空間:
HGLOBAL GlobalAlloc(UINT uFlags,SIZE_T dwBytes),uFlags參數(shù)用來說明Windows如何分配內(nèi)存(有GHND、GMEM_FIXED(默認(rèn)值,可以用0或NULL代替)、GMEM_MOVEABLE和GPTR)。參數(shù)dwBytes是一個(gè)雙字節(jié)(DWORD)值,用來指定分配的緩沖區(qū)的大小。
2)鎖定全局內(nèi)存:LPVOID GlobalLock(HGLOBAL hMem)
3)解除對全局內(nèi)存的鎖定:BOOL GlobalUnlock(HGLOBAL hMem)
4)打開剪貼板:BOOL OpenClipboard(HWND hWndNewOwner)
5)清空剪貼板:BOOL EmptyClipboard()
6)設(shè)置剪貼板數(shù)據(jù):HANDLE SetClipboardData(UINT uFormat,HANDLE hMem),uFormat值常見的有(CF_BITMAP、CF_TEXT、CF_OMETEXT)
7)關(guān)閉剪貼板:BOOL CloseClipboard()
2、示例程序
相關(guān)變量沒給出,自己看程序創(chuàng)建

1)傳遞純文本
//傳遞純文本
void CSimpleTextTransferDlg::OnButton2() //按下Copy按鈕事件
{
// TODO: Add your control notification handler code here
if(UpdateData())
{
CString strData;
m_edtToClipboard.GetWindowText(strData);
if(OpenClipboard())
{
EmptyClipboard();
HGLOBAL hClipboardData;
hClipboardData=GlobalAlloc(GMEM_DDESHARE,strData.GetLength()+1);
char* pchData;
pchData=(char*)GlobalLock(hClipboardData);
strcpy(pchData,LPCSTR(strData));
GlobalUnlock(hClipboardData);
SetClipboardData(CF_TEXT,hClipboardData);
CloseClipboard();
}
}
}
void CSimpleTextTransferDlg::OnButton3() //按下cut按鈕事件
{
// TODO: Add your control notification handler code here
OnButton2();
m_edtToClipboard.SetWindowText("");
}
void CSimpleTextTransferDlg::OnButton4()//按下Paste按鈕事件
{
// TODO: Add your control notification handler code here
if (OpenClipboard())
{
HANDLE hClipboardData=GetClipboardData(CF_TEXT);
char* pchData=(char*)GlobalLock(hClipboardData);
CString StrFromClipboard=pchData;
m_edtFromClipboard.SetWindowText(StrFromClipboard);
GlobalUnlock(hClipboardData);
CloseClipboard();
}
}
2)轉(zhuǎn)移位圖圖像
//按Alt+Print Screen組合鍵,可以把當(dāng)前窗口的位圖圖像復(fù)制到剪貼板上
void CSimpleTextTransferDlg::OnButton7()//按下Paste Bitmap按鈕事件,粘帖剪貼板中的位圖圖像
{
// TODO: Add your control notification handler code here
if(OpenClipboard())
{
if(::IsClipboardFormatAvailable(CF_BITMAP))
{
HBITMAP handle=(HBITMAP)GetClipboardData(CF_BITMAP);
CBitmap* bm=CBitmap::FromHandle(handle);
CClientDC cdc(this);
CDC dc;
dc.CreateCompatibleDC(&cdc);
dc.SelectObject(bm);
RECT rect;
m_grpBitmapFromClipboard.GetWindowRect(&rect);
ScreenToClient(&rect);
cdc.BitBlt(rect.left+10,rect.top+20,(rect.right-rect.left)-10,(rect.bottom-rect.top)-40,&dc,0,0,SRCCOPY);
}
else
{
AfxMessageBox("There is no BITMAP data on the Clipboard");
}
CloseClipboard();
}
}
void CSimpleTextTransferDlg::OnButton6()//按下Copy Test Bitmap按鈕事件,吧位圖圖像復(fù)制到剪貼板上
{
// TODO: Add your control notification handler code here
if(OpenClipboard())
{
EmptyClipboard();
CBitmap* pNewBitmap=new CBitmap();
CClientDC cdc(this);
CDC dc;
dc.CreateCompatibleDC(&cdc);
CRect client(0,0,200,200);
pNewBitmap->CreateCompatibleBitmap(&cdc,client.Width(),client.Height());
dc.SelectObject(pNewBitmap);
CString strBitmapText;
m_edtBitmapText.GetWindowText(strBitmapText);
DrawImage(&dc,strBitmapText);
SetClipboardData(CF_BITMAP,pNewBitmap->m_hObject);
CloseClipboard();
delete pNewBitmap;
}
//MessageBox("The bitmap has been copied to clipboard.haha",NULL,MB_OK);
}
void CSimpleTextTransferDlg::DrawImage(CDC *pDC, CString pText)//進(jìn)行GDI調(diào)用進(jìn)行做圖
{
CRect rectDrawingArea(0,0,200,200);
pDC->FillSolidRect(rectDrawingArea,RGB(0,208,200));
CPen pen;
pen.CreatePen(PS_SOLID,3,RGB(0,0,255));
CPen * oldpen=pDC->SelectObject(&pen);
pDC->MoveTo(70,10);
pDC->LineTo(70,190);
pDC->MoveTo(130,10);
pDC->LineTo(130,190);
pDC->MoveTo(10,70);
pDC->LineTo(190,70);
pDC->MoveTo(10,130);
pDC->LineTo(190,130);
pDC->SelectObject(oldpen);
pen.DeleteObject();
pDC->TextOut(10,10,pText);
pDC->TextOut(20,50,pText);
pDC->TextOut(50,100,pText);
}
3)轉(zhuǎn)移自定義數(shù)據(jù)
在把自定義數(shù)據(jù)復(fù)制到剪貼板上之前,首先要把該數(shù)據(jù)注冊為Windows可以識(shí)別的格式。UINT RegisterClipboardFormat(LPCTSTR lpszFormat),lpszFormat是要被注冊的格式的名稱,該名稱可以用任何合法的字符串來表示。
把自定義數(shù)據(jù)復(fù)制到剪貼板上首先,在頭文件中添加CCustomer類,接著為Copy Data按鈕添加按下事件。
class CCustomer
{
public:
int iCustomerNumber;
float dCustomerBalance;
};
void CSimpleTextTransferDlg::OnButton5()
{
// TODO: Add your control notification handler code here
if(UpdateData())
{
UINT uiCUstomerDataFormat=RegisterClipboardFormat("CUSTOMER_DATA");
if(OpenClipboard())
{
EmptyClipboard();
CCustomer customer;
customer.iCustomerNumber=m_iCustomerNumber;
customer.dCustomerBalance=m_dCustomerBalance;
HGLOBAL hClipboardData;
hClipboardData=GlobalAlloc(GMEM_DDESHARE,sizeof(CCustomer));
CCustomer * pchCustomerData=(CCustomer*)GlobalLock(hClipboardData);
*pchCustomerData=customer;
GlobalUnlock(hClipboardData);
SetClipboardData(uiCUstomerDataFormat,hClipboardData);
CloseClipboard();
}
}
}
從剪貼板上粘貼自定義數(shù)據(jù)
void CSimpleTextTransferDlg::OnButton8()
{
// TODO: Add your control notification handler code here
UINT uiCUstomerDataFormat=RegisterClipboardFormat("CUSTOMER_DATA");
if(OpenClipboard())
{
if(::IsClipboardFormatAvailable(uiCUstomerDataFormat))
{
HANDLE hData=GetClipboardData(uiCUstomerDataFormat);
CCustomer * pchCustomerData=(CCustomer*)GlobalLock(hData);
CCustomer customer=*pchCustomerData;
m_iCustomerNumber=customer.iCustomerNumber;
m_dCustomerBalance=customer.dCustomerBalance;
UpdateData(FALSE);
GlobalUnlock(hData);
CloseClipboard();
}
}
}
4)接收剪貼板上內(nèi)容已被修改的通知消息
添加DispalyClipboardData函數(shù)來顯示剪貼板中的內(nèi)容
void CSimpleTextTransferDlg::DisplayClipboardData()
{
m_strClipboardText="";
if(OpenClipboard())
{
if(::IsClipboardFormatAvailable(CF_TEXT)||::IsClipboardFormatAvailable(CF_OEMTEXT)||::IsClipboardFormatAvailable(CF_BITMAP))
{
m_strNotExt.ShowWindow(SW_HIDE);
HANDLE hClipboardData=GetClipboardData(CF_TEXT);
char *pchData=(char*)GlobalLock(hClipboardData);
m_strClipboardText=pchData;
GlobalUnlock(hClipboardData);
}
else
{
m_strNotExt.ShowWindow(SW_SHOW);
}
CloseClipboard();
UpdateData(FALSE);
}
}
初始化兩個(gè)編輯欄的內(nèi)容
CSimpleTextTransferDlg::CSimpleTextTransferDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSimpleTextTransferDlg::IDD, pParent)
,m_strLastUpdated("<Unknown>")
,m_strClipboardText("")
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
在OnInitDialog函數(shù)中添加DisplayClipboardData函數(shù)和SetClipboardViewer函數(shù)(每當(dāng)剪貼板的內(nèi)容發(fā)生變化時(shí),這個(gè)函數(shù)將窗口加入被通知的窗口鏈(通過WM_DRAWCLIPBOARD消息))
BOOL CSimpleTextTransferDlg::OnInitDialog()
{

DisplayClipboardData();
SetClipboardViewer();//每當(dāng)剪貼板的內(nèi)容發(fā)生變化時(shí),這個(gè)函數(shù)將窗口加入被通知的窗口鏈(通過WM_DRAWCLIPBOARD消息)。
return TRUE;
}
手工添加WM_DRAWCLIPBOARD事件,相關(guān)函數(shù)為OnClipboardChange。
afx_msg LRESULT OnClipboardChange(WPARAM,LPARAM);

ON_MESSAGE(WM_DRAWCLIPBOARD,OnClipboardChange)

LRESULT CSimpleTextTransferDlg::OnClipboardChange(WPARAM,LPARAM)


{
CTime time=CTime::GetCurrentTime();
m_strLastUpdated=time.Format("%a, %b %d, %Y -- %H:%M:%S");
DisplayClipboardData();
return 0L;
}
posted on 2009-07-24 10:55
The_Moment 閱讀(5361)
評論(2) 編輯 收藏 引用 所屬分類:
VC實(shí)踐