C screen capture code
Last Update:2018-12-04
Source: Internet
Author: User
Void capturescreen ()
...{
// Obtain the screen width
Int nscreenwidth = getsystemmetrics (sm_cxscreen );
// Obtain the screen height
Int nscreenheight = getsystemmetrics (sm_cyscreen );
// Obtain the desktop window handle
Hwnd hsf-topwnd = getw.topwindow ();
// Obtain the desktop window device Environment
HDC hsf-topdc = getdc (hsf-topwnd );
// Generate the compatible device environment of the full screen device description table
HDC hcapturedc = createcompatibledc (hsf-topdc );
// Generate a compatible bitmap for the full screen device description table
Hbitmap hcapturebitmap = createcompatiblebitmap (hsf-topdc, nscreenwidth, nscreenheight );
// Select the compatible bitmap into the compatible device Environment
SelectObject (hcapturedc, hcapturebitmap );
// Copy the pixel data of the full screen window bitmap to the compatible device description table.
Bitblt (hcapturedc, 0, 0, nscreenwidth, nscreenheight, hsf-topdc, 0, 0, srccopy );
Bitmapinfo Bi;
Void * pbits = NULL;
Zeromemory (& BI, sizeof (BI ));
Bi. bmiheader. bisize = sizeof (Bi. bmiheader );
Bi. bmiheader. biheight = nscreenheight;
Bi. bmiheader. biwidth = nscreenwidth;
Bi. bmiheader. biplanes = 1;
Bi. bmiheader. bibitcount = 24;
Bi. bmiheader. bicompression = bi_rgb;
Bi. bmiheader. bisizeimage = 3 * nscreenheight * nscreenwidth;
// Generate a bitmap-compatible device description table
HDC hbmpfiledc = createcompatibledc (hsf-topdc );
// Generate Bitmap-compatible bitmap
Hbitmap hbmpfilebitmap = createdibsection (hsf-topdc, & BI, dib_rgb_colors, & pbits, null, 0 );
// Select the compatible bitmap and the device description table.
SelectObject (hbmpfiledc, hbmpfilebitmap );
// Copy the captured bitmap pixels to the device environment of the image.
Bitblt (hbmpfiledc, nscreenwidth, nscreenheight, hcapturedc, srccopy );
// Create a file
Handle hfile = createfile ("111.bmp", generic_write, file_pai_write, null, create_always, file_attribute_normal, null );
// Save the bitmap file
If (hfile! = Invalid_handle_value)
...{
DWORD dwret = 0;
Bitmapfileheader bmfheader;
Zeromemory (& bmfheader, sizeof (bmfheader ));
Bmfheader. bfoffbits = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader );
Bmfheader. bfsize = Bi. bmiheader. bisizeimage + bmfheader. bfoffbits;
Bmfheader. bftype = 'mb ';
// Write the Bitmap header.
Writefile (hfile, & bmfheader, sizeof (bmfheader), & dwret, null );
// Write bitmap size, color table, and other information
Writefile (hfile, & BI. bmiheader, sizeof (Bi. bmiheader), & dwret, null );
// Write bitmap data blocks
Writefile (hfile, pbits, Bi. bmiheader. bisizeimage, & dwret, null );
Closehandle (hfile );
}
Deletedc (hbmpfiledc );
Deleteobject (hbmpfilebitmap );
Releasedc (hsf-topwnd, hsf-topdc );
Deletedc (hcapturedc );
Deleteobject (hcapturebitmap );
}