Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 20: List-Based Controls Visual C++ and MFC Fundamentals


The first argument, pdc, specifies the device context on which you are drawing. The
nImage argument is the index of the picture you want to draw. The pt argument is a
POINT or a CPoint value that specifies the location of the new picture. The nStyle
argument is a flag that specifies how the picture will be drawn.

Practical Learning: Using an Image List



  1. From the AirCraft folder that accompanies this book, import the following bitmaps
    and change their IDs as follows:


File ID
AH64.bmp IDB_AH64
AH64SIDE.bmp IDB_AH64SIDE
AKIOWA.bmp IDB_AKIOWA
COMANCHE.bmp IDB_COMANCHE


  1. In the header file of the view class, declare a private CImageList variable and name
    it ImgList

  2. In the header file of the view class, declare another private int variable and name it
    nImage

  3. In the constructor of the view class, initialize the image list and the nImage variable
    as follows:


CAirCraft1View::CAirCraft1View()
{
// TODO: add construction code here
ImgList.Create(400, 180, ILC_COLOR, 4, 1);

CBitmap Bmp[4];

Bmp[0].LoadBitmap(IDB_AH64);
ImgList.Add(&Bmp[0], RGB(0, 0, 0));
Bmp[1].LoadBitmap(IDB_AH64SIDE);
ImgList.Add(&Bmp[1], RGB(0, 0, 0));
Bmp[2].LoadBitmap(IDB_AKIOWA);
ImgList.Add(&Bmp[2], RGB(0, 0, 0));
Bmp[3].LoadBitmap(IDB_COMANCHE);
ImgList.Add(&Bmp[3], RGB(0, 0, 0));

nImage = 0;
}


  1. In the OnPaint() event of the view class, display an image by calling the
    CImageList::Draw() method as follows:


void CAirCraft1View::OnDraw(CDC* pDC)
{
CAirCraft1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

ImgList.Draw(pDC, nImage, CPoint(255, 255, 255), ILD_NORMAL);
// TODO: add draw code for native data here
}


  1. Open the IDR_MAINFRAME menu. Under the Status Bar menu item of View, add a
    separator, followed by a new menu item captioned as &AH64 and press Enter

Free download pdf