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; }
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; }