Created
February 1, 2017 19:53
-
-
Save devxom/0e96c4c40bf1f988f0baf2048c1058e4 to your computer and use it in GitHub Desktop.
Capture screen [c/c++]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// source: web.archive.org/web/20140326200825/http://it-talk.org/topic2646.html | |
void CaptureScreen(HWND window) | |
{ | |
RECT windowRect; | |
GetWindowRect(window, &windowRect); | |
int bitmap_dx = windowRect.right-windowRect.left; | |
int bitmap_dy = windowRect.bottom-windowRect.top; | |
BITMAPINFOHEADER bmpInfoHeader; | |
BITMAPFILEHEADER bmpFileHeader; | |
BITMAP* pBitmap; | |
bmpFileHeader.bfType = 0x4d42; | |
bmpFileHeader.bfSize = 0; | |
bmpFileHeader.bfReserved1 = 0; | |
bmpFileHeader.bfReserved2 = 0; | |
bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); | |
bmpInfoHeader.biSize = sizeof(bmpInfoHeader); | |
bmpInfoHeader.biWidth = bitmap_dx; | |
bmpInfoHeader.biHeight = bitmap_dy; | |
bmpInfoHeader.biPlanes = 1; | |
bmpInfoHeader.biBitCount = 24; | |
bmpInfoHeader.biCompression = BI_RGB; | |
bmpInfoHeader.biSizeImage = bitmap_dx*bitmap_dy*(24/8); | |
bmpInfoHeader.biXPelsPerMeter = 0; | |
bmpInfoHeader.biYPelsPerMeter = 0; | |
bmpInfoHeader.biClrUsed = 0; | |
bmpInfoHeader.biClrImportant = 0; | |
BITMAPINFO info; | |
info.bmiHeader = bmpInfoHeader; | |
BYTE* memory; | |
HDC winDC = GetWindowDC(window); | |
HDC bmpDC = CreateCompatibleDC(winDC); | |
HBITMAP bitmap = CreateDIBSection(winDC, &info, DIB_RGB_COLORS, (void**)&memory, NULL, 0); | |
SelectObject(bmpDC, bitmap);//Выбираем в контекст битмэп | |
BitBlt(bmpDC, 0, 0, bitmap_dx, bitmap_dy, winDC, 0, 0, SRCCOPY); | |
ReleaseDC(window, winDC); | |
OPENFILENAME ofn;//Указатель на структуру с данными инициализации диалогового окна | |
char strFilter[] = "Файлы данных *.bmp\0"; | |
char strFileName[MAX_PATH] = ""; | |
memset(&ofn, 0, sizeof(OPENFILENAME));//Обнуление ofn | |
ofn.lStructSize = sizeof(OPENFILENAME); | |
ofn.hwndOwner = window; | |
ofn.lpstrFilter = strFilter; | |
ofn.lpstrFile = strFileName;//Буфер для имени файла | |
ofn.nMaxFile = MAX_PATH;//Размер файла | |
ofn.lpstrInitialDir = NULL; | |
ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT; | |
strcpy(strFileName, ofn.lpstrFile); | |
GetSaveFileName(&ofn); //MessageBox(hwnd,"Невозможно сохранить файл","О программе...",MB_ICONINFORMATION); | |
HANDLE hFile = CreateFile( | |
ofn.lpstrFile, | |
GENERIC_WRITE, | |
0, | |
NULL, | |
OPEN_ALWAYS, | |
FILE_ATTRIBUTE_NORMAL, | |
NULL); | |
if (hFile == INVALID_HANDLE_VALUE) | |
return; | |
DWORD dwWritten = 0; | |
WriteFile(hFile, &bmpFileHeader, sizeof(BITMAPFILEHEADER), &dwWritten, NULL); | |
WriteFile(hFile, &bmpInfoHeader, sizeof(BITMAPINFOHEADER), &dwWritten, NULL); | |
WriteFile(hFile, memory, bmpInfoHeader.biSizeImage, &dwWritten, NULL); | |
CloseHandle(hFile); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment