① vc++ mfc 鍓璐存澘鎿嶄綔鏁欑▼
鏈鏂囦富瑕佷粙緇嶄簡VC++/MFC涓濡備笅鍐呭圭殑鍓璐存澘鎿嶄綔錛
1銆佹枃鏈鍐呭圭殑鎿嶄綔
2銆乄MF鏁版嵁鐨勬搷浣
3銆佷綅鍥劇殑鎿嶄綔
4銆佽劇疆浣跨敤鑷瀹氫箟鏍煎紡
5銆佹劅鐭ュ壀璐存澘鍐呭圭殑鏀瑰彉
6銆佽嚜鍔ㄥ皢鏁版嵁綺樿創鍒板彟涓搴旂敤紼嬪簭紿楀彛
涓銆佹枃鏈鍐呭圭殑鎿嶄綔
涓嬮潰鐨勪唬鐮佺ず鑼冧簡濡備綍灝嗘枃鏈鍐呭瑰嶅埗鍒板壀璐存澘(Unicode緙栫爜鐨勫厛杞鍖栦負ASCII)錛
CString source;
//鏂囨湰鍐呭逛繚瀛樺湪source鍙橀噺涓
if( OpenClipboard() )
{
HGLOBAL clipbuffer;
char * buffer;
EmptyClipboard();
clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.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();
}
浜屻乄MF鏁版嵁鐨勬搷浣
鍦ㄥ壀璐存澘涓婅誨啓鍥捐薄鏁版嵁鏄闈炲父鏈夌敤鐨勫姛鑳斤紝騫朵笖瀹炵幇璧鋒潵涔熷緢綆鍗曘備笅闈㈢殑浠g爜鏄劇ず浜嗗備綍灝嗘墿灞曞浘鍏冩枃浠跺嶅埗鍒板壀璐存澘錛
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())
{
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.SelectObje
ct(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();
}
鍥涖佽劇疆騫朵嬌鐢ㄨ嚜瀹氫箟鏍煎紡
浣跨敤RegisterClipboardFormat()鍑芥暟錛屽彲浠ュ嶅埗鍜岀矘璐翠換浣曚綘闇瑕佺殑鏁版嵁綾誨瀷銆傛瘮濡傛垜浠鏈変互涓嬩竴涓鏁版嵁綾誨瀷錛
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();
}
璇誨彇鏁版嵁浣跨敤浠ヤ笅浠g爜錛
UINT format = RegisterClipboardFormat("MY_CUSTOM_FORMAT");
MyFormatData data;
if(Openclipboard())
{
HANDLE hData =GetClipboardData(format);
MyFormatData * buffer = (MyFormatData*)GlobalLock(hData);
data = *buffer;
GlobalUnlock(hData);
CloseClipboard();
}
浜斻佹劅鐭ュ壀璐存澘鍐呭圭殑鏀瑰彉
閫氳繃Windows娑堟伅鍙浠ユ劅鐭ュ壀璐存澘鍐呭規槸鍚﹀彂鐢熸敼鍙橈紝浠g爜濡備笅錛
//In your initialization code call:
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();
}
鍏銆佽嚜鍔ㄥ皢鏁版嵁綺樿創鍒板彟涓搴旂敤紼嬪簭紿楀彛
鍙闇鑾峰緱鐩稿簲紿楀彛鐨勫彞鏌勶紝騫跺彂閫佷竴涓娑堟伅灝卞彲浠ヤ簡