The keyboard has the print screen key.
This key is simulated by each program.
Save the image to the clipboard.
Then the program accesses the clipboard,
Save it to a file.
You can also directly call winapi to copy the interface to the clipboard.
API timing + screenshot!
Windows GDI
(1) screenshots
Capturing an image
You can use a bitmap to capture an image, and you can store the captured image in memory, display it at a different location in your application //'s window, or display it in another window.
In some cases, you may want your application to capture images and store them only temporarily. for example, when you scale or Zoom a picture created in a drawing application, the application must temporarily Save the normal view of the image and display the zoomed view. later, when the user selects the normal view, the application must replace the zoomed image with a copy of the normal view that it temporarily saved.
To store an image temporarily, your application must call createcompatibledc to create a DC that is compatible with the current window DC. after you create a compatible DC, you create a bitmap with the appropriate dimensions by calling the createcompatiblebitmap function and then select it into this device context by calling the SelectObject function.
After the compatible device context is created and the appropriate bitmap has been selected into it, you can capture the image. the bitblt function captures images. this function performs a bit block transfer that is, it copies data from a source bitmap into a destination bitmap. however, the two arguments to this function are not bitmap handles. instead, bitblt into es handles that identify two device contexts and copies the bitmap data from a bitmap selected into the source DC into a bitmap selected into the target DC. in this case, the target DC is the compatible DC, so when bitblt completes the transfer, the image has been stored in memory. to redisplay the image, call bitblt a second time, specifying the compatible DC as the source DC and a window (or printer) DC as the target DC.
The following example code, from an application that captures an image of the entire desktop, creates a compatible device context and a bitmap with the appropriate dimensions, selects the bitmap into the compatible DC, and then copies the image using the bitblt function.
// Create a normal DC and a memory DC for the entire screen.
// Normal DC provides a // "snapshot //" of the screen contents.
// Memory DC keeps a copy of this // "snapshot //" in the associated
// Bitmap.
Hdcscreen = createdc (// "display //", null );
Hdccompatible = createcompatibledc (hdcscreen );
// Create a compatible bitmap for hdcscreen.
Hbmscreen = createcompatiblebitmap (hdcscreen,
Getdevicecaps (hdcscreen, horzres ),
Getdevicecaps (hdcscreen, vertres ));
If (hbmscreen = 0)
Errhandler (// "hbmscreen //", hwnd );
// Select the bitmaps into the compatible DC.
If (! SelectObject (hdccompatible, hbmscreen ))
Errhandler (// "compatible bitmap selection //", hwnd );
// Hide the application window.
Showwindow (hwnd, sw_hide );
// Copy color data for the entire display into
// Bitmap that is selected into a compatible DC.
If (! Bitblt (hdccompatible,
0, 0,
BMP. bmwidth, BMP. bmheight,
Hdcscreen,
0, 0,
Srccopy ))
Errhandler (// "screen to compat BLT failed //", hwnd );
// Redraw the application window.
Showwindow (hwnd, sw_show );
(2) Save
Storing an image
Applications store images permanently as files. For example, drawing applications store pictures, spreadsheet applications store charts, CAD applications store drawings, and so on.
If you are writing an application that stores a bitmap image in a file, you should use the bitmap file format described in bitmap storage. to store a bitmap in this format, you must use a bitmapinfoheader, A bitmapv4header, or a bitmapv5header structure and an array of rgbquad structures, as well as an array of palette indexes.
The following example code defines a function that uses a bitmapinfo structure and allocates memory for and initializes members within a bitmapinfoheader structure. note that the bitmapinfo structure cannot be used with either a bitmapv4header or a bitmapv5header structure.
Pbitmapinfo createbitmapinfostruct (hwnd, hbitmap hbmp)
{
Bitmap BMP;
Pbitmapinfo pbmi;
Word cclrbits;
// Retrieve the bitmap color format, width, and height.
If (! GetObject (hbmp, sizeof (Bitmap), (lpstr) & BMP ))
Errhandler (/"GetObject/", hwnd );
// Convert the color format to a count of bits.
Cclrbits = (Word) (BMP. bmplanes * BMP. bmbitspixel );
If (cclrbits = 1)
Cclrbits = 1;
Else if (cclrbits <= 4)
Cclrbits = 4;
Else if (cclrbits <= 8)
Cclrbits = 8;
Else if (cclrbits <= 16)
Cclrbits = 16;
Else if (cclrbits <= 24)
Cclrbits = 24;
Else cclrbits = 32;
// Allocate memory for the bitmapinfo structure. (This structure
// Contains a bitmapinfoheader structure and an array of rgbquad
// Data structures .)
If (cclrbits! = 24)
Pbmi = (pbitmapinfo) localalloc (lptr,
Sizeof (bitmapinfoheader) +
Sizeof (rgbquad) * (1 <cclrbits ));
// There is no rgbquad array for the 24-bit-per-pixel format.
Else
Pbmi = (pbitmapinfo) localalloc (lptr,
Sizeof (bitmapinfoheader ));
// Initialize the fields in the bitmapinfo structure.
Pbmi-> bmiheader. bisize = sizeof (bitmapinfoheader );
Pbmi-> bmiheader. biwidth = BMP. bmwidth;
Pbmi-> bmiheader. biheight = BMP. bmheight;
Pbmi-> bmiheader. biplanes = BMP. bmplanes;
Pbmi-> bmiheader. bibitcount = BMP. bmbitspixel;
If (cclrbits <24)
Pbmi-> bmiheader. biclrused = (1 <cclrbits );
// If the bitmap is not compressed, set the bi_rgb flag.
Pbmi-> bmiheader. bicompression = bi_rgb;
// Compute the number of bytes in the array of color
// Indices and store the result in bisizeimage.
// For Windows NT, the width must be DWORD aligned unless
// The bitmap is RLE Compressed. This example shows this.
// For Windows 95/98/Me, the width must be word aligned unless
// Bitmap is RLE Compressed.
Pbmi-> bmiheader. bisizeimage = (pbmi-> bmiheader. biwidth * cclrbits + 31 )&~ 31)/8
* Pbmi-> bmiheader. biheight;
// Set biclrimportant to 0, indicating that all of
// Device colors are important.
Pbmi-> bmiheader. biclrimportant = 0;
Return pbmi;
}
The following example code defines a function that initializes the remaining structures, retrieves the array of palette indices, opens the file, copies the data, and closes the file.
Void createbmpfile (hwnd, lptstr pszfile, pbitmapinfo PBI,
Hbitmap hbmp, HDC)
{
Handle Hf; // file handle
Bitmapfileheader HDR; // bitmap file-Header
Pbitmapinfoheader pbih; // bitmap Info-Header
Lpbyte lpbits; // memory pointer
DWORD dwtotal; // total count of bytes
Dword cb; // incremental count of bytes
Byte * HP; // byte pointer
DWORD dwtmp;
Pbih = (pbitmapinfoheader) PBI;
Lpbits = (lpbyte) globalalloc (gmem_fixed, pbih-> bisizeimage );
If (! Lpbits)
Errhandler (/"globalalloc/", hwnd );
// Retrieve the color table (rgbquad array) and the bits
// (Array of palette indices) from the DiB.
If (! Getdibits (HDC, hbmp, 0, (Word) pbih-> biheight, lpbits, PBI,
Dib_rgb_colors ))
{
Errhandler (/"getdibits/", hwnd );
}
// Create the. BMP file.
HF = createfile (pszfile,
Generic_read | generic_write,
(DWORD) 0,
Null,
Create_always,
File_attribute_normal,
(Handle) null );
If (HF = invalid_handle_value)
Errhandler (/"createfile/", hwnd );
HDR. bftype = 0x4d42; // 0x42 =/"B/" 0x4d =/"m /"
// Compute the size of the entire file.
HDR. bfsize = (DWORD) (sizeof (bitmapfileheader) +
Pbih-> bisize + pbih-> biclrused
* Sizeof (rgbquad) + pbih-> bisizeimage );
HDR. bfreserved1 = 0;
HDR. bfreserved2 = 0;
// Compute the offset to the array of color indices.
HDR. bfoffbits = (DWORD) sizeof (bitmapfileheader) +
Pbih-> bisize + pbih-> biclrused
* Sizeof (rgbquad );
// Copy the bitmapfileheader into the. BMP file.
If (! Writefile (HF, (lpvoid) & HDR, sizeof (bitmapfileheader ),
(Lpdword) & dwtmp, null )))
{
Errhandler (/"writefile/", hwnd );
}
// Copy the bitmapinfoheader and rgbquad array into the file.
If (! Writefile (HF, (lpvoid) pbih, sizeof (bitmapinfoheader)
+ Pbih-> biclrused * sizeof (rgbquad ),
(Lpdword) & dwtmp, (null ))
Errhandler (/"writefile/", hwnd );
// Copy the array of color indices into the. BMP file.
Dwtotal = CB = pbih-> bisizeimage;
HP = lpbits;
If (! Writefile (HF, (lpstr) Hp, (INT) Cb, (lpdword) & dwtmp, null ))
Errhandler (/"writefile/", hwnd );
// Close the. BMP file.
If (! Closehandle (HF ))
Errhandler (/"closehandle/", hwnd );
// Free memory.
Globalfree (hglobal) lpbits );
}