zoukankan      html  css  js  c++  java
  • vc++如何将客户区存为bmp【zz】

    在view类中添加以下三个函数:

    BOOL CTestestView::WriteWindowToDIB(LPTSTR szFile, CWnd *pWnd)

    {

           CBitmap bitmap;

           CWindowDC dc(pWnd);

           CDC memDC;

           CRect rect;

           memDC.CreateCompatibleDC(&dc);

           pWnd->GetWindowRect(rect);

           bitmap.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());

           CBitmap* pOldBitmap=memDC.SelectObject(&bitmap);

           memDC.BitBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,SRCCOPY);

           CPalette pal;

           if(dc.GetDeviceCaps(RASTERCAPS)&RC_PALETTE)

           {

                  UINT nSize=sizeof(LOGPALETTE)+(sizeof(PALETTEENTRY)*256);

                  LOGPALETTE* pLP=(LOGPALETTE*)new BYTE[nSize];

                  pLP->palVersion=0x300;

                  pLP->palNumEntries=GetSystemPaletteEntries(dc,0,255,pLP->palPalEntry);

                  pal.CreatePalette(pLP);

                  delete[] pLP;

     

           }

           memDC.SelectObject(pOldBitmap);

           HANDLE hDIB=DDBToDIB(bitmap,BI_RGB,&pal);

           if(hDIB==NULL)

                  return FALSE;

           WriteDIB(szFile,hDIB);

           GlobalFree(hDIB);

           return TRUE;

     

    }

     

    HANDLE CTestestView::DDBToDIB(CBitmap &bitmap, DWORD dwCompression, CPalette *pPal)

    {

           BITMAP bm;

           BITMAPINFOHEADER bi;

           LPBITMAPINFOHEADER lpbi;

           DWORD dwLen;

           HANDLE hDIB;

           HANDLE handle;

           HDC hDC;

           HPALETTE hPal;

           ASSERT(bitmap.GetSafeHandle());

           if(dwCompression==BI_BITFIELDS)

                  return NULL;

           hPal=(HPALETTE)pPal->GetSafeHandle();

           if(hPal==NULL)

                  hPal=(HPALETTE)GetStockObject(DEFAULT_PALETTE);

           bitmap.GetObject(sizeof(bm),(LPSTR)&bm);

           bi.biSize=sizeof(BITMAPINFOHEADER);

           bi.biWidth=bm.bmWidth;

           bi.biHeight=bm.bmHeight;

           bi.biPlanes=1;

           bi.biBitCount=bm.bmPlanes*bm.bmBitsPixel;

           bi.biCompression=dwCompression;

           bi.biSizeImage=0;

           bi.biXPelsPerMeter=0;

           bi.biYPelsPerMeter=0;

           bi.biClrImportant=0;

           bi.biClrUsed=0;

     

           int nColors=(1<<bi.biBitCount);

           if(nColors>256)

                  nColors=0;

           dwLen=bi.biSize+nColors*sizeof(RGBQUAD);

           hDC=::GetDC(NULL);

           hPal=SelectPalette(hDC,hPal,FALSE);

           RealizePalette(hDC);

           hDIB=GlobalAlloc(GMEM_FIXED,dwLen);

           if(!hDIB)

           {

                  SelectPalette(hDC,hPal,FALSE);

                         ::ReleaseDC(NULL,hDC);

                  return NULL;

     

           }

          

           lpbi=(LPBITMAPINFOHEADER)hDIB;

           *lpbi=bi;

           GetDIBits(hDC,(HBITMAP)bitmap.GetSafeHandle(),0L,(DWORD)bi.biHeight,(LPBYTE)NULL,(LPBITMAPINFO)lpbi,(DWORD)DIB_RGB_COLORS);

           bi=*lpbi;

           if(bi.biSizeImage==0)

           {

                  bi.biSizeImage=((((bi.biWidth*bi.biBitCount)+31)& ~31)/8)*bi.biHeight;

                  if(dwCompression!=BI_RGB)

                         bi.biSizeImage=(bi.biSizeImage*3)/2;

           }

     

           dwLen+=bi.biSizeImage;

           if(handle=GlobalReAlloc(hDIB,dwLen,GMEM_MOVEABLE))

                  hDIB=handle;

           else

           {

                  GlobalFree(hDIB);

                  SelectPalette(hDC,hPal,FALSE);

                  ::ReleaseDC(NULL,hDC);

                  return NULL;

           }

           lpbi=(LPBITMAPINFOHEADER)hDIB;

           BOOL bGotBits=GetDIBits(hDC,(HBITMAP)bitmap.GetSafeHandle(),0L,(DWORD)bi.biHeight,(LPBYTE)lpbi+(bi.biSize+nColors*sizeof(RGBQUAD)),

                  (LPBITMAPINFO)lpbi,(DWORD)DIB_RGB_COLORS);

           if(!bGotBits)

           {

                  GlobalFree(hDIB);

                  SelectPalette(hDC,hPal,FALSE);

                  ::ReleaseDC(NULL,hDC);

                  return NULL;

           }

           SelectPalette(hDC,hPal,FALSE);

           ::ReleaseDC(NULL,hDC);

           return hDIB;

    }

     

    BOOL CTestestView::WriteDIB(LPTSTR szFile, HANDLE hDIB)

    {

           BITMAPFILEHEADER hdr;

           LPBITMAPINFOHEADER lpbi;

           if(!hDIB)

                  return FALSE;

           CFile file;

           if(!file.Open(szFile,CFile::modeWrite|CFile::modeCreate))

                  return FALSE;

           lpbi=(LPBITMAPINFOHEADER)hDIB;

           int nColors=1<<lpbi->biBitCount;

           hdr.bfType=((WORD)('M'<<8)|'B');

           hdr.bfSize=::GlobalSize(hDIB)+sizeof(hdr);

           hdr.bfReserved1=0;

           hdr.bfReserved2=0;

           hdr.bfOffBits=(DWORD)(sizeof(hdr)+lpbi->biSize+nColors*sizeof(RGBQUAD));

           file.Write(&hdr,sizeof(hdr));

           file.Write(lpbi,GlobalSize(hDIB));

           return TRUE;

     

    }

     在保存时调用函数WriteWindowToDIB()

    void CTestestView::OnFileSave()

    {

           // TODO: Add your command handler code here

           WriteWindowToDIB("D:\\My.bmp",this);    

    }

     

    本人测试过,确实直接加上就可以了,但是注意类名要改为和你的一致的

  • 相关阅读:
    KBEngine:记录
    h5游戏研讨
    Django部署方法
    JQuery Advanced
    Web从入门到放弃<7>
    Web从入门到放弃<6>
    Web从入门到放弃<5>
    Web从入门到放弃<4>
    Web从入门到放弃<3>
    Web从入门到放弃<2>
  • 原文地址:https://www.cnblogs.com/cuteshongshong/p/2819368.html
Copyright © 2011-2022 走看看