C++ CBitmap,HBitmap,Bitmap区别及联系

Linux大全评论6K views阅读模式

加载一位图,可以使用LoadImage:

HANDLE LoadImage(HINSTANCE hinst,LPCTSTR lpszName,UINT uType,int cxDesired,int CyDesired,UINT fuLoad);

LoadImage可以用来加载位图,图标和光标

加载时可以规定加载图的映射到内存的大小:

    cxDesired:指定图标或光标的宽度,以像素为单位。如果此参数为零并且参数fuLoad值中LR_DEFAULTSIZE没有被使用,那么函数使用目前的资源宽度。

 cyDesired:指定图标或光标的高度,以像素为单位。如果此参数为零并且参数fuLoad值中LR_DEFAULTSIZE没有被使用,那么函数使用目前的资源高度。

LoadImage的返回值是相关资源的句柄。因为加载的是位图所以返回的句柄是HBITMAP型的(需要强制转换)。

延伸理解 HBITMAP/CBitmap/BITMAP:

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.

BITMAP是一个结构体,封装着bitmap的一些信息。定义了逻辑位图的高,宽,颜色格式和位值。

MSDN中如是:This structure defines the type, width, height, color format, and bit values of a bitmap.

三者之间的关系转换:

HBITMAP hBitmap;

CBitmap bitmap;

BITMAP bm;

//下面是三者之间的联系:

bitmap.Attach(hBitmap);//由HBITMAP 得到关联的CBitmap

bitmap.GetBitmap(&bm); // 由CBitmap 得到关联的BITMAP
hBitmap=(HBITMAP)bitmap.GetSafeHandle();//由CBitmap得到相关的HBITMAP

BITMAP结构具有如下形式:

typedef struct tagBITMAP
{
    int      bmType;
    int      bmWidth;//宽
      int      bmHeight;//高
      int      bmWidthBytes;
    BYTE    bmPlanes;
    BYTE    bmBitsPixel;
    LPVOID  bmBits;
}  BITMAP;

延伸理解下Attach/Detach:

企鹅博客
  • 本文由 发表于 2019年7月13日 02:59:22
  • 转载请务必保留本文链接:https://www.qieseo.com/181936.html

发表评论