gdiobj.cpp
上传用户:xhy777
上传日期:2007-02-14
资源大小:24088k
文件大小:8k
源码类别:

系统编程

开发平台:

Visual C++

  1. #include "precomp.hxx" // PCH
  2. #pragma hdrstop
  3. #include "gdiobj.h"
  4. CGDIObject::CGDIObject(
  5.     HANDLE handle
  6.     ) : m_handle(handle)
  7. {
  8. }
  9. CGDIObject::~CGDIObject(
  10.     VOID
  11.     )
  12. {
  13.     if (NULL != m_handle)
  14.     {
  15.         if (0 == DeleteObject(m_handle))
  16.         {
  17.             DebugMsg(DM_ERROR, TEXT("CGDIObject::~CGDIObject, DeleteObject failed. obj: 0x%08X"), m_handle);
  18.         }
  19.     }
  20. }
  21. CFont::CFont(
  22.     const LOGFONT& lf
  23.     )
  24. {
  25.     *this = lf;
  26. }
  27. CFont& 
  28. CFont::operator = (
  29.     const LOGFONT& lf
  30.     )
  31. {
  32.     if (NULL != m_handle)
  33.     {
  34.         if (0 == DeleteObject(m_handle))
  35.             DebugMsg(DM_ERROR, TEXT("CFont::operator = , DeleteObject failed. obj: 0x%08X"), m_handle);
  36.         m_handle = NULL;
  37.     }
  38.     if (NULL == (m_handle = CreateFontIndirect(&lf)))
  39.         throw CGDIObject::Exception();
  40.     return *this;
  41. }
  42. BOOL
  43. CFont::GetLogFont(
  44.     LPLOGFONT lpf
  45.     ) const
  46. {
  47.     return (0 != ::GetObject(m_handle, sizeof(*lpf), lpf));
  48. }
  49. CDC::CDC(
  50.     HWND hwnd
  51.     ) throw(CGDIObject::Exception, OutOfMemory) :
  52.       m_pRealDC(NULL)
  53. {
  54.     m_pRealDC = new WindowDC(m_handle, hwnd);
  55. }
  56. CDC::CDC(
  57.     HDC hdc
  58.     ) throw(CGDIObject::Exception, OutOfMemory) :
  59.       m_pRealDC(NULL)
  60. {
  61.     m_pRealDC = new CompatibleDC(m_handle, hdc);
  62. }
  63. CDC::~CDC(
  64.     VOID
  65.     )
  66. {
  67.     delete m_pRealDC;
  68. }
  69. CDC::WindowDC::WindowDC(
  70.     HANDLE& refHandle,
  71.     HWND hwnd
  72.     ) : RealDC(refHandle),
  73.         m_hwnd(hwnd)
  74. {
  75.     if (NULL == (refHandle = (HANDLE)GetDC(m_hwnd)))
  76.         throw CGDIObject::Exception();
  77. }
  78. CDC::CompatibleDC::CompatibleDC(
  79.     HANDLE& refHandle,
  80.     HDC hdc
  81.     ) : RealDC(refHandle)
  82. {
  83.     if (NULL == (refHandle = (HANDLE)CreateCompatibleDC(hdc)))
  84.         throw CGDIObject::Exception();
  85. }
  86. CDC::WindowDC::~WindowDC(
  87.     VOID
  88.     )
  89. {
  90.     if (NULL != m_refHandle)
  91.     {
  92.         ReleaseDC(m_hwnd, (HDC)m_refHandle);
  93.         m_refHandle = NULL;
  94.     }
  95.     else
  96.     {
  97.         DebugMsg(DM_ERROR, TEXT("CDC::WindowDC::~WindowDC.  Null hdc.  obj: 0x%08X"), this);
  98.     }
  99. }
  100. CDC::CompatibleDC::~CompatibleDC(
  101.     VOID
  102.     )
  103. {
  104.     if (NULL != m_refHandle)
  105.     {
  106.         DeleteDC((HDC)m_refHandle);
  107.         m_refHandle = NULL;
  108.     }
  109.     else
  110.     {
  111.         DebugMsg(DM_ERROR, TEXT("CDC::CompatibleDC::~CompatibleDC.  Null hdc.  obj: 0x%08X"), this);
  112.     }
  113. }
  114. CBrush::CBrush(
  115.     VOID
  116.     )
  117. {
  118. }
  119.         
  120. CBrush::CBrush(
  121.     COLORREF clrRGB
  122.     )
  123. {
  124.     *this = clrRGB;
  125. }
  126. CBrush& CBrush::operator = (
  127.     COLORREF clrRGB
  128.     )
  129. {
  130.     if (NULL != m_handle)
  131.     {
  132.         if (0 == DeleteObject(m_handle))
  133.             DebugMsg(DM_ERROR, TEXT("CBrush::operator = , DeleteObject failed. obj: 0x%08X"), m_handle);
  134.         m_handle = NULL;
  135.     }
  136.     m_handle = CreateSolidBrush(clrRGB);
  137.     if (NULL == m_handle)
  138.         throw CGDIObject::Exception();
  139.     return *this;
  140. }            
  141. CDIB::CDIB(
  142.     VOID
  143.     ) : m_hPalette(NULL)
  144. {
  145.     ZeroMemory(&m_bitmap, sizeof(m_bitmap));
  146. }
  147. CDIB::CDIB(
  148.     HINSTANCE hInstance,
  149.     LPCTSTR pszResource
  150.     ) : m_hPalette(NULL)
  151. {
  152.     Load(hInstance, pszResource);
  153. }
  154. CDIB::~CDIB(
  155.     VOID
  156.     )
  157. {
  158.     if (NULL != m_hPalette)
  159.     {
  160.         if (0 == DeleteObject(m_hPalette))
  161.             DebugMsg(DM_ERROR, TEXT("CDIB::~CDIB, DeleteObject failed. obj: 0x%08X"), m_hPalette);
  162.     }
  163. }
  164. VOID 
  165. CDIB::Load(
  166.     HINSTANCE hInstance,
  167.     LPCTSTR pszResource
  168.     )
  169. {
  170.     //
  171.     // Delete palette and bitmap if they exist.
  172.     //
  173.     if (NULL != m_hPalette)
  174.     {
  175.         if (0 == DeleteObject(m_hPalette))
  176.             DebugMsg(DM_ERROR, TEXT("CDIB::Load, DeleteObject failed. obj: 0x%08X"), m_hPalette);
  177.         m_hPalette = NULL;
  178.     }
  179.     if (NULL != m_handle)
  180.     {
  181.         if (0 == DeleteObject(m_handle))
  182.             DebugMsg(DM_ERROR, TEXT("CDIB::Load, DeleteObject failed. obj: 0x%08X"), m_handle);
  183.         m_handle = NULL;
  184.     }
  185.     //
  186.     // Load the palette and bitmap.
  187.     //
  188.     m_handle = LoadResourceBitmap(hInstance,
  189.                                   pszResource,
  190.                                   &m_hPalette);
  191.     //
  192.     // If something failed, leave the object in a known state and throw an exception.
  193.     //
  194.     if (NULL == m_handle || NULL == m_hPalette)
  195.     {
  196.         if (NULL != m_handle)
  197.         {
  198.             if (0 == DeleteObject(m_handle))
  199.                 DebugMsg(DM_ERROR, TEXT("CDIB::Load, DeleteObject failed. obj: 0x%08X"), m_handle);
  200.             m_handle = NULL;
  201.         }
  202.         if (NULL != m_hPalette)
  203.         {
  204.             if (0 == DeleteObject(m_hPalette))
  205.                 DebugMsg(DM_ERROR, TEXT("CDIB::Load, DeleteObject failed. obj: 0x%08X"), m_hPalette);
  206.             m_hPalette = NULL;
  207.         }
  208.         throw CGDIObject::Exception();
  209.     }
  210.     //
  211.     // We have a bitmap and palette.  Now cache the bitmap info.
  212.     //
  213.     GetObject(m_handle, sizeof(m_bitmap), &m_bitmap);
  214. }
  215. VOID 
  216. CDIB::GetRect(
  217.     LPRECT prc
  218.     )
  219. {
  220.     prc->left   = prc->top = 0;
  221.     prc->right  = m_bitmap.bmWidth;
  222.     prc->bottom = m_bitmap.bmHeight;
  223. }
  224. VOID 
  225. CDIB::GetBitmapInfo(
  226.     LPBITMAP pbm
  227.     )
  228. {
  229.     *pbm = m_bitmap;
  230. }
  231. HPALETTE
  232. CDIB::CreateDIBPalette(
  233.     LPBITMAPINFO lpbmi, 
  234.     LPINT lpiNumColors
  235.     )
  236. {
  237.     LPBITMAPINFOHEADER  lpbi;
  238.     LPLOGPALETTE     lpPal;
  239.     HANDLE           hLogPal;
  240.     HPALETTE         hPal = NULL;
  241.     int              i;
  242.  
  243.     lpbi = (LPBITMAPINFOHEADER)lpbmi;
  244.     if (lpbi->biBitCount <= 8)
  245.         *lpiNumColors = (1 << lpbi->biBitCount);
  246.     else
  247.         *lpiNumColors = 0;  // No palette needed for 24 BPP DIB
  248.  
  249.     if (*lpiNumColors)
  250.     {
  251.         hLogPal = GlobalAlloc(GHND, 
  252.                              sizeof (LOGPALETTE) +
  253.                              sizeof (PALETTEENTRY) * (*lpiNumColors));
  254.         lpPal = (LPLOGPALETTE) GlobalLock (hLogPal);
  255.         lpPal->palVersion    = 0x300;
  256.         lpPal->palNumEntries = (unsigned short)(*lpiNumColors);
  257.  
  258.         for (i = 0;  i < *lpiNumColors;  i++)
  259.         {
  260.             lpPal->palPalEntry[i].peRed   = lpbmi->bmiColors[i].rgbRed;
  261.             lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen;
  262.             lpPal->palPalEntry[i].peBlue  = lpbmi->bmiColors[i].rgbBlue;
  263.             lpPal->palPalEntry[i].peFlags = 0;
  264.         }
  265.         hPal = CreatePalette (lpPal);
  266.         GlobalUnlock (hLogPal);
  267.         GlobalFree   (hLogPal);
  268.     }
  269.     return hPal;
  270. }
  271. HBITMAP
  272. CDIB::LoadResourceBitmap(
  273.     HINSTANCE hInstance, 
  274.     LPCTSTR lpString,
  275.     HPALETTE *lphPalette
  276.     )
  277. {
  278.     HRSRC  hRsrc;
  279.     HGLOBAL hGlobal;
  280.     HBITMAP hBitmapFinal = NULL;
  281.     LPBITMAPINFOHEADER  lpbi;
  282.     int iNumColors;
  283.  
  284.     if (NULL != (hRsrc = FindResource(hInstance, lpString, RT_BITMAP)))
  285.     {
  286.         hGlobal = LoadResource(hInstance, hRsrc);
  287.         lpbi = (LPBITMAPINFOHEADER)LockResource(hGlobal);
  288.  
  289.         CDC dc;
  290.         HPALETTE hpalOld = NULL;
  291.         *lphPalette =  CreateDIBPalette ((LPBITMAPINFO)lpbi, &iNumColors);
  292.         if (*lphPalette)
  293.         {
  294.             hpalOld = SelectPalette(dc, *lphPalette, TRUE);
  295.             RealizePalette(dc);
  296.         }
  297.  
  298.         hBitmapFinal = CreateDIBitmap(dc,
  299.                                       (LPBITMAPINFOHEADER)lpbi,
  300.                                       (LONG)CBM_INIT,
  301.                                       (LPSTR)lpbi + lpbi->biSize + iNumColors * sizeof(RGBQUAD),
  302.                                       (LPBITMAPINFO)lpbi,
  303.                                       DIB_RGB_COLORS);
  304.  
  305.         if (NULL != hpalOld)
  306.             SelectPalette(dc, hpalOld, TRUE);
  307.         UnlockResource(hGlobal);
  308.         FreeResource(hGlobal);
  309.     }
  310.     return (hBitmapFinal);
  311. }
  312.