Add the following functions to the View class:
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 (, rect. Width (), rect. Height (), & DC, 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;
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) B _dirgb_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;
}
Call the writewindowtodib function when saving ()
Void ctestestview: onfilesave ()
{
// Todo: add your command handler code here
Writewindowtodib ("D: \ my.bmp", this );
}
I have tested it. You can add it directly, but note that the class name should be the same as yours.