HBITMAP CExampleDlg::CopyScrToBit(LPRECT lpRect) { HDC hScrDC, hMemDC; HBITMAP hOldBitmap; int nX, nY, nX2, nY2; int nWidth, nHeight; int xScrn, yScrn; if (IsRectEmpty(lpRect)) return NULL; hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL); hMemDC = CreateCompatibleDC(hScrDC); nX = lpRect->left; nY = lpRect->top; nX2 = lpRect->right; nY2 = lpRect->bottom; xScrn = GetDeviceCaps(hScrDC, HORZRES); yScrn = GetDeviceCaps(hScrDC, VERTRES); if (nX < 0) nX = 0; if (nY < 0) nY = 0; if (nX2 > xScrn) nX2 = xScrn; if (nY2 > yScrn) nY2 = yScrn; nWidth = nX2 - nX; nHeight = nY2 - nY; hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight); hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap); ShowWindow(SW_HIDE); BitBlt(hMemDC, 0, 0, nWidth, nHeight,hScrDC, nX, nY, SRCCOPY); hBitmap =(HBITMAP)SelectObject(hMemDC, hOldBitmap); DeleteDC(hScrDC); DeleteDC(hMemDC); ShowWindow(SW_SHOW); return hBitmap; } void CExampleDlg::OnBtnCopyscreen() { // TODO: Add your control notification handler code here CRect rc; GetDesktopWindow()->GetWindowRect(&rc); hBitmap=CopyScrToBit(&rc); } void CExampleDlg::OnBtnClipboard() { // TODO: Add your control notification handler code here if (OpenClipboard()) { EmptyClipboard(); SetClipboardData(CF_BITMAP, hBitmap); CloseClipboard(); } }
2.BMP 显示
CPaintDC dc(this); // device context for painting //获得窗口大小 CRect r; GetClientRect(&r); //填充背景 HBRUSH brush = (HBRUSH)::GetStockObject(GRAY_BRUSH); ::FillRect(dc.m_hDC,&r,brush); dc.SetStretchBltMode(HALFTONE); //将位图选进设备场景中 CDC memdc; memdc.CreateCompatibleDC( &dc ); CBitmap* pOldBmp= memdc.SelectObject(&m_bmpImage); //获得位图参数 BITMAP bmp; m_bmpImage.GetBitmap(&bmp); //开始缩放 switch(m_nStrechType) { case Type_1_1://1:1 dc.BitBlt(r.left,r.top,r.Width(),r.Height(),&memdc,0,0,SRCCOPY); break; case Type_FULL_X://按宽度 dc.StretchBlt(r.left,r.top,r.Width(),bmp.bmHeight,&memdc,0,0, bmp.bmWidth,bmp.bmHeight,SRCCOPY); break; case Type_FULL_Y://按高度 dc.StretchBlt(r.left,r.top,bmp.bmWidth,r.Height(),&memdc,0,0, bmp.bmWidth,bmp.bmHeight,SRCCOPY); break; case Type_FULL://充满窗口 dc.StretchBlt(r.left,r.top,r.Width(),r.Height(),&memdc,0,0, bmp.bmWidth,bmp.bmHeight,SRCCOPY); break; case Type_CENTER://居中显示,大小为窗口的一半 dc.StretchBlt(r.Width()/4,r.Height()/4,r.Width()/2,r.Height()/2,&memdc,0,0, bmp.bmWidth,bmp.bmHeight,SRCCOPY); break; } //还原 memdc.SelectObject(pOldBmp);