static BOOL PasteBitmap( /************************************************************************/ HWND hWindow, LPSTR lpImageFile, LPSTR lpMaskFile) { BOOL bRet; HBITMAP hBitmap; HPALETTE hPal; LPBITMAPINFOHEADER lpDIB; if ( !OpenClipboard( hWindow ) ) return( FALSE ); if ( !(hPal = (HPALETTE)GetClipboardData(CF_PALETTE)) ) hPal = lpBltScreen->hPal; if ( !(hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP)) ) { CloseClipboard(); return( NO ); } if ( !(lpDIB = BitmapToDIB( NULL/*hDC*/, hBitmap, hPal )) ) { CloseClipboard(); return( NO ); } bRet = WriteDIBClip( lpDIB, lpImageFile, lpMaskFile ); FreeUp( (LPTR)lpDIB ); CloseClipboard(); return( bRet ); }
HDIB FAR CopyScreenToDIB(LPRECT lpRect) { HBITMAP hBitmap; // handle to device-dependent bitmap HPALETTE hPalette; // handle to palette HDIB hDIB = NULL; // handle to DIB /* get the device-dependent bitmap in lpRect by calling * CopyScreenToBitmap and passing it the rectangle to grab */ hBitmap = CopyScreenToBitmap(lpRect); /* check for a valid bitmap handle */ if (!hBitmap) return NULL; /* get the current palette */ hPalette = GetSystemPalette(); /* convert the bitmap to a DIB */ hDIB = BitmapToDIB(hBitmap, hPalette); /* clean up */ DeleteObject(hPalette); DeleteObject(hBitmap); /* return handle to the packed-DIB */ return hDIB; }
LRESULT CDesktopDlg::OnLButtonDblClick(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if (m_rcSelect.left != m_rcSelect.right && m_rcSelect.top != m_rcSelect.bottom) { CDC dcMem; dcMem.CreateCompatibleDC(NULL); CBitmap bitmap; BITMAP bm; GetObject(m_hBmpDesktop, sizeof(bm), &bm); bm.bmWidth = m_rcSelect.Width(); bm.bmHeight = m_rcSelect.Height(); bitmap.CreateBitmapIndirect(&bm); HBITMAP hBmpOld = dcMem.SelectBitmap(bitmap.m_hBitmap); dcMem.StretchBlt(0,0, m_rcSelect.Width(), m_rcSelect.Height(), m_hDcMemory, m_rcSelect.left, m_rcSelect.top, m_rcSelect.Width(), m_rcSelect.Height(), SRCCOPY); HBITMAP hBmp = (HBITMAP)bitmap.Detach(); dcMem.SelectBitmap(hBmpOld); // save to the clipboard if(::OpenClipboard(m_hWnd)) { ::EmptyClipboard(); ::GdiFlush(); HANDLE hDib = NULL; HPALETTE ghPal = NULL; if (hBmp) { hDib = BitmapToDIB(hBmp, ghPal); if (hDib) { HANDLE hResult = ::SetClipboardData(CF_DIB, hDib); if (hResult == NULL) { // _ShowLastError(); } } else { MessageBeep(0); } } ::CloseClipboard(); //::EmptyClipboard(); //hDib = BitmapToDIB(m_hBitmap, ghPal); //::SetClipboardData(CF_BITMAP, hBmp); //::CloseClipboard(); } EndDialog(IDOK); } bHandled = FALSE; return 0; }
BOOL CaptureWindow(HWND hwndOwner, HWND hwnd) { RECT rect; HDC hdc, hdcMem, hdcOld; HBITMAP hBmp; HANDLE hDIB; HPALETTE hPal; int width, height; int RasterCapsScrn; int PaletteSizeScrn; GetWindowRect(hwnd, &rect); width = rect.right-rect.left; height = rect.bottom-rect.top; hdc = GetDC(0); hdcMem = CreateCompatibleDC(hdc); hBmp = CreateCompatibleBitmap(hdc, width, height); hdcOld = SelectObject(hdcMem, hBmp); //copy the screen contents BitBlt(hdcMem, 0, 0, width, height, hdc, rect.left, rect.top, SRCCOPY); SelectObject(hdcMem, hdcOld); OpenClipboard(hwndOwner); EmptyClipboard(); #ifdef SUPPORT_DIBS //palette detection RasterCapsScrn = GetDeviceCaps(hdc, RASTERCAPS); PaletteSizeScrn = GetDeviceCaps(hdc, SIZEPALETTE); if((RasterCapsScrn & RC_PALETTE) && (PaletteSizeScrn == 256)) hPal = GetSystemPalette(hdc); else hPal = 0; hDIB = BitmapToDIB(hBmp, hPal); SetClipboardData(CF_DIB, hDIB); #endif SetClipboardData(CF_BITMAP, hBmp); CloseClipboard(); ReleaseDC(0, hdc); return TRUE; }
void SaveWindowToDIB(HWND hWnd, int nBitCount, int nCompression) { HBITMAP hBmp = CaptureWindow(hWnd); if ( hBmp ) { BITMAPINFO * pDIB = BitmapToDIB(NULL, hBmp, nBitCount, nCompression); if ( pDIB ) { SaveDIBToFile(NULL, pDIB, NULL); delete [] (BYTE *) pDIB; } DeleteObject(hBmp); } }