// 拷贝屏幕 HBITMAP Cs1Dlg::CopyScreenToBitmap(int & nWidth, int & nHeight) { HDC hScrDC,hMemDC; HBITMAP hBitmap,hOldBitmap; int xScrn,yScrn; hScrDC=CreateDC(_T("display"),NULL,NULL,NULL); hMemDC=CreateCompatibleDC(hScrDC); xScrn=GetDeviceCaps(hScrDC,HORZRES); yScrn=GetDeviceCaps(hScrDC,VERTRES); nWidth=xScrn; nHeight=yScrn; hBitmap=CreateCompatibleBitmap(hScrDC,xScrn,yScrn); hOldBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap); BitBlt(hMemDC,0,0,xScrn,yScrn,hScrDC,0,0,SRCCOPY); hBitmap=(HBITMAP)SelectObject(hMemDC,hOldBitmap); DeleteDC(hScrDC); DeleteDC(hMemDC); return HBITMAP(); } void Cs1Dlg::OnBnClickedButton2() { // TODO: 在此添加控件通知处理程序代码 HBITMAP bmp; int nWidth,nHeight; CPen *pOldPen=NULL; HBITMAP hOldBmp=NULL; bmp=CopyScreenToBitmap (nWidth,nHeight); CDC *pDC=new CClientDC(this); CDC srcDC; CRect rect; this->GetClientRect (&rect); rect.DeflateRect (0,20); CPen pen; pen.CreatePen (PS_SOLID,2,RGB(0,255,255)); pOldPen =pDC->SelectObject(&pen); pDC->Rectangle(rect); srcDC.CreateCompatibleDC (pDC); hOldBmp=(HBITMAP)srcDC.SelectObject(bmp); pDC->StretchBlt(rect.left +2,rect.top +2,rect.right -rect.left -4,rect.bottom -rect.top -4,&srcDC,0,0,nWidth,nHeight,SRCCOPY); pDC->SelectObject (pOldPen); pDC->SelectObject (hOldBmp); pen.DeleteObject (); DeleteObject(bmp); srcDC.DeleteDC (); delete pDC; pDC=NULL; }