void CPowerDownDlg::CopyScreenToBitmap(void)
{
dcc = GetDesktopWindow()->GetWindowDC();
CBitmap hbitmap;//参数可选
dcMem = CreateCompatibleDC(*dcc); //guanlian
BITMAPINFO RGB16BitsBITMAPINFO;
ZeroMemory(&RGB16BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB16BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB16BitsBITMAPINFO.bmiHeader.biWidth = 480;
RGB16BitsBITMAPINFO.bmiHeader.biHeight = 800;
RGB16BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB16BitsBITMAPINFO.bmiHeader.biBitCount = 16;
BYTE *lpBitmapBits = NULL;
HBITMAP directBmp = CreateDIBSection(dcMem, (BITMAPINFO*)&RGB16BitsBITMAPINFO,
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(dcMem, directBmp);
BitBlt(dcMem, 0, 0, 480, 800, *dcc, 0, 0, SRCCOPY);
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((480*800)*2); ///2=(16 / 8)
FILE *mStream = NULL;
if((mStream = fopen("deskmap.bmp", "wb")))
{
//write bitmap file header
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
//write bitmap info
fwrite(&(RGB16BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
//write bitmap pixels data
fwrite(lpBitmapBits, 2*480*800, 1, mStream);
//close file
fclose(mStream);
}
//delete 如果只是导出bmp 需要销毁
DeleteObject(dcMem);
DeleteObject(dcc);
DeleteObject(directBmp);
DeleteObject(previousObject);
}
截取的桌面图片保存在 deskmap.map 中