码迷,mamicode.com
首页 > Windows程序 > 详细

Windows剪贴板操作简单小例

时间:2017-08-06 11:03:51      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:param   his   call   ring   opencl   data-   size   清空   操作   

1、复制文字到剪贴板

CString strText = L"须要拷贝到剪贴板的文字";
if ( ::OpenClipboard(m_hWnd) )
{
	if ( ::EmptyClipboard() )//清空剪贴板内数据
	{
		int nSize = strText.GetLength()*2+1;//这里是字节数,UNICODE编码每一个字符占2个字节,后面加上一个\0结尾符.
		HGLOBAL hMem = ::GlobalAlloc(GHND, nSize);	
		byte* pData=(byte*)::GlobalLock(hMem);
		memcpy_s(pData, nSize, strData.GetData(), nSize-1);
		pData[nSize-1]='\0';
		::GlobalUnlock(hMem);
		::SetClipboardData(CF_UNICODETEXT, hMem);
		::CloseClipboard();
		::GlobalFree(hMem);
	}
}

2、复制位图句柄到剪贴板

//保存截屏区域位图到剪切板
if( ::OpenClipboard(m_hWnd) )
{
	if( ::EmptyClipboard() )
	{
		HBITMAP hBmp = GetBitmapRect(g_hScreenBmp, m_rect);
		::SetClipboardData(CF_BITMAP, hBmp);
		DeleteObject(hBmp);
		::CloseClipboard();
	}
}
3、总的来说,你必须依照这个顺序来操作剪贴板:open ---- empty ---- setdata ----- close。(打开-清空-设置-关闭)

很多其它具体參数介绍能够參考MSDN说明

Remarks

OpenClipboard fails if another window has the clipboard open. 

An application should call the CloseClipboard function after every successful call to OpenClipboard. 

The window identified by the hWndNewOwner parameter does not become the clipboard owner unless the EmptyClipboard function is called. 

If an application calls OpenClipboard with hwnd set to NULL, EmptyClipboard sets the clipboard owner to NULL; this causes SetClipboardData to fail.



Windows剪贴板操作简单小例

标签:param   his   call   ring   opencl   data-   size   清空   操作   

原文地址:http://www.cnblogs.com/slgkaifa/p/7293840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!