screen capture of UI failed when minimized or other window overlaps

  • Thread starter Thread starter R_MWKwok
  • Start date Start date
R

R_MWKwok

Guest
Hello,


The following problem happens to my program on WinXP, and seems not in Win7. I want to solve it so the problem goes on WinXP.

My target is to capture an image of my user interface, and I "assembled" the following codes with what I found on web. However, it works only when my UI is showing on top of the screen (i.e. nothing else overlaps, and it is not minimized). Is there a way that I can guarantee a capture in all circumstances (i.e. other windows overlaps, minimized, etc)?


CDC * pDC = GetDC();

HDC hScreenDC = pDC->GetSafeHdc();
HDC hMyDC= CreateCompatibleDC(hScreenDC);

RECT rect;
GetWindowRect(&rect);
int iScreenWidth = rect.right - rect.left + 1 - 2*GetSystemMetrics(SM_CYFIXEDFRAME);
int iScreenHeight = rect.bottom - rect.top + 1 - GetSystemMetrics(SM_CYSIZE)- 2*GetSystemMetrics(SM_CXFIXEDFRAME)- GetSystemMetrics(SM_CYMENU);

int iBpi= GetDeviceCaps(hScreenDC ,BITSPIXEL);


BITMAPINFO info_bitmap;
BITMAPFILEHEADER Header;
memset(&info_bitmap, 0, sizeof(info_bitmap));
memset(&Header, 0, sizeof(Header));
info_bitmap.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info_bitmap.bmiHeader.biWidth = iScreenWidth;
info_bitmap.bmiHeader.biHeight = iScreenHeight ;
info_bitmap.bmiHeader.biPlanes = 1;
info_bitmap.bmiHeader.biBitCount = iBpi;
info_bitmap.bmiHeader.biCompression = BI_RGB;
Header.bfType = 0x4D42;
Header.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

void *data;
HBITMAP hBitmap = CreateDIBSection(hMyDC,&info_bitmap,DIB_RGB_COLORS,(void**)&data,0,0);
// Select it into your DC
SelectObject(hMyDC,hBitmap);

// Copy image from screen
BitBlt(hMyDC, 0, 0, iScreenWidth, iScreenHeight, hScreenDC , 0, 0, SRCCOPY);

CString tmp = path.Get_result_image_filepath(info);
std::fstream hFile(path.Get_result_image_filepath(info), std::ios::out | std::ios::binary);
if (hFile.is_open())
{
hFile.write((char*)&Header, sizeof(Header));
hFile.write((char*)&info_bitmap.bmiHeader, sizeof(info_bitmap.bmiHeader));
hFile.write((char*) data, (((iBpi * iScreenWidth + 31) & ~31) / 8) * iScreenHeight);
hFile.close();
DeleteObject(hBitmap);
}
hFile.close();
ReleaseDC(pDC);

Continue reading...
 
Back
Top