现在的位置: 首页 > 综合 > 正文

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

2013年10月11日 ⁄ 综合 ⁄ 共 760字 ⁄ 字号 评论关闭

 

//拷贝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);
}


抱歉!评论已关闭.