拷贝pWnd指向的窗口的指定区域到剪贴板

 
//拷贝pWnd指向的窗口的lpSrcRect到剪贴板
//运行过此程序,可以打开画图进行粘贴测试
CopyScreenToClipboard(CWnd *pWnd, CRect *lpSrcRect)
{
	if(!IsWindow(pWnd->GetSafeHwnd()))
	{
		AfxMessageBox("窗口句柄无效");
		return (FALSE);
	}

	// first, determine the update region and select it
	if ( ! pWnd->OpenClipboard() )
	{
		AfxMessageBox( "无法打开剪贴板" );
		return (FALSE);
	}

	// Remove the current Clipboard contents
	if( ! EmptyClipboard() )
	{
		AfxMessageBox( "清空剪贴板失败" );
		return (FALSE);
	}

	//
	int   Width = lpSrcRect->Width();   
	int   Height = lpSrcRect->Height();

	CDC *pDC = pWnd->GetDC();

	//创建内存DC
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);

	//创建位图
	CBitmap m_Bitmap;
	m_Bitmap.CreateCompatibleBitmap(pDC,Width,Height);

	//选中位图
	memDC.SelectObject(&m_Bitmap);
	//拷贝
	memDC.BitBlt(0, 0, Width, Height,
		pDC, 0, 0,
		SRCCOPY);

	//拷贝至剪贴板
	SetClipboardData(CF_BITMAP,m_Bitmap.m_hObject);

	//关闭剪贴板
	CloseClipboard();

	return (TRUE);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值