sprint 1-alpha
sprint/gtl/Bitmap.h
Go to the documentation of this file.
00001 /******************************************************************************
00002  *  GTL: GUI Template Library 
00003  *    A Cross Platform C++ Wrapper for Win32 users
00004  *  Copyright (C) 2007-2011 Paolo Medici (www.pmx.it)
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2.1 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Lesser General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019  *
00020  *******************************************************************************/
00021 
00022 #ifndef _SPRINT_GTL_BITMAP_H
00023 #define _SPRINT_GTL_BITMAP_H
00024 
00028 #ifdef WIN32
00029 
00030 #include <windows.h>
00031 
00032 namespace sprint {
00033         namespace gtl {
00034 
00035 #if 0
00036 HBITMAP LoadBitmapAndRemap(HINSTANCE hInst, int id, COLORREF from, COLORREF to)
00037 {
00038 // DWORD backgroundColor = GetSysColor(COLOR_BTNFACE);
00039 COLORMAP colorMap;
00040 colorMap.from = from;
00041 colorMap.to = to;
00042 return  ::CreateMappedBitmap(hInst, id, 0, &colorMap, 1);
00043 }
00044 #endif
00045 
00047 template<bool tManagedBmp>
00048 class TBitmap {
00049         public:
00050         HBITMAP m_bmp;
00051         public:
00052                 TBitmap(HBITMAP hbmp = NULL) : m_bmp(hbmp) { }
00053                 ~TBitmap() { if(tManagedBmp) ::DeleteObject(m_bmp); }
00054 
00056                 bool IsNull() const { return m_bmp == NULL; }
00057 
00058                 
00059                 operator HBITMAP() const { return m_bmp; }
00060 
00061         bool DeleteObject()
00062         {
00063         if(m_bmp)
00064         {
00065                 BOOL bRet = ::DeleteObject(m_bmp);
00066                 if(bRet)
00067                         m_bmp = NULL;
00068                 return bRet;
00069         }
00070         else
00071         return true;
00072         }
00073                 
00075                 HBITMAP Attach(HBITMAP hBitmap)
00076                 {
00077                         if(tManagedBmp && m_bmp != NULL&& m_bmp != hBitmap)
00078                                 ::DeleteObject(m_bmp);
00079                         m_bmp = hBitmap;
00080                         return hBitmap;
00081                 }
00082                 
00084                 HBITMAP Detach()
00085                 {
00086                         HBITMAP hBitmap = m_bmp;
00087                         m_bmp = NULL;
00088                         return hBitmap;
00089                 }
00090                 
00092         HBITMAP LoadBitmap(LPCTSTR  bitmap)
00093         {
00094         return Attach( ::LoadBitmap(::GetModuleHandle(NULL), bitmap) );
00095         }
00096 
00097         HBITMAP LoadOEMBitmap(UINT nIDBitmap) // for OBM_/OCR_/OIC_
00098         {
00099         return Attach( ::LoadBitmap(NULL, MAKEINTRESOURCE(nIDBitmap)) );
00100         }
00101 
00102         HBITMAP CreateDIBitmap(HDC hDC, CONST BITMAPINFOHEADER* lpbmih, DWORD dwInit, CONST VOID* lpbInit, CONST BITMAPINFO* lpbmi, UINT uColorUse)
00103         {
00104                 return Attach( ::CreateDIBitmap(hDC, lpbmih, dwInit, lpbInit, lpbmi, uColorUse) );
00105         }
00106         
00112         HBITMAP CreateDIBSection(HDC hDC, const BITMAPINFO* lpbmi, UINT uColorUse, void** ppvBits, HANDLE hSection = 0, DWORD dwOffset = 0)
00113         {
00114         return Attach( ::CreateDIBSection(hDC, lpbmi, uColorUse, ppvBits, hSection, dwOffset) );
00115         }
00116 
00119         HBITMAP CreateBitmap(int nWidth, int nHeight, UINT nPlanes, UINT nBitsPerPixel, const void* lpBits)
00120         {
00121         return Attach( ::CreateBitmap(nWidth, nHeight, nPlanes, nBitsPerPixel, lpBits) );
00122         }       
00123         
00125                 HBITMAP CreateCompatibleBitmap(HDC hDC, int nWidth, int nHeight)
00126                 {
00127                         return Attach( ::CreateCompatibleBitmap(hDC, nWidth, nHeight) );
00128                 }
00129 
00131                 bool GetBitmap(BITMAP& bm) const
00132                 {
00133                         return (::GetObject(m_bmp, sizeof(BITMAP), &bm) == sizeof(BITMAP));
00134                 }
00135                 
00136         int GetDIBits(HDC hDC, UINT uStartScan, UINT cScanLines,  LPVOID lpvBits, LPBITMAPINFO lpbmi, UINT uColorUse) const
00137         {
00138                 return ::GetDIBits(hDC, m_bmp, uStartScan, cScanLines,  lpvBits, lpbmi, uColorUse);
00139         }
00140 
00141         int SetDIBits(HDC hDC, UINT uStartScan, UINT cScanLines, CONST VOID* lpvBits, const BITMAPINFO* lpbmi, UINT uColorUse)
00142         {
00143                 return ::SetDIBits(hDC, m_bmp, uStartScan, cScanLines, lpvBits, lpbmi, uColorUse);
00144         }
00145                 
00146         bool GetSize(SIZE& size) const
00147         {
00148                 BITMAP bm = { 0 };
00149                 if(!GetBitmap(&bm))
00150                         return false;
00151                 size.cx = bm.bmWidth;
00152                 size.cy = bm.bmHeight;
00153                 return true;
00154         }               
00155         
00156 };
00157 
00159 typedef TBitmap<true> CBitmap;
00161 typedef TBitmap<false> CBitmapHandle;
00162 
00163 }
00164 
00165 }
00166 
00167 #endif
00168 
00169 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines