home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap12 / Vista / VistaDoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  3.2 KB  |  111 lines

  1. //***********************************************************************
  2. //
  3. //  VistaDoc.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "VistaDoc.h"
  9.  
  10. IMPLEMENT_DYNCREATE (CVistaDoc, CDocument)
  11.  
  12. BOOL CVistaDoc::OnOpenDocument (LPCTSTR lpszPathName) 
  13. {
  14.     if (!CDocument::OnOpenDocument (lpszPathName)) {
  15.         POSITION pos = GetFirstViewPosition ();
  16.         CView* pView = GetNextView (pos);
  17.         pView->OnInitialUpdate ();
  18.         return FALSE;
  19.     }
  20.  
  21.     // Return now if this device doesn't support palettes
  22.     CClientDC dc (AfxGetMainWnd ());
  23.     if ((dc.GetDeviceCaps (RASTERCAPS) & RC_PALETTE) == 0)
  24.         return TRUE;
  25.  
  26.     // Create a palette to go with the DIB section
  27.     if ((HBITMAP) m_bitmap != NULL) {
  28.         DIBSECTION ds;
  29.         m_bitmap.GetObject (sizeof (DIBSECTION), &ds);
  30.  
  31.         int nColors;
  32.         if (ds.dsBmih.biClrUsed != 0)
  33.             nColors = ds.dsBmih.biClrUsed;
  34.         else
  35.             nColors = 1 << ds.dsBmih.biBitCount;
  36.  
  37.         // Create a halftone palette if the DIB section contains more
  38.         // than 256 colors
  39.         if (nColors > 256)
  40.             m_palette.CreateHalftonePalette (&dc);
  41.  
  42.         // Create a custom palette from the DIB section's color table
  43.         // if the number of colors is 256 or less
  44.         else {
  45.             RGBQUAD* pRGB = new RGBQUAD[nColors];
  46.  
  47.             CDC memDC;
  48.             memDC.CreateCompatibleDC (&dc);
  49.             CBitmap* pOldBitmap = memDC.SelectObject (&m_bitmap);
  50.             ::GetDIBColorTable ((HDC) memDC, 0, nColors, pRGB);
  51.             memDC.SelectObject (pOldBitmap);
  52.  
  53.             UINT nSize = sizeof (LOGPALETTE) +
  54.                 (sizeof (PALETTEENTRY) * (nColors - 1));
  55.             LOGPALETTE* pLP = (LOGPALETTE*) new BYTE[nSize];
  56.  
  57.             pLP->palVersion = 0x300;
  58.             pLP->palNumEntries = nColors;
  59.  
  60.             for (int i=0; i<nColors; i++) {
  61.                 pLP->palPalEntry[i].peRed = pRGB[i].rgbRed;
  62.                 pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
  63.                 pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
  64.                 pLP->palPalEntry[i].peFlags = 0;
  65.             }
  66.  
  67.             m_palette.CreatePalette (pLP);
  68.             delete[] pLP;
  69.             delete[] pRGB;
  70.         }
  71.     }
  72.     return TRUE;
  73. }
  74.  
  75. void CVistaDoc::DeleteContents ()
  76. {
  77.     if ((HBITMAP) m_bitmap != NULL)
  78.         m_bitmap.DeleteObject ();
  79.  
  80.     if ((HPALETTE) m_palette != NULL)
  81.         m_palette.DeleteObject ();
  82.     
  83.     CDocument::DeleteContents();
  84. }
  85.  
  86. void CVistaDoc::Serialize (CArchive& ar)
  87. {
  88.     if (ar.IsLoading ()) {
  89.         CString strFileName = ar.GetFile ()->GetFilePath ();
  90.  
  91.         HBITMAP hBitmap = (HBITMAP) ::LoadImage (AfxGetInstanceHandle (),
  92.             (LPCTSTR) strFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE |
  93.             LR_CREATEDIBSECTION);
  94.  
  95.         if (hBitmap == NULL)
  96.             AfxThrowArchiveException (CArchiveException::badIndex);
  97.  
  98.         m_bitmap.Attach (hBitmap);
  99.     }
  100. }
  101.  
  102. CBitmap* CVistaDoc::GetBitmap ()
  103. {
  104.     return ((HBITMAP) m_bitmap == NULL) ? NULL : &m_bitmap;
  105. }
  106.  
  107. CPalette* CVistaDoc::GetPalette ()
  108. {
  109.     return ((HPALETTE) m_palette == NULL) ? NULL : &m_palette;
  110. }
  111.