int CTestDlg::CTaveClientToFile(HWND hWnd, LPCTSTR szFName)
{
HDC hDC, hMemDC;
HANDLE hBits, hFile;
HBITMAP hBitmap, hTmpBmp;
HPALETTE hPal;
LPVOID lpBits;
RGBQUAD RGBQuad;
DWORD ImgSize, plSize, dwWritten;
int i, CRes, Height, Width;
BITMAPFILEHEADER bmFH;
LPBITMAPINFO pBmInfo, pBmInfoMem;
LPLOGPALETTE lp;
if((hFile = CreateFile(szFName, GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL)) == NULL)
return 1;
SetCursor(LoadCursor(NULL, IDC_WAIT));
if(hWnd==HWND_DESKTOP)
{
Width = GetSystemMetrics(SM_CXSCREEN);
Height = GetSystemMetrics(SM_CYSCREEN);
}
else
{
RECT rc;
::GetClientRect(hWnd,&rc);
Width = rc.right-rc.left;
Height = rc.bottom-rc.top;
}
hDC = ::GetDC(hWnd);
hMemDC = CreateCompatibleDC(hDC);
hBitmap= CreateCompatibleBitmap(hDC, Width, Height);
hTmpBmp= CreateCompatibleBitmap(hDC, 8, 8);
pBmInfoMem = (LPBITMAPINFO)GlobalAlloc(GHND,sizeof(BITMAPINFO)+256*sizeof(RGBQUAD));
pBmInfo = (LPBITMAPINFO)GlobalLock(pBmInfoMem);
SelectObject(hMemDC, hBitmap);
BitBlt(hMemDC,0,0,Width,Height,hDC,0,0,SRCCOPY);
SelectObject(hMemDC, hTmpBmp);
ZeroMemory(pBmInfo, sizeof(BITMAPINFO));
pBmInfo->bmiHeader.biSize = (DWORD)sizeof(BITMAPINFOHEADER);
pBmInfo->bmiHeader.biWidth = Width;
pBmInfo->bmiHeader.biHeight = Height;
pBmInfo->bmiHeader.biPlanes = 1;
pBmInfo->bmiHeader.biBitCount = (WORD)GetDeviceCaps(hDC, BITSPIXEL);
pBmInfo->bmiHeader.biCompression = BI_RGB;
GetDIBits(hDC, hBitmap, 0,Height, NULL, pBmInfo, DIB_RGB_COLORS);
if(!pBmInfo->bmiHeader.biSizeImage)
pBmInfo->bmiHeader.biSizeImage = ((((pBmInfo->bmiHeader.biWidth * pBmInfo->bmiHeader.biBitCount) + 31) & ~31) / 8)
* pBmInfo->bmiHeader.biHeight;
CRes = GetDeviceCaps(hDC, SIZEPALETTE);
plSize = CRes*sizeof(RGBQUAD);
ImgSize= pBmInfo->bmiHeader.biSizeImage;
bmFH.bfType = 0x4d42; //"BM"
bmFH.bfOffBits = plSize + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
bmFH.bfSize = ImgSize + bmFH.bfOffBits;
bmFH.bfReserved1 = 0;
bmFH.bfReserved2 = 0;
WriteFile(hFile, &bmFH, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
WriteFile(hFile, &(pBmInfo->bmiHeader), sizeof(BITMAPINFOHEADER), &dwWritten, NULL);
if(CRes)
{
hPal=(HPALETTE)GlobalAlloc(GHND, sizeof(LOGPALETTE) + (CRes*sizeof(PALETTEENTRY)));
lp=(LPLOGPALETTE)GlobalLock(hPal);
lp->palNumEntries=(WORD)CRes;
lp->palVersion=0x0300;
GetSystemPaletteEntries(hDC, 0, CRes, lp->palPalEntry);
RGBQuad.rgbReserved=0;
for(i=0; i {
RGBQuad.rgbRed = lp->palPalEntry[i].peRed;
RGBQuad.rgbGreen = lp->palPalEntry[i].peGreen;
RGBQuad.rgbBlue = lp->palPalEntry[i].peBlue;
WriteFile(hFile, &RGBQuad, sizeof(RGBQUAD), &dwWritten, NULL);
}
GlobalUnlock(hPal);
GlobalFree(hPal);
}
hBits = GlobalAlloc(GHND, pBmInfo->bmiHeader.biSizeImage);
lpBits = (LPVOID)GlobalLock(hBits);
GetDIBits(hDC, hBitmap, 0,Height, lpBits, pBmInfo, DIB_RGB_COLORS);
WriteFile(hFile,lpBits,ImgSize,&dwWritten,NULL);
GlobalUnlock(hBits);
GlobalFree(hBits);
GlobalUnlock(pBmInfo);
GlobalFree(pBmInfoMem);
DeleteObject(hTmpBmp);
DeleteObject(hBitmap);
DeleteDC(hMemDC);
::ReleaseDC(hWnd,hDC);
CloseHandle(hFile);
SetCursor(LoadCursor(NULL,IDC_ARROW));
return 0;
}
阅读(1386) | 评论(1) | 转发(0) |