① 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();
}
鍏銆佽嚜鍔ㄥ皢鏁版嵁绮樿创鍒板彟涓搴旂敤绋嬪簭绐楀彛
鍙闇鑾峰緱鐩稿簲绐楀彛鐨勫彞鏌勶紝骞跺彂閫佷竴涓娑堟伅灏卞彲浠ヤ簡