bool printText(bmpImage & image, HDC dc, HFONT font, DWORD color, DWORD x, DWORD y, const char *text) { bool res = false; if (image.isValid() && dc!=NULL && text != NULL && strlen(text)>0) { HDC memDC = CreateCompatibleDC(dc); if (memDC != 0) { HBITMAP bmDefault = CreateCompatibleBitmap(dc, image.getWidth(), image.getHeight()); if (bmDefault != 0) { SelectObject(memDC, bmDefault); drawImage(memDC, image, 0,0); int oldMOde = SetBkMode(memDC, 1); HFONT oldFont = (HFONT)SelectObject(memDC, font); SetTextColor(memDC, color); ::TextOutA(memDC, x, y, text, (int)strlen(text)); ::SelectObject(memDC, oldFont); ::SetBkMode(memDC, oldMOde); ::SelectObject(dc, bmDefault); if (copyFromBitmap(memDC,image,bmDefault)) { res = true; #if TEST { drawImage(dc, image, 10,10); } #endif } DeleteObject(bmDefault); } DeleteDC(memDC); } } return res; }
BOOL fipWinImage::captureWindow(HWND hWndApplicationWindow, HWND hWndSelectedWindow) { int xScreen, yScreen, xshift, yshift; RECT r; // Get window size GetWindowRect(hWndSelectedWindow, &r); // Check if the window is out of the screen or maximixed xshift = 0; yshift = 0; xScreen = GetSystemMetrics(SM_CXSCREEN); yScreen = GetSystemMetrics(SM_CYSCREEN); if(r.right > xScreen) r.right = xScreen; if(r.bottom > yScreen) r.bottom = yScreen; if(r.left < 0) { xshift = -r.left; r.left = 0; } if(r.top < 0){ yshift = -r.top; r.top = 0; } int width = r.right - r.left; int height = r.bottom - r.top; if(width <= 0 || height <= 0) return FALSE; // Hide the application window. ShowWindow(hWndApplicationWindow, SW_HIDE); // Bring the window at the top most level SetWindowPos(hWndSelectedWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE); // Give enough time to refresh the window Sleep(500); // Prepare the DCs HDC dstDC = GetDC(NULL); HDC srcDC = GetWindowDC(hWndSelectedWindow); // full window (GetDC(hWndSelectedWindow) = clientarea) HDC memDC = CreateCompatibleDC(dstDC); // Copy the screen to the bitmap HBITMAP bm = CreateCompatibleBitmap(dstDC, width, height); HBITMAP oldbm = (HBITMAP)SelectObject(memDC, bm); BitBlt(memDC, 0, 0, width, height, srcDC, xshift, yshift, SRCCOPY); // Redraw the application window. ShowWindow(hWndApplicationWindow, SW_SHOW); // Restore the position SetWindowPos(hWndSelectedWindow, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE); SetWindowPos(hWndApplicationWindow, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE); // Convert the HBITMAP to a FIBITMAP copyFromBitmap(bm); // Free objects DeleteObject(SelectObject(memDC, oldbm)); DeleteDC(memDC); // Convert 32-bit images to 24-bit if(getBitsPerPixel() == 32) { convertTo24Bits(); } return TRUE; }