Delen via


CStatic::SetBitmap

Associates a new bitmap with the static control.

HBITMAP SetBitmap(
   HBITMAP hBitmap 
);

Parameters

  • hBitmap
    Handle of the bitmap to be drawn in the static control.

Return Value

The handle of the bitmap that was previously associated with the static control, or NULL if no bitmap was associated with the static control.

Remarks

The bitmap will be automatically drawn in the static control. By default, it will be drawn in the upper-left corner and the static control will be resized to the size of the bitmap.

You can use various window and static control styles, including these:

  • SS_BITMAP   Use this style always for bitmaps.

  • SS_CENTERIMAGE   Use to center the image in the static control. If the image is larger than the static control, it will be clipped. If it is smaller than the static control, the empty space around the image will be filled by the color of the pixel in the upper left corner of the bitmap.

  • MFC provides the class CBitmap, which you can use when you have to do more with a bitmap image than just call the Win32 function LoadBitmap. CBitmap, which contains one kind of GDI object, is often used in cooperation with CStatic, which is a CWnd class that is used for displaying a graphic object as a static control.

CImage is an ATL/MFC class that lets you more easily work with device independent bitmaps (DIB). For more information, see CImage Class.

  • Typical usage is to give CStatic::SetBitmap a GDI object that is returned by the HBITMAP operator of a CBitmap or CImage object. The code to do this resembles the following line.
MyStaticControl.SetBitmap(HBITMAP(MyBitmap));

The following example creates two CStatic objects on the heap. It then loads one with a system bitmap using CBitmap::LoadOEMBitmap and the other from a file using CImage::Load.

Example

// Code such as this could be placed in the OnInitDialog callback. 
// It creates two bitmap static controls on the heap, using members 
// _m_pCStatic_A and _m_pCStatic_B to identify them so that they can 
// be destroyed when no longer needed.

  CBitmap CBmp;
  CImage CImg;

  // Create a child bitmap static control and load it from a CBitmap object.
  _m_pCStatic_A = new CStatic;
  _m_pCStatic_A->Create(_T("A bitmap static control (A)"), 
      WS_CHILD|WS_BORDER|WS_VISIBLE|SS_BITMAP|SS_CENTERIMAGE, CRect(16,16,64,64),
      pParentWnd);
  CBmp.LoadOEMBitmap(OBM_CLOSE);  // Loads one of the default Windows bitmaps
  _m_pCStatic_A->SetBitmap( HBITMAP(CBmp) );
  _m_pCStatic_A->ShowWindow( SW_SHOW );

  // Create a child bitmap static control and load it from a CImage object.
  _m_pCStatic_B = new CStatic;
  _m_pCStatic_B->Create(_T("A bitmap static control (B)"), 
      WS_CHILD|WS_BORDER|WS_VISIBLE|SS_BITMAP|SS_CENTERIMAGE, CRect(90,16,138,64),
      pParentWnd);
  CImg.Load( _T("test.png") );
  if( _m_pCStatic_B->GetBitmap( ) == NULL )
    _m_pCStatic_B->SetBitmap( HBITMAP(CImg) );

  /* Then, later: 
   delete( _m_pCStatic_A );
   delete( _m_pCStatic_B );
   */

Requirements

Header: afxwin.h

See Also

Reference

CStatic Class

Hierarchy Chart

CStatic::GetBitmap

STM_SETIMAGE

Bitmaps

CBitmap Class

CImage Class