HWND hwnd = ::FindWindow(NULL, "RobotSoccer"); if (hwnd == NULL) return;
// get window size RECT r; ::GetWindowRect(hwnd,&r);
int xScreen,yScreen; //check if the window is out of the screen or maximixed <Qiang> int 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; }
SIZE sz; sz.cx = r.right - r.left; sz.cy = r.bottom - r.top;
if(sz.cx <= 0 || sz.cy <= 0) return;
// bring the window at the top most level ::SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
// prepare the DCs HDC dstDC = ::GetDC(NULL); HDC srcDC = ::GetWindowDC(hwnd); //full window (::GetDC(hwnd); = clientarea) HDC memDC = ::CreateCompatibleDC(dstDC);
// copy the screen to the bitmap HBITMAP bm =::CreateCompatibleBitmap(dstDC, sz.cx, sz.cy); HBITMAP oldbm = (HBITMAP)::SelectObject(memDC,bm); ::BitBlt(memDC, 0, 0, sz.cx, sz.cy, srcDC, xshift, yshift, SRCCOPY);
// restore the position ::SetWindowPos(hwnd,HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
CxImage *newima = new CxImage(); newima->CreateFromHBITMAP(bm); char strFileName[10]; sprintf(strFileName, "c:\\%d.jpg", m_nTimer/10); newima->Save(strFileName, CXIMAGE_FORMAT_JPG); // free objects DeleteObject(SelectObject(memDC,oldbm)); DeleteObject(memDC); |