【转】CBitmap和HBITMAP的区别及相互转换方法

hbitmap是bitmap的指针,

msdn中:Handle to a bitmap.typedef HANDLE HBITMAP;

cbitmap是mfc中封装bitmap的类;

msdn中:Encapsulates(囊括) a Windows graphics device interface (GDI)bitmap and provides member functions to manipulate(操作) the bitmap.

class CBitmap : public CgdiObject 

 转化方法两种,Attach和FromHandle:

 

1 CBitmap bmp;
2 bmp.LoadBitmap(IDB_BITMAP1);
3 HBITMAP hbm = (HBITMAP)bmp;
4 CBitmap bmp1;
5 bmp1.Attach(hbm);
6
7 HBITMAP = CBitmap.m_hObject
8 CBitmap* = CBitmap.FromHandle(HBITMAP)
9
10  //已知HBITMAP hbit;
11  CBitmap cb;
12 cb.FromHandle(hbit);
13
14  //已知CBitmap cb;
15  HBITMAP hbit=(HBITMAP)cb;
16
17 CBitmap bmp;
18 HBITMAP hBmp;
19  //相互转换:
20  hBmp=(HBITMAP)bmp.GetSafeHandle();
21 bmp.Attach(hBmp);

注意点:

Attach和FromHandle的区别

FromHandle得到的指针是临时变量,

,通过Attach连接的句柄可以长久保留,但通过FromHandle得到的只是暂时的,

大概只在一个消息区间内有效,很快便会被删除,所以基本上不能用。

我用了FromHandle然后一直出错!!!

from:http://blog.csdn.net/newstarao/archive/2009/05/22/4209656.aspx

原文地址:https://www.cnblogs.com/dahai/p/1762422.html