GDI 总结一 CBitmap类

CBitmap类:

 

[cpp]  view plain copy
  1. class CBitmap : public CGdiObject  
  2. {  
  3.     DECLARE_DYNAMIC(CBitmap)  
  4.   
  5. public:  
  6.     static CBitmap* PASCAL FromHandle(HBITMAP hBitmap);  
  7.   
  8. // Constructors  
  9.     CBitmap();  
  10.   
  11.     BOOL LoadBitmap(LPCTSTR lpszResourceName);  
  12.     BOOL LoadBitmap(UINT nIDResource);  
  13.     BOOL LoadOEMBitmap(UINT nIDBitmap); // for OBM_/OCR_/OIC_  
  14. #ifndef _AFX_NO_AFXCMN_SUPPORT  
  15.     BOOL LoadMappedBitmap(UINT nIDBitmap, UINT nFlags = 0,  
  16.         LPCOLORMAP lpColorMap = NULL, int nMapSize = 0);  
  17. #endif  
  18.     BOOL CreateBitmap(int nWidth, int nHeight, UINT nPlanes, UINT nBitcount,  
  19.             const void* lpBits);  
  20.     BOOL CreateBitmapIndirect(LPBITMAP lpBitmap);  
  21.     BOOL CreateCompatibleBitmap(CDC* pDC, int nWidth, int nHeight);  
  22.     BOOL CreateDiscardableBitmap(CDC* pDC, int nWidth, int nHeight);  
  23.   
  24. // Attributes  
  25.     operator HBITMAP() const;  
  26.     int GetBitmap(BITMAP* pBitMap);  
  27.   
  28. // Operations  
  29.     DWORD SetBitmapBits(DWORD dwCount, const void* lpBits);  
  30.     DWORD GetBitmapBits(DWORD dwCount, LPVOID lpBits) const;  
  31.     CSize SetBitmapDimension(int nWidth, int nHeight);  
  32.     CSize GetBitmapDimension() const;  
  33.   
  34. // Implementation  
  35. public:  
  36.     virtual ~CBitmap();  
  37. #ifdef _DEBUG  
  38.     virtual void Dump(CDumpContext& dc) const;  
  39. #endif  
  40. };  


 

[cpp]  view plain copy
  1. class CGdiObject : public CObject  
  2. {  
  3.     DECLARE_DYNCREATE(CGdiObject)  
  4. public:  
  5.   
  6. // Attributes  
  7.     HGDIOBJ m_hObject;                  // must be first data member  
  8.     operator HGDIOBJ() const;  
  9.     HGDIOBJ GetSafeHandle() const;  
  10.   
  11.     static CGdiObject* PASCAL FromHandle(HGDIOBJ hObject);  
  12.     static void PASCAL DeleteTempMap();  
  13.     BOOL Attach(HGDIOBJ hObject);  
  14.     HGDIOBJ Detach();  
  15.   
  16. // Constructors  
  17.     CGdiObject(); // must Create a derived class object  
  18.     BOOL DeleteObject();  
  19.   
  20. // Operations  
  21. #pragma push_macro("GetObject")  
  22. #undef GetObject  
  23.     int _AFX_FUNCNAME(GetObject)(int nCount, LPVOID lpObject) const;  
  24.     int GetObject(int nCount, LPVOID lpObject) const;  
  25. #pragma pop_macro("GetObject")  
  26.     UINT GetObjectType() const;  
  27.     BOOL CreateStockObject(int nIndex);  
  28.     BOOL UnrealizeObject();  
  29.     BOOL operator==(const CGdiObject& obj) const;  
  30.     BOOL operator!=(const CGdiObject& obj) const;  
  31.   
  32. // Implementation  
  33. public:  
  34.     virtual ~CGdiObject();  
  35. #ifdef _DEBUG  
  36.     virtual void Dump(CDumpContext& dc) const;  
  37.     virtual void AssertValid() const;  
  38. #endif  
  39. };  

 

 


 

1 装载已导入工程的位图资源

 

[cpp]  view plain copy
  1. // 装载位图  
  2.   
  3. CBitmap bmp;  
  4. bmp.LoadBitmap(IDB_BITMAP);  


 

2 装载位图文件

    为了能让CBitmap能够装载位图文件,必须调用API函数LoadImage

 

[cpp]  view plain copy
  1. HANDLE LoadImage(  
  2.   HINSTANCE hinst,   // handle of the instance containing the image  
  3.   LPCTSTR lpszName,  // name or identifier of image  
  4.   UINT uType,        // type of image  
  5.   int cxDesired,     // desired width  
  6.   int cyDesired,     // desired height  
  7.   UINT fuLoad        // load flags  
  8. );  

 

装载:

 Example 1:

[cpp]  view plain copy
  1. HBITMAP hBmp = (HBITMAP)LoadImage(NULL,  
  2.     m_fileName,  
  3.     IMAGE_BITMAP,   
  4.     0, 0,   
  5.     LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_DEFAULTSIZE);  

 

Example 2:

[cpp]  view plain copy
  1.   HBITMAP   hBmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),  
  2. "BG.bmp",  
  3. IMAGE_BITMAP,  
  4. 0,0,  
  5. LR_LOADFROMFILE);  


将装载后得到的HBITMAP资源句柄 与 CBitmap 对象 相连

 

[cpp]  view plain copy
  1. if (hBmp != NULL) {  
  2.     CBitmap *pBmp = CBitmap::FromHandle(hBmp);  
  3. }  


     OR

[cpp]  view plain copy
  1. CBitmap bmp;  
  2. if (hBmp != NULL) {  
  3.     bmp.DeleteObject();  
  4.     bmp.Attach(hBmp);     
  5. }  


 

3 显示位图

 

 

[cpp]  view plain copy
  1. CBitmap bmp;  
  2. bmp.LoadBitmap(IDB_BITMAP1);  
  3.   
  4. BITMAP bm;  
  5. bmp.GetBitmap(&bm);  
  6.   
  7. CDC dc;  
  8. dc.CreateCompatibleDC(pDC);  
  9. CBitmap*pOldBmp=(CBitmap *)dc.SelectObject(&bmp);  
  10.   
  11. pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dc,0,0,SRCCOPY);  
  12. pDC->SelectObject(pOldBmp);  
  13.   
  14. bmp.DeleteObject();  
  15. bmp.LoadBitmap(IDB_BITMAP2);  


4 删除资源

 

错误方式:

[cpp]  view plain copy
  1. CBitmap bmp;  
  2. bmp.LoadBitmap(IDB_BITMAP);  
  3.   
  4. CBitmap *pOld=pDC->SelectObject(&bmp);  
  5.   
  6. // 此时位图对象还在pDC中,因此不能删除  下面的语句是错误的  
  7. bmp.DeleteObject();  


 

正确方式:

[cpp]  view plain copy
  1. CBitmap bmp;  
  2. bmp.LoadBitmap(IDB_BITMAP);  
  3.   
  4. CBitmap *pOld=pDC->SelectObject(&bmp);  
  5.   
  6. // 此时位图对象还在pDC中,因此不能马上删除  
  7. // 而是先将位图从DC中选出 然后再删除  
  8. pDC->SelectObject(pOld);  
  9. bmp.DeleteObject();  


 

5 CBitmap 析构

 

当CBitmap作为局部变量 在其退出作用范围后,会发生析构,这时候CBitmap会将其对应的位图资源(hBitmap )释放掉。

若想继续使用该位图资源hBitmap,则在退出作用范围前,应将位图资源hBitmap和CBitmap对象通过Detach()函数进行分离 

 

[cpp]  view plain copy
  1. HBITMAP  CMyClass::Load()  
  2. {  
  3.     CBitmap bmp;  
  4.     bmp.LoadBitmap(IDB_BITMAP);  
  5.   
  6.     // 通过Detach 将资源与对象分离,这样bmp析构后,资源仍存在    
  7.     // 否则 ,bmp析构时,会将位图资源一起析构掉,这样出了局部范围外,就不可再使用这个位图资源了  
  8.     return bmp.Detach();  
  9. }  


 

6 在仅获得HBITMAP资源句柄情况下,如何获得这个资源的BITMAP信息

 

[cpp]  view plain copy
  1. BITMAP bm;  
  2. GetObject(hBitmap,sizeof(BITMAP),&bm);  


 

 7 在内存中开辟资源空间 将原图保存到内存中

 

[cpp]  view plain copy
  1. //-------------------在内存中建立区域以存放所得位图-------------------  
  2. // hBitmapSrc 为 CBitmap中保存的矩形原图资源句柄  
  3. // hDC 句柄     
  4. // 在内存中开辟位图资源,用以保存原图  
  5. HBITMAP CopyHBitmap(HBITMAP hBitmapSrc,HDC hDC)  
  6. {  
  7.       
  8.     BITMAP bm;  
  9.     HBITMAP hBitmapDst;  
  10.     HDC hdcSrc,hdcDst;  
  11.   
  12.     GetObject(hBitmapSrc,sizeof(BITMAP),&bm);  
  13.     hBitmapDst=CreateCompatibleBitmap(hDC,bm.bmWidth,bm.bmHeight);  
  14.   
  15.     hdcSrc=CreateCompatibleDC(hDC);  
  16.     hdcDst=CreateCompatibleDC(hDC);  
  17.   
  18.     SelectObject(hdcSrc,hBitmapSrc);   
  19.     SelectObject(hdcDst,hBitmapDst);  
  20.   
  21.     BitBlt(hdcDst,0,0,bm.bmWidth,bm.bmHeight,hdcSrc,0,0,SRCCOPY);  
  22.       
  23.     DeleteDC(hdcSrc);  
  24.     DeleteDC(hdcDst);     
  25.     return hBitmapDst;  
  26.   
  27. }  


 

[cpp]  view plain copy
  1. ON_MESSAGE(WM_PIC,OnPic)  

 

[cpp]  view plain copy
  1. (theApp.m_pMainWnd)->PostMessage(WM_PIC,(WPARAM)CopyHBitmap(hBitmap,IMG_HDC),0);  

 

[cpp]  view plain copy
  1. void OnPic(HBITMAP hBitmap)  


 

 转自:http://blog.csdn.net/shuilan0066/article/details/7079973

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值