C++ Cbitmap HBitmap Bitmap differences and connections

  • 2020-04-02 03:06:09
  • OfStack

To load a one-bit graph, use LoadImage:

HANDLE LoadImage (HINSTANCE hinst, LPCTSTR lpszName, UINT uType, int cxDesired, int CyDesired, UINT fuLoad);
LoadImage can be used to load bitmaps, ICONS, and cursors

The size of the map to memory of the load graph can be specified when loading:

      CxDesired: specifies the width of the icon or cursor, in pixels. If this parameter is zero and the LR_DEFAULTSIZE in the parameter fuLoad value is not used, the function USES the current resource width.

CyDesired: specifies the height of the icon or cursor, in pixels. If this parameter is zero and the LR_DEFAULTSIZE in the parameter fuLoad value is not used, the function USES the current resource height.

The return value of LoadImage is a handle to the relevant resource. Because the bitmap is loaded, the returned handle is of the HBITMAP type (casts are required).

Extended understanding of HBITMAP/CBitmap/BITMAP:

HBITMAP is a pointer to a bitmap,

In MSDN: Handle to a bitmap. Typedef Handle HBITMAP;

CBitmap is the class that encapsulates bitmap in MFC.

MSDN:

Encapsulates (include) a Windows graphics device interface (GDI) bitmap and provides member functions provides the to file (operations) the bitmap.

A BITMAP is a structure that encapsulates some information about a BITMAP. Defines the height, width, color format, and bit values of a logical bitmap.

This structure defines the type, width, height, color format, and bit values of a bitmap.

Transformation of the relationship between the three:


HBITMAP hBitmap;

CBitmap bitmap;

BITMAP bm;

//Here's the connection:

bitmap.Attach(hBitmap);//The associated CBitmap is obtained by HBITMAP

bitmap.GetBitmap(&bm); //The associated BITMAP is obtained by CBitmap
hBitmap=(HBITMAP)bitmap.GetSafeHandle();//Related HBITMAP was obtained by CBitmap

The BITMAP structure has the following form:


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

Attach/Detach:

  Attach associates a C++ object with a WINDOWS object until detach removes the association.  
  If there is no detach after attach, the WINDOWS object dies when the C++ object is destroyed.  
  After attach, there will be a mapping between the pointer to the C++ object and the HWND of the WINDOWS object, which is equivalent to you directly using a C++ object to Create a WINDOWS object, for example     CEdit     Edit;     Edit. The create (...).  
  And the mapping is permanent until the object dies.  
  If you use a function like GetDlgItem, you can also return a pointer and cast. The GetDlgItem will go to the map table.  
  There are two types of mapping tables, one permanent and one temporary.  
  WINDOWS objects created directly from C++ objects or the mapping of objects by attach are placed in the permanent table, otherwise the mapping is created in the temporary table.  
  So GetDlgItem does not recommend that you save the returned pointer, because it is difficult to ensure that your WINDOWS object and C++ object associations are in the permanent table.  
  If the map is placed in a temporary table, it is automatically deleted during idle time.  
  Attcah is all about manipulating WINDOWS objects with MFC class member functions.

The above is all the content of this article, I hope you can enjoy it.


Related articles: