|
Displaying
Bitmaps
|
|
This exercise shows how to display and change
bitmaps
with little effort.
Prerequisites:
|
- First create or load a picture in a graphics application,
such
as MS Paint or Paint Shop Pro, that can create good pictures.
Don't
use the Image Editor of MSVC. For example, here is a picture
in
Microsoft Paint (MS Paint is an application that installs with
Microsoft Windows):
- Save the picture as a bitmap (with the .bmp extension) and
remember where you save it.
|
- Start Microsoft Visual Studio or Microsoft Visual C++ and,
using MFC AppWizard (exe)
or MFC Application, create a Dialog-Based project named Bitmaps1
- Delete the TODO line on the dialog box
- To add the bitmap picture, in the Resource View, right-click
any
folder and click Import...
- In the Import Picture dialog box, change the files of type
to All
Files
- Locate the folder where you saved the picture and display it
in the
Look In combo box
- Click the name of the picture and click Import
- You should receive a message box. Read it and click OK.
Right-click the name IDB_BITMAP1 and click Properties. Change
its ID
to IDB_PICTURE1
- From the Controls toolbox, click the Picture control and
draw the
frame of the picture where you want to display the picture on
the
dialog box. Here is an example
- In the Properties window, change its ID to IDC_PICTURE
- Change its Type to Bitmap and, in the Image combo box,
select
IDB_PICTURE1 and click anywhere on the dialog box:
- As you can see, this was done without a single line of code.
You can
test the application now to see the result.
|
|
|
- Create additional pictures using MS Paint and save
the pictures as Windows Bitmap (.bmp)
Besides the above Picture1.bmp, here are the pictures we will
use
here:
- Import the pictures as we did for the above Picture1.bmp
- Change their IDs to IDB_PICTURE2, IDB_PICTURE3,
IDB_PICTURE4, and
IDB_PICTURE5
- Add a combo box under the OK button and change its ID to IDC_CHOICE
- Using Ctrl + Enter in the Data property page, create a list
as
follows:
- Set its Type to Drop List and the Owner Draw
to No
- Uncheck the Sort check box
- In the header file of the dialog, declare a private CBitmap
variable
for each picture. Here is an example:
private:
CBitmap Bmp1, Bmp2, Bmp3, Bmp4, Bmp5;
};
|
- Press Ctrl + W to display the MFC ClassWizard dialog box.
- From the Member Variables dialog box, Add a Control variable
for the
IDC_CHOICE identifier and name it m_Choice
- Add a Control variable for the IDC_PICTURE identifier
and
name it m_Picture
- Add a CBN_SELCHANGE event handler for the IDC_CHOICE
control and click Edit Code
- In the OnInitDialog() method, type the following:
BOOL CBitmaps1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
. . .
// TODO: Add extra initialization here
// TODO: Add extra initialization here
m_Choice.SetCurSel(0);
Bmp1.LoadBitmap(IDB_PICTURE1);
Bmp2.LoadBitmap(IDB_PICTURE2);
Bmp3.LoadBitmap(IDB_PICTURE3);
Bmp4.LoadBitmap(IDB_PICTURE4);
Bmp5.LoadBitmap(IDB_PICTURE5);
return TRUE; // return TRUE unless you set the focus to a control
}
|
- Implement the OnSelchange event as follows:
void CBitmaps1Dlg::OnSelchangeChoice()
{
// TODO: Add your control notification handler code here
// What is the current selection?
int CurSel = m_Choice.GetCurSel();
// Change the bitmap according to the selection
switch( CurSel )
{
case 0:
m_Picture.SetBitmap(Bmp1);
break;
case 1:
m_Picture.SetBitmap(Bmp2);
break;
case 2:
m_Picture.SetBitmap(Bmp3);
break;
case 3:
m_Picture.SetBitmap(Bmp4);
break;
case 4:
m_Picture.SetBitmap(Bmp5);
break;
}
}
|
- Test the application
|
|