#include void ScreenCap (TCHAR* file) { HBITMAP hDIB; BITMAPINFOHEADER bmpih; BITMAPFILEHEADER bmpfh; HDC hdc, hdcOld, hdcMem; BYTE* imgBuf; HANDLE hFile; DWORD written; UINT w = GetSystemMetrics (SM_CXSCREEN), h = GetSystemMetrics (SM_CYSCREEN), size = w * h * 3; // Allocate image buffer imgBuf = new BYTE[size]; // Initialize bitmap information header bmpih.biSize = sizeof(BITMAPINFOHEADER); bmpih.biWidth = w; bmpih.biHeight = h; bmpih.biPlanes = 1; bmpih.biBitCount = 24; bmpih.biCompression = BI_RGB; bmpih.biSizeImage = sizeof(imgBuf); bmpih.biXPelsPerMeter = 0; bmpih.biYPelsPerMeter = 0; bmpih.biClrUsed = 0; bmpih.biClrImportant = 0; // Capture Screen hdc = GetDC (NULL); hDIB = CreateDIBSection (hdc, (BITMAPINFO*)&bmpih, DIB_RGB_COLORS, (void**)&imgBuf, NULL, 0); hdcMem = CreateCompatibleDC (hdc); hdcOld = (HDC) SelectObject (hdcMem, hDIB); BitBlt (hdcMem, 0, 0, w, h, hdc, 0, 0, SRCCOPY); SelectObject (hdcMem, hdcOld); ReleaseDC (NULL, hdc); // Initialize bitmap file header bmpfh.bfType = *(WORD*)"BM"; bmpfh.bfOffBits = sizeof(BITMAPFILEHEADER) + bmpih.biSize; bmpfh.bfSize = bmpfh.bfOffBits + bmpih.biSizeImage; bmpfh.bfReserved1 = bmpfh.bfReserved2 = 0; // Write bitmap to file hFile = CreateFile (file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile != INVALID_HANDLE_VALUE) { WriteFile (hFile, &bmpfh, sizeof(BITMAPFILEHEADER), &written, NULL); WriteFile (hFile, &bmpih, sizeof(BITMAPINFOHEADER), &written, NULL); WriteFile (hFile, imgBuf, size, &written, NULL); CloseHandle (hFile); } else { MessageBox (NULL, file, NULL, MB_OK); } }