void screen(wchar_t* fileName)
{
HWND window = ::GetDesktopWindow();
HDC _dc = ::GetWindowDC(window);//屏幕DC
HDC dc = CreateCompatibleDC(0);//内存DC
RECT re;
::GetWindowRect(window, &re);
int w = re.right,
h = re.bottom;
void* buf = new char[w*h*4];
HBITMAP bm = CreateCompatibleBitmap(_dc, w, h);//建立和屏幕兼容的bitmap
SelectObject(dc, bm);//将memBitmap选入内存DC
StretchBlt(dc, 0, 0, w, h, _dc, 0, 0, w, h, SRCCOPY);//复制屏幕图像到内存DC
void* f =CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
GetObject(bm, 84, buf);
tagBITMAPINFO bi;
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = w;
bi.bmiHeader.biHeight = h;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = 0;
bi.bmiHeader.biSizeImage = 0;
CreateDIBSection(dc, &bi, DIB_RGB_COLORS, &buf, 0, 0);
GetDIBits(dc, bm, 0, h, buf, &bi, DIB_RGB_COLORS);
BITMAPFILEHEADER bif;
bif.bfType = MAKEWORD('B','M');
bif.bfSize = w*h*4+54;
bif.bfOffBits = 54;
BITMAPINFOHEADER bii;
bii.biSize = 40;
bii.biWidth = w;
bii.biHeight = h;
bii.biPlanes = 1;
bii.biBitCount = 32;
bii.biCompression = 0;
bii.biSizeImage = w*h*4;
DWORD r;
WriteFile(f, &bif, sizeof(bif), &r, NULL);
WriteFile(f, &bii, sizeof(bii), &r, NULL);
WriteFile(f, buf, w*h*4, &r, NULL);
CloseHandle(f);
DeleteDC(_dc);
DeleteDC(dc);
}
{
HWND window = ::GetDesktopWindow();
HDC _dc = ::GetWindowDC(window);//屏幕DC
HDC dc = CreateCompatibleDC(0);//内存DC
RECT re;
::GetWindowRect(window, &re);
int w = re.right,
h = re.bottom;
void* buf = new char[w*h*4];
HBITMAP bm = CreateCompatibleBitmap(_dc, w, h);//建立和屏幕兼容的bitmap
SelectObject(dc, bm);//将memBitmap选入内存DC
StretchBlt(dc, 0, 0, w, h, _dc, 0, 0, w, h, SRCCOPY);//复制屏幕图像到内存DC
void* f =CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
GetObject(bm, 84, buf);
tagBITMAPINFO bi;
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = w;
bi.bmiHeader.biHeight = h;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = 0;
bi.bmiHeader.biSizeImage = 0;
CreateDIBSection(dc, &bi, DIB_RGB_COLORS, &buf, 0, 0);
GetDIBits(dc, bm, 0, h, buf, &bi, DIB_RGB_COLORS);
BITMAPFILEHEADER bif;
bif.bfType = MAKEWORD('B','M');
bif.bfSize = w*h*4+54;
bif.bfOffBits = 54;
BITMAPINFOHEADER bii;
bii.biSize = 40;
bii.biWidth = w;
bii.biHeight = h;
bii.biPlanes = 1;
bii.biBitCount = 32;
bii.biCompression = 0;
bii.biSizeImage = w*h*4;
DWORD r;
WriteFile(f, &bif, sizeof(bif), &r, NULL);
WriteFile(f, &bii, sizeof(bii), &r, NULL);
WriteFile(f, buf, w*h*4, &r, NULL);
CloseHandle(f);
DeleteDC(_dc);
DeleteDC(dc);
}
本文介绍了一个使用C++实现的功能,用于从Windows桌面捕获屏幕截图,并将其保存到指定的文件中。通过获取桌面设备上下文、创建兼容的内存设备上下文、复制屏幕图像到内存DC,然后将此图像转换为位图格式,并最终将其保存到磁盘文件中。
242

被折叠的 条评论
为什么被折叠?



