Hi,
I'd like to screengrab a window even when it is not visible but can't work out how to do it and I wonder if someone can help me.
The function below grabs the contents of the window ok if I set it as the foreground window first with a very short delay of a couple of hundred ms to ensure it has been drawn, but this feels very much like a fudge.
Thanks
Code:void CaptureWindow ( HWND hwnd ) { RECT rect; HDC src_hdc; HDC dst_hdc; HBITMAP hbitmap; BITMAPINFO bi; LPBYTE pBits; int w; int h; HANDLE file_handle; GetClientRect ( hwnd, &rect ); w = rect.right - rect.left; h = rect.bottom - rect.top; src_hdc = GetDC ( hwnd ); dst_hdc = CreateCompatibleDC ( src_hdc ); ZeroMemory ( &bi, sizeof ( bi ) ); bi.bmiHeader.biSize = sizeof ( bi.bmiHeader ); bi.bmiHeader.biHeight = h; bi.bmiHeader.biWidth = w; bi.bmiHeader.biPlanes = 1; bi.bmiHeader.biBitCount = 24; bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biSizeImage = ((w * bi.bmiHeader.biBitCount +31)& ~31) / 8 * h; hbitmap = CreateDIBSection ( src_hdc, &bi, DIB_RGB_COLORS, (LPVOID *) &pBits, NULL, 0 ); SelectObject ( dst_hdc, hbitmap ); if ( BitBlt ( dst_hdc, 0, 0, w, h, src_hdc, rect.left, rect.top, SRCCOPY ) ) { file_handle = CreateFile ( "grab.bmp", GENERIC_WRITE, FILE_SHARE_WRITE, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if ( file_handle != INVALID_HANDLE_VALUE ) { DWORD dwRet = 0; BITMAPFILEHEADER bfh; ZeroMemory ( &bfh, sizeof ( bfh ) ); bfh.bfType = 0x4D42; bfh.bfOffBits = sizeof ( BITMAPFILEHEADER ) + sizeof ( BITMAPINFOHEADER ); bfh.bfSize = bi.bmiHeader.biSizeImage + bfh.bfOffBits; WriteFile ( file_handle, &bfh, sizeof ( bfh ), &dwRet, NULL); WriteFile ( file_handle, &bi.bmiHeader, sizeof ( bi.bmiHeader ), &dwRet, NULL ); WriteFile ( file_handle, pBits, bi.bmiHeader.biSizeImage, &dwRet, NULL ); } CloseHandle ( file_handle ); DeleteDC ( dst_hdc ); DeleteObject ( hbitmap ); ReleaseDC ( hwnd, src_hdc ); } }



LinkBack URL
About LinkBacks


