winapi 截屏函数(转)
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);
}