如果窗口是隐藏的,但是我有它的窗口句柄和HDC可不可以把那个窗口的图抓下来?
代码如下:
CRect rect;
GetClientRect(&rect);
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
CBitmap *pOldBmp = MemDC.SelectObject(&bitmap);
int nRasterCapsScrn = GetDeviceCaps(pDC->m_hDC, RASTERCAPS);
int nHasPaletteScrn = nRasterCapsScrn & RC_PALETTE;
int nPaletteSizeScrn = GetDeviceCaps(pDC->m_hDC, SIZEPALETTE);
LOGPALETTE LogPal;
HPALETTE hPal, hOldPal;
if (nHasPaletteScrn && (nPaletteSizeScrn == 256))
{
LogPal.palVersion = 0x300;
LogPal.palNumEntries = 256;
GetSystemPaletteEntries(pDC->m_hDC, 0, 256, &(LogPal.palPalEntry[0]));
hPal = CreatePalette(&LogPal);
hOldPal = SelectPalette(MemDC.m_hDC, hPal, 0);
}
MemDC.BitBlt(0, 0, rect.Width(), rect.Height(), pDC, rect.left, rect.top, SRCCOPY);
MemDC.SelectObject(pOldBmp);
if (nHasPaletteScrn && (nPaletteSizeScrn == 256))
{
SelectPalette(MemDC.m_hDC, hOldPal, 0);
}
BITMAP btm;
bitmap.GetBitmap(&btm);
DWORD size=btm.bmWidthBytes*btm.bmHeight;
LPSTR lpData=(LPSTR)GlobalAlloc(GPTR, size);
/////////////////////////////////////////////
BITMAPINFOHEADER bih;
bih.biBitCount=btm.bmBitsPixel;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=btm.bmHeight;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=size;
bih.biWidth=btm.bmWidth;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
///////////////////////////////////
GetDIBits(MemDC.m_hDC,bitmap,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
//////////////////////////////
CString strFileName;
GetParentFrame()->GetWindowText(strFileName);
strFileName = "C:\\"+strFileName + ".BMP";
BITMAPFILEHEADER bfh;
bfh.bfReserved1=bfh.bfReserved2=0;
bfh.bfType=((WORD)('M'<< 8)|'B');
bfh.bfSize=54+size;
bfh.bfOffBits=54;
CFile file(strFileName, CFile::modeCreate|CFile::modeWrite);
file.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
file.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
file.WriteHuge(lpData,size);
file.Close();
GlobalFree(lpData);
--------------------------------------
用上面的代码我只能抓到顶层窗口的图形(上面的代码在定时器中),如果我切换到别的窗口,那么抓到的图形也就变成别的窗口了,各位兄弟姐妹们帮帮忙吧