class CBitmap : public CGdiObject { DECLARE_DYNAMIC(CBitmap) public: static CBitmap* PASCAL FromHandle(HBITMAP hBitmap); // Constructors CBitmap(); BOOL LoadBitmap(LPCTSTR lpszResourceName); BOOL LoadBitmap(UINT nIDResource); BOOL LoadOEMBitmap(UINT nIDBitmap); // for OBM_/OCR_/OIC_ #ifndef _AFX_NO_AFXCMN_SUPPORT BOOL LoadMappedBitmap(UINT nIDBitmap, UINT nFlags = 0, LPCOLORMAP lpColorMap = NULL, int nMapSize = 0); #endif BOOL CreateBitmap(int nWidth, int nHeight, UINT nPlanes, UINT nBitcount, const void* lpBits); BOOL CreateBitmapIndirect(LPBITMAP lpBitmap); BOOL CreateCompatibleBitmap(CDC* pDC, int nWidth, int nHeight); BOOL CreateDiscardableBitmap(CDC* pDC, int nWidth, int nHeight); // Attributes operator HBITMAP() const; int GetBitmap(BITMAP* pBitMap); // Operations DWORD SetBitmapBits(DWORD dwCount, const void* lpBits); DWORD GetBitmapBits(DWORD dwCount, LPVOID lpBits) const; CSize SetBitmapDimension(int nWidth, int nHeight); CSize GetBitmapDimension() const; // Implementation public: virtual ~CBitmap(); #ifdef _DEBUG virtual void Dump(CDumpContext& dc) const; #endif };
父
CGdiObjectclass CGdiObject : public CObject { DECLARE_DYNCREATE(CGdiObject) public: // Attributes HGDIOBJ m_hObject; // must be first data member operator HGDIOBJ() const; HGDIOBJ GetSafeHandle() const; static CGdiObject* PASCAL FromHandle(HGDIOBJ hObject); static void PASCAL DeleteTempMap(); BOOL Attach(HGDIOBJ hObject); HGDIOBJ Detach(); // Constructors CGdiObject(); // must Create a derived class object BOOL DeleteObject(); // Operations #pragma push_macro("GetObject") #undef GetObject int _AFX_FUNCNAME(GetObject)(int nCount, LPVOID lpObject) const; int GetObject(int nCount, LPVOID lpObject) const; #pragma pop_macro("GetObject") UINT GetObjectType() const; BOOL CreateStockObject(int nIndex); BOOL UnrealizeObject(); BOOL operator==(const CGdiObject& obj) const; BOOL operator!=(const CGdiObject& obj) const; // Implementation public: virtual ~CGdiObject(); #ifdef _DEBUG virtual void Dump(CDumpContext& dc) const; virtual void AssertValid() const; #endif };
1 裝載已導入工程的位圖資源
// 裝載位圖 CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP);
2 裝載位圖文件
為了能讓CBitmap能夠裝載位圖文件,必須調用API函數LoadImage
HANDLE LoadImage( HINSTANCE hinst, // handle of the instance containing the image LPCTSTR lpszName, // name or identifier of image UINT uType, // type of image int cxDesired, // desired width int cyDesired, // desired height UINT fuLoad // load flags );
裝載: Example 1:
HBITMAP hBmp = (HBITMAP)LoadImage(NULL, m_fileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_DEFAULTSIZE);
Example 2:
HBITMAP hBmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), "BG.bmp", IMAGE_BITMAP, 0,0, LR_LOADFROMFILE);
將裝載後得到的HBITMAP資源句柄 與 CBitmap 對象 相連
if (hBmp != NULL) { CBitmap *pBmp = CBitmap::FromHandle(hBmp); }
或
CBitmap bmp; if (hBmp != NULL) { bmp.DeleteObject(); bmp.Attach(hBmp); }3 顯示位圖
CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP1); BITMAP bm; bmp.GetBitmap(&bm); CDC dc; dc.CreateCompatibleDC(pDC); CBitmap*pOldBmp=(CBitmap *)dc.SelectObject(&bmp); pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dc,0,0,SRCCOPY); pDC->SelectObject(pOldBmp); bmp.DeleteObject(); bmp.LoadBitmap(IDB_BITMAP2);
4 刪除資源
CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP); CBitmap *pOld=pDC->SelectObject(&bmp); // 此時位圖對象還在pDC中,因此不能馬上刪除 // 而是先將位圖從DC中選出 然後再刪除 pDC->SelectObject(pOld); bmp.DeleteObject();
5 CBitmap 析構
當CBitmap作為局部變量 在其退出作用范圍後,會發生析構,這時候CBitmap會將其對應的位圖資源(hBitmap )釋放掉。
若想繼續使用該位圖資源hBitmap,則在退出作用范圍前,應將位圖資源hBitmap和CBitmap對象通過Detach()函數進行分離
HBITMAP CMyClass::Load() { CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP); // 通過Detach 將資源與對象分離,這樣bmp析構後,資源仍存在 // 否則 ,bmp析構時,會將位圖資源一起析構掉,這樣出了局部范圍外,就不可再使用這個位圖資源了 return bmp.Detach(); }
6 在僅獲得HBITMAP資源句柄情況下,如何獲得這個資源的BITMAP信息
BITMAP bm; GetObject(hBitmap,sizeof(BITMAP),&bm);7 在內存中開辟資源空間 將原圖保存到內存中
//-------------------在內存中建立區域以存放所得位圖------------------- // hBitmapSrc 為 CBitmap中保存的矩形原圖資源句柄 // hDC 句柄 // 在內存中開辟位圖資源,用以保存原圖 HBITMAP CopyHBitmap(HBITMAP hBitmapSrc,HDC hDC) { BITMAP bm; HBITMAP hBitmapDst; HDC hdcSrc,hdcDst; GetObject(hBitmapSrc,sizeof(BITMAP),&bm); hBitmapDst=CreateCompatibleBitmap(hDC,bm.bmWidth,bm.bmHeight); hdcSrc=CreateCompatibleDC(hDC); hdcDst=CreateCompatibleDC(hDC); SelectObject(hdcSrc,hBitmapSrc); SelectObject(hdcDst,hBitmapDst); BitBlt(hdcDst,0,0,bm.bmWidth,bm.bmHeight,hdcSrc,0,0,SRCCOPY); DeleteDC(hdcSrc); DeleteDC(hdcDst); return hBitmapDst; }