11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4. Button<br />

Check here to<br />

create bitmap<br />

button<br />

Figure 4-1. Change button’s style to “Owner draw”<br />

Although every button has four states, we do not need to provide four bitmaps all the time. If one of<br />

the bitmaps is not available, class CBitmapButton will draw the button’s corresponding state using the<br />

default bitmap, which is the bitmap associated with button’s “up” state. So the bitmap used to represent a<br />

button’s “up” state is required all the time and can not be omitted.<br />

Automatic Method<br />

We have two ways of associating bitmap images with different states of a bitmap button: we can either<br />

let class CBitmapButton handle this automatically or we can do it manually. To use the automatic method,<br />

the IDs of all four bitmap resources must be text strings, and must be formed by suffixing one of the<br />

following four letters to the button’s caption text: ‘U’, ‘D’, ‘F’, ‘X’. These letters represent “up”, “down”,<br />

“focused” and “disabled” state respectively. By naming the resource IDs this way, the rest thing we need to<br />

do is calling function CBitmapButton::AutoLoad() in the dialog box’s initialization stage (within member<br />

function CDialog::OnInitDialog()). Please note that we cannot call this function in the constructor of<br />

class CDialog. At that time, the dialog box window is still not created (Therefore, the buttons are still not<br />

available), and the bitmaps cannot be associated with the button correctly.<br />

Sample<br />

Sample 4.1\Btn demonstrates how to create bitmap button using automatic method. It is a dialog-based<br />

application that is generated by the Application Wizard. First, the ID of default dialog template is changed<br />

to IDD_DIALOG_BTN. Also, the “OK” and “Cancel” buttons originally included in the template are deleted.<br />

Then a new button IDC_PLAY is added, whose caption text is set to “Play” (Figure 4-2). Since the button<br />

will be drawn using the bitmaps, it doesn’t matter how big the button resource is. Besides this, we need to<br />

set button’s style to “Owner draw”.<br />

Set the button’s<br />

caption text to<br />

“Play”<br />

Figure 4-2. Add a button in the dialog template to create bitmap button<br />

Two bitmap resources are added to the application whose IDs are “PLAYU” and “PLAYD”<br />

respectively (Figure 4-3). They correspond to button’s “up” and “down” states. In addition, the sizes of the<br />

two bitmaps are exactly the same.<br />

A CBitmapButton type variable is declared in class CBtnDlg to implement this bitmap button:<br />

70

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!