1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
BOOL ScreenshotToBmp(HWND hWnd, LPCTSTR szFilename, BOOL bFlag)
{
BOOL bRet = FALSE;
HDC hMemDC, hDC;
DWORD dwWrite;
HANDLE hFile;
void *pBits;
WORD wBpp;
HBITMAP hBmp;
BITMAPFILEHEADER bfh;
BITMAPINFO bi;
RECT r;
if(bFlag) // Screenshot de la fenetre entiere
{
hDC = GetWindowDC(hWnd);
GetWindowRect(hWnd, &r);
}
else // Screenshot de la zone cliente uniquement
{
hDC = GetDC(hWnd);
GetClientRect(hWnd, &r);
}
r.bottom -= r.top;
r.right -= r.left;
hMemDC = CreateCompatibleDC(hDC);
wBpp = GetDeviceCaps(hDC, BITSPIXEL);
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = r.right;
bi.bmiHeader.biHeight = r.bottom;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = wBpp;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = bi.bmiHeader.biXPelsPerMeter = 0;
bi.bmiHeader.biYPelsPerMeter = bi.bmiHeader.biClrUsed = bi.bmiHeader.biClrImportant = 0;
if((hBmp = CreateDIBSection(hDC, &bi, DIB_PAL_COLORS, &pBits, 0, 0)))
{
SelectObject(hMemDC, hBmp);
if(BitBlt(hMemDC, 0, 0, r.right, r.bottom, hDC, 0, 0, SRCCOPY))
{
bfh.bfType = 0x4D42;
bfh.bfSize = ((r.right * r.bottom * wBpp) >> 3) + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bfh.bfReserved1 = bfh.bfReserved2 = 0;
hFile = CreateFile(szFilename, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
WriteFile(hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwWrite, 0);
WriteFile(hFile, &bi.bmiHeader, sizeof(BITMAPINFOHEADER), &dwWrite, 0);
WriteFile(hFile, pBits, (r.right * r.bottom * wBpp) >> 3, &dwWrite, 0);
CloseHandle(hFile);
bRet = TRUE;
}
}
DeleteObject(hBmp);
}
ReleaseDC(hWnd, hDC);
DeleteDC(hMemDC);
return bRet;
}
...
// Fenetre entiere
ScreenshotToBmp(hWnd, "screenshot.bmp", 1);
...
// Zone cliente
ScreenshotToBmp(hDlg, "screenshot.bmp", 0);
... |
Partager