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

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)轉移位圖圖像
//按Alt+Print Screen組合鍵,可以把當前窗口的位圖圖像復制到剪貼板上
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按鈕事件,吧位圖圖像復制到剪貼板上
{
// 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)//進行GDI調用進行做圖
{
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)轉移自定義數據
在把自定義數據復制到剪貼板上之前,首先要把該數據注冊為Windows可以識別的格式。UINT RegisterClipboardFormat(LPCTSTR lpszFormat),lpszFormat是要被注冊的格式的名稱,該名稱可以用任何合法的字符串來表示。
把自定義數據復制到剪貼板上首先,在頭文件中添加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();
}
}
}
從剪貼板上粘貼自定義數據
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)接收剪貼板上內容已被修改的通知消息
添加DispalyClipboardData函數來顯示剪貼板中的內容
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);
}
}
初始化兩個編輯欄的內容
CSimpleTextTransferDlg::CSimpleTextTransferDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSimpleTextTransferDlg::IDD, pParent)
,m_strLastUpdated("<Unknown>")
,m_strClipboardText("")
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
在OnInitDialog函數中添加DisplayClipboardData函數和SetClipboardViewer函數(每當剪貼板的內容發生變化時,這個函數將窗口加入被通知的窗口鏈(通過WM_DRAWCLIPBOARD消息))
BOOL CSimpleTextTransferDlg::OnInitDialog()
{

DisplayClipboardData();
SetClipboardViewer();//每當剪貼板的內容發生變化時,這個函數將窗口加入被通知的窗口鏈(通過WM_DRAWCLIPBOARD消息)。
return TRUE;
}
手工添加WM_DRAWCLIPBOARD事件,相關函數為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實踐