Example code for more than 100 years ago, put it out to calculate
/ / Prerequisite #include
// Each line data is aligned to four bytes, the following definition is only used for 24-bit color DIB # define _widthpitch (w) ((w w w 3) & 0xfffffffc) // Function declaration HbitMap WinApi CaptureScreen LPCRect PrtRect); BOOL WINAPI SaveBitmap (Hbitmap Hbitmap, LPCSTR PTZFileName); // Program Inlet ) VOID WINAPI EntryPoint () {ExitProcess (WinMain (GetModuleHandle (NULL), NULL, GetCommandLine (), SW_SHOWNORMAL));} #endif // _DEBUG // main function INT WINAPI WinMain (hINSTANCE hInstance, hINSTANCE hPrevInstance, PSTR pszCmdLine, INT ICMDSHOW) {hbitmap hbitmap; hbitmap = captureScreen (null); if (hbitmap) {if (SaveBitmap (HbitMap, "C: //capturescreen.bmp")) {deleteObject (hbitmap); MessageBox (NULL, "screen image Save C: //capturescreen.bmp "," Complete ", MB_ICONITION; RETURN 0;} deleteObject (hbitMap);} MessageBox (null," I don't know what error:) ... "," wrong ", MB_ICONSTOP); Return -1;} // Capture the window window rectangular area.
prtRect a rectangular area to be captured, and if NULL, the entire window capture HBITMAP WINAPI CaptureScreen (LPCRECT prtRect) {INT x; INT y; HDC hdcSrc; HDC hdcMem; INT iWidth; INT iHeight; HBITMAP hbmpTemp; HBITMAP hbmpReturn; // Create screen device scene hdcsrc = createdc (Text ("Display"), NULL, NULL, NULL); // Set capture rectangular IF (prTRect == null) {x = 0; y = 0; iWidth = getDeviceCaps (HDCSRC, Horzres ); Iheight = getDevicecaps (hdcsrc, vertres);} else {x = prtRect-> left; y = prtRect-> top; iWidth = prtRect-> Right - prTRect-> left; Iheight = prtRect-> bottom - prTRect-> top;} // Create a memory device context compatible hdcMem = CreateCompatibleDC (hdcSrc); hbmpReturn = CreateCompatibleBitmap (hdcSrc, iWidth, iHeight); // transmitting bitmap hbmpTemp = (HBITMAP) SelectObject (hdcMem, hbmpReturn); BitBlt (hdcMem, 0 , 0, iWidth, Iheight, HDCSRC, X, Y, Srccopy; SelectObject (HDCMEM, HBMPTEMP); // Delete device scene deletedc (hdcmem); deletedc (hdcsrc); return hbmpreturn;} // Save screen partial map 24-bit color bitmap file Bool WinAPI SaveBitmap (Hbitmap Hbitmap, LPCSTMAP HBitmap, LPCSTR PTZFILENAME) {HDC HDC; DWORD dwSize; BOOL bReturn; LPBYTE pBits; HANDLE hFile; BITMAP bmBitmap; BITMAPFILEHEADER bfhFile; BITMAPINFOHEADER bihInfo; // return value is initialized to FALSE bReturn = FALSE; // made compatible bitmap screen object information if (GetObject (hBitmap, sizeof ( BITMAP), & bmBitmap)) {// set BITMAPINFOHEADER structure bihInfo.biSize = sizeof (BITMAPINFOHEADER); bihInfo.biWidth = bmBitmap.bmWidth; bihInfo.biHeight = bmBitmap.bmHeight; bihInfo.biPlanes = 1; bihInfo.biBitCount = 24; bihInfo .bicompression = bi_rgb; bihinfo.bisizeImage = _widthpitch (bmbitmap.bmwidth) * bmbitmap.bmheight; bihinfo.bixpelspermeter = 0;