Windows core programming code analysis based on Visual C ++ (40) screen capture

Source: Internet
Author: User

Screen scraper is a type of software that allows a PC to intercept character-based data from a host and display it on a GUI that is easier to understand. The updated screen interceptor displays information in HTML, so it can be accessed in a browser. The key factor for distinguishing screen capture from conventional analysis is that the intercepted output is sent to humans in name rather than machine interpretation. There are many synonyms for screenshots: Data Capturing, data extraction, network capturing, web page packaging, and HTML capturing (the last four are used to capture web pages ).

 

We have all experienced the special functions of QQ. Next we will develop a function based on C ++.

See the code.

 

 

 

 

 

# Include <windows. h> handle transform (hbitmap bitmap, DWORD dwcompression, hpalette hpal, DWORD * sizeimage); bool capscreen (lptstr filename); // handle transform (hbitmap bitmap, DWORD dwcompression, hpalette hpal, DWORD * sizeimage) {bitmap bm; bitmapinfoheader Bi; lpbitmapinfoheader lpbi; DWORD dwlen; handle hdib; handle; HDC; // The bi_bitfields type if (dwcompression = bi_bitfields) retu is not supported Rn NULL; // if the color palette is empty, use the default color palette if (hpal = NULL) hpal = (hpalette) getstockobject (default_palette); // obtain the bitmap information GetObject (bitmap, sizeof (BM), (lpstr) & BM); // initialize the bitmap information header bi. bisize = sizeof (bitmapinfoheader); bi. biwidth = BM. bmwidth; bi. biheight = BM. bmheight; bi. biplanes = 1; bi. bibitcount = BM. bmplanes * BM. bmbitspixel; bi. bicompression = dwcompression; bi. bisizeimage = 0; bi. bixpelspermeter = 0; bi. biypelsper Meter = 0; bi. biclrused = 0; bi. biclrimportant = 0; // calculate the size of the Information header and the color table int ncolors = (1 <bi. bibitcount); If (ncolors> 256) ncolors = 0; dwlen = Bi. bisize + ncolors * sizeof (rgbquad); // we need a device context to get the dib from HDC = getdc (null); hpal = selectpalette (HDC, hpal, false ); realizepalette (HDC); // allocate memory hdib = globalalloc (gmem_fixed, dwlen) for the information header and color table; If (! Hdib) {selectpalette (HDC, hpal, false); releasedc (null, HDC); return NULL;} lpbi = (lpbitmapinfoheader) hdib; * lpbi = Bi; // call getdibits to calculate the image size. getdibits (HDC, bitmap, 0l, (DWORD) Bi. biheight, (lpbyte) null, (lpbitmapinfo) lpbi, (DWORD) dib_rgb_colors); Bi = * lpbi; // each line of the image is aligned with the (32bit) boundary if (Bi. bisizeimage = 0) {Bi. bisizeimage = (Bi. biwidth * bi. bibitcount) + 31 )&~ 31)/8) * bi. biheight; If (dwcompression! = Bi_rgb) Bi. bisizeimage = (Bi. bisizeimage * 3)/2;} // re-allocate the memory size to put down all data in dwlen + = Bi. bisizeimage; If (handle = globalrealloc (hdib, dwlen, gmem_moveable) hdib = handle; else {globalfree (hdib); // reselect the original palette selectpalette (HDC, hpal, false ); releasedc (null, HDC); return NULL;} // obtain bitmap data lpbi = (lpbitmapinfoheader) hdib; // The final Dib bool bgotbits = getdibits (HDC, bitmap, 0l, // at the beginning of the scanning line (DWORD) Bi. biheight, // Scan Number of rows (lpbyte) lpbi // bitmap data address + (Bi. bisize + ncolors * sizeof (rgbquad), (lpbitmapinfo) lpbi, // bitmap information address (DWORD) dib_rgb_colors); // color palette uses RGB if (! Bgotbits) {globalfree (hdib); selectpalette (HDC, hpal, false); releasedc (null, HDC); return NULL;} selectpalette (HDC, hpal, false ); releasedc (null, HDC); * sizeimage = Bi. bisizeimage; return hdib;} bool capscreen (lptstr filename) {DWORD sizeimage; HDC = createdc ("display", null); HDC compatiblehdc = createcompatibledc (HDC ); hbitmap BMP screen = convert (HDC, getdevicecaps (HDC, horzres), getdevicecaps (HDC, vertres); SelectObject (success, BMP screen); bitblt (compatiblehdc, getdevicecaps (HDC, horzres), getdevicecaps (HDC, vertres), HDC, 0,0, srccopy); handlepbitmapwithoutfileh = ddbtodib (BMP screen, bi_rgb, 0, & sizeimage); bitmapfileheader BFH; // sets the Bitmap header structure BFH. bftype = (Word) ('M' <8) | 'B'); BFH. bfreserved1 = 0; BFH. bfreserved2 = 0; BFH. bfsize = 54 + sizeimage; BFH. bfoffbits = 54; // create a bitmap file handle hfile = createfile (filename, generic_write, create_always, file_attribute_normal, 0); DWORD dwwrite; // write the bitmap file header writefile (hfile, & BFH, sizeof (bitmapfileheader), & dwwrite, null); // write the remaining content of the bitmap file writefile (hfile, pbitmapwithoutfileh, BFH. bfsize, & dwwrite, null); deletedc (HDC); closehandle (compatiblehdc); Return true ;} int main (void) {capscreen ("D: \ shorttop.bmp "); return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.