Screen capture programming with VC cool

Source: Internet
Author: User

----Screenshots are interesting. although there are many applications such as hypersnap that can be used to capture your favorite screen image, if you can add this function to your program, you can use it more effectively.

----Next we will use VC to gradually introduce the implementation process in Windows 95. first, we need to determine the area of the screenshot and define it using the lprect structure. you can intercept a window or the entire screen. the following code copies the selected screen area in place.

Hbitmap copyscreentobitmap (lprect) // lprect indicates the selected region {HDC hscrdc, hmemdc; // hbitmap, holdbitmap, and int nx, NY, nx2, ny2; // The Int nwidth and nheight coordinates of the selected region; // The bitmap width and height int xscrn and yscrn; // screen resolution // ensure that the selected area is not empty. If (isrectempty (lprect) return NULL; // create a device description table for the screen. hscrdc = createdc ("display ", null, null, null); // create a compatible memory device description table hmemdc = createcompatibledc (hscrdc) for the screen device description table ); // obtain the coordinates of the selected region Nx = lprect-> left; ny = lprect-> top; nx2 = lprect-> right; ny2 = lprect-> bottom; // obtain the screen resolution xscrn = getdevicecaps (hscrdc, horzres); yscrn = getdevicecaps (hscrdc, vertres); // make sure that the selected area is visible if (nx <0) nx = 0; If (NY <0) ny = 0; If (nx2> xscrn) nx2 = xscrn; If (ny2> yscrn) ny2 = yscrn; nwidth = nx2-NX; nheight = ny2-NY; // create a bitmap hbitmap = createcompatiblebitmap (hscrdc, nwidth, nheight) compatible with the on-screen device description table ); // select the new bitmap in the memory device description table holdbitmap = SelectObject (hmemdc, hbitmap); // copy the screen device description table to the bitblt (hmemdc, 0, 0, 0, nwidth, nheight, hscrdc, NX, NY, srccopy); // obtain the screen bitmap handle hbitmap = SelectObject (hmemdc, holdbitmap); // clear deletedc (hscrdc ); deletedc (hmemdc); // return the bitmap handle return hbitmap;} after obtaining the bitmap handle, we can paste the screen content to the clipboard. if (openclipboard (hwnd) // hwnd is the program window handle {// clear the clipboard emptyclipboard (); // paste the screen content to the clipboard, hbitmap is the screen bitmap handle setclipboarddata (cf_bitmap, hbitmap); // close the clipboard closeclipboard ();} We can also save the screen content to the disk file in bitmap format. int savebitmaptofile (hbitmap, lpstr lpfilename) // hbitmap is the screen bitmap handle. {// lpfilename is the bitmap file name HDC; // The device description table int ibits; // The number of bytes occupied by each pixel in the current display resolution word wbitcount; // The number of bytes occupied by each pixel in the bitmap // defines the color palette size, pixel byte size in the bitmap, and bitmap file size, number of bytes written to the file DWORD dwpalettesize = 0, dwbmbitssize, dwdibsize, dwwritten; Bitmap bitmap; // bitmapfileheader bmfhdr; // bitmapinfoheader bi; // bitmap information header structure lpbitmapinfoheader lpbi; // point to the bitmap information header structure handle FH, hdib, hpal, holdpal = NULL; // defines the file, allocates a memory handle, palette handle // calculate the number of bytes occupied by each pixel in a bitmap file. HDC = createdc ("display", null); ibits = getdevicecaps (HDC, bitspixel) * getdevicecaps (HDC, planes); deletedc (HDC); If (ibits <= 1) wbitcount = 1; else if (ibits <= 4) wbitcount = 4; else if (ibits <= 8) wbitcount = 8; else if (ibits <= 24) wbitcount = 24; // calculate the color palette size if (wbitcount <= 8) dwpalettesize = (1 <wbitcount) * sizeof (rgbquad); // you can specify the Bitmap header. GetObject (hbitmap, sizeof (Bitmap), (lpstr) & Bitmap), AND Bi. bisize = sizeof (bitmapinfoheader); bi. biwidth = bitmap. bmwidth; bi. biheight = bitmap. bmheight; bi. biplanes = 1; bi. bibitcount = wbitcount; bi. bicompression = bi_rgb; bi. bisizeimage = 0; bi. bixpelspermeter = 0; bi. biypelspermeter = 0; bi. biclrused = 0; bi. biclrimportant = 0; dwbmbitssize = (bitmap. bmwidth * wbitcount + 31)/32) * 4 * bitmap. bmheight; // allocate memory for bitmap content hdib = globalalloc (ghnd, signature + dwpalettesize + sizeof (bitmapinfoheader); lpbi = (lpbitmapinfoheader) globallock (hdib); * lpbi = Bi; // process the palette hpal = getstockobject (default_palette); If (hpal) {HDC = getdc (null); holdpal = selectpalette (HDC, hpal, false); realizepalette (HDC );} // obtain the new pixel value getdibits (HDC, hbitmap, 0, (uint) Bitmap Under the color palette. bmheight, (lpstr) lpbi + sizeof (bitmapinfoheader) + dwpalettesize, (bitmapinfoheader *) lpbi, dib_rgb_colors); // restore if palette (holdpal) {selectpalette (HDC, holdpal, true); realizepalette (HDC); releasedc (null, HDC);} // create a bitmap file FH = createfile (lpfilename, generic_write, 0, null, create_always, file_attribute_normal | file _ flag_sequential_scan, null); If (FH = invalid_handle_value) return false; // sets the bitmap file header bmfhdr. bftype = 0x4d42; // "BM" dwdibsize = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + dwpalettesize + dwbmbitssize; bmfhdr. bfsize = dwdibsize; bmfhdr. bfreserved1 = 0; bmfhdr. bfreserved2 = 0; bmfhdr. bytes = (DWORD) sizeof (bitmapfileheader) + (DWORD) sizeof (bitmapinfoheader) + dwpalettesize; // write bitmap file header writefile (FH, (lpstr) & bmfhdr, sizeof (bitmapfileheader ), & dwwritten, null); // write the remaining content of the bitmap file writefile (FH, (lpstr) lpbi, dwdibsize, & dwwritten, null); // clear globalunlock (hdib ); globalfree (hdib); closehandle (FH );}

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.