The first step of screen capture technology is to capture the desktop of the screen.
Although the print screen key is acceptable, it is not applicable in practice.
The following code can be copied to a function and executed to generate a bmp file for obtaining the screen.
[Cpp]
// Obtain the CDC of the desktop window
CDC * pdeskdc = GetDesktopWindow ()-> GetDC ();
CRect re;
// Obtain the window size
GetClientRect (& re );
CBitmap bmp;
Bmp. CreateCompatibleBitmap (p1_dc, re. Width (), re. Height ());
// Create a compatible memory canvas
CDC memorydc;
Memorydc. CreateCompatibleDC (p1_dc );
// Select the paint brush
CBitmap * pold = memorydc. SelectObject (& bmp );
// Draw the image
Memorydc. BitBlt (0, 0, re. Width (), re. Height (), p1_dc, 0, 0, SRCCOPY );
// Obtain the mouse position and add the mouse Image
CPoint po;
GetCursorPos (& po );
HICON hinco = (HICON) GetCursor ();
Memorydc. DrawIcon (po. X-10, po. y-10, hinco );
// Select the original paint brush
Memorydc. SelectObject (pold );
BITMAP bit;
Bmp. GetBitmap (& bit );
/DWORD size = bit. bmWidth * bit. bmHeight;
// Define the image size (unit: byte)
DWORD size = bit. bmWidthBytes * bit. bmHeight;
LPSTR lpdata = (LPSTR) GlobalAlloc (GPTR, size );
// The file header is required to create a bmp file. For details, refer to the msdn
BITMAPINFOHEADER pbitinfo;
Pbitinfo. biBitCount = 24;
Pbitinfo. biClrImportant = 0;
Pbitinfo. biCompression = BI_RGB;
Pbitinfo. biHeight = bit. bmHeight;
Pbitinfo. biPlanes = 1;
Pbitinfo. biSize = sizeof (BITMAPINFOHEADER );
Pbitinfo. biSizeImage = size;
Pbitinfo. biWidth = bit. bmWidth;
Pbitinfo. biXPelsPerMeter = 0;
Pbitinfo. biYPelsPerMeter = 0;
GetDIBits (p1_dc-> m_hDC, bmp, 0, pbitinfo. biHeight, lpdata,
(BITMAPINFO *) & pbitinfo, DIB_RGB_COLORS );
BITMAPFILEHEADER bfh;
Bfh. bfReserved1 = bfh. bfReserved2 = 0;
Bfh. bfType = (WORD) ('M' <8) | 'B ');
Bfh. bfSize = 54 + size;
Bfh. bfOffBits = 54;
// Write a file
CFile file;
If (file. Open ("1.bmp", CFile: modeCreate | CFile: modeWrite ))
{
File. WriteHuge (& bfh, sizeof (BITMAPFILEHEADER ));
File. WriteHuge (& pbitinfo, sizeof (BITMAPINFOHEADER ));
File. WriteHuge (lpdata, size );
File. Close ();
}
GlobalFree (lpdata );