Use VC to program screenshot Screen

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.

---- The following uses VC to gradually introduce the implementation process in Windows95. 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 area
{
HDC hscrdc, hmemdc;
// Screen and memory device description table
Hbitmap, holdbitmap;
// Bitmap handle
Int NX, NY, nx2, ny2;
// Coordinates of the selected region
Int nwidth, nheight;
// Bitmap width and height
Int xscrn, yscrn;
// Screen resolution

// Make sure that the selected area is not empty.
If (isrectempty (lprect ))
Return NULL;
// Create a device description table for the screen
Hscrdc = createdc ("display", null );
// Create a compatible memory device description table for the screen device description table
Hmemdc = createcompatibledc (hscrdc );
// 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 compatible with the on-screen device description table
Hbitmap = createcompatiblebitmap
(Hscrdc, nwidth, nheight );
// Select the new bitmap to the memory device description table.
Holdbitmap = SelectObject (hmemdc, hbitmap );
// Copy the screen device description table to the memory device description table.
Bitblt (hmemdc, 0, 0, nwidth, nheight,
Hscrdc, NX, NY, srccopy );
// Obtain the handle of the screen bitmap.
Hbitmap = SelectObject (hmemdc, holdbitmap );
// Clear
Deletedc (hscrdc );
Deletedc (hmemdc );
// Returns the bitmap handle.
Return hbitmap;
}

After obtaining the screen bitmap handle, we
You 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 a disk file in bitmap format.

Int savebitmaptofile (hbitmap,
Lpstr lpfilename) // hbitmap is the screen bitmap handle.
{// Lpfilename is the bitmap file name.
HDC;
// Device description
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 size of the color palette. The pixel bytes in the bitmap,
Bitmap File size, number of bytes written to the file
DWORD dwpalettesize = 0,
Dwbmbitssize,
Dwdibsize, dwwritten;
Bitmap bitmap;
// Bitmap attribute Structure
Bitmapfileheader bmfhdr;
// Bitmap file header Structure
Bitmapinfoheader Bi;
// Bitmap header Structure
Lpbitmapinfoheader lpbi;
// Point to the bitmap information header Structure
Handle FH, hdib, hpal, holdpal = NULL;
// Defines the file, allocates memory handles, and color palette handles.

// 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 palette size
If (wbitcount <= 8)
Dwpalettesize = (1 <wbitcount )*
Sizeof (rgbquad );

// Set the bitmap information header Structure
GetObject (hbitmap, sizeof (Bitmap), (lpstr) & Bitmap );
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, dwbmbitssize +
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 under the color palette
Getdibits (HDC, hbitmap, 0, (uint) bitmap. bmheight,
(Lpstr) lpbi + sizeof (bitmapinfoheader)
+ Dwpalettesize,
(Bitmapinfoheader *)
Lpbi, dib_rgb_colors );
// Restore the palette
If (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;
// Set 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. bfoffbits = (DWORD) sizeof
(Bitmapfileheader)
+ (DWORD) sizeof (bitmapinfoheader)
+ Dwpalettesize;
// Write the bitmap file header
Writefile (FH, (lpstr) & bmfhdr, sizeof
(Bitmapfileheader), & dwwritten, null );
// Write the remaining contents 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.