11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4. Button<br />

Now we can remove or modify bitmap button IDC_PLAY’s caption text “Play. Actually, it doesn’t<br />

matter if the button has caption text or not. By compiling the application and executing it at this point, we<br />

will see that the bitmap button implemented here is exactly the same as the one implemented in the<br />

previous sample.<br />

4.4 Bitmap Check Box and Radio Button: Method 2<br />

In sample 4.2\Btn, although we can represent the checked and unchecked states of a check box or a<br />

radio button using different bitmaps, we could not customize their “focused” state. When a button has the<br />

current focus, a rectangle with dashed border will always be put over button’s face (Figure 4-5). This is<br />

because we use CButton instead of CBitmapButton to implement buttons, this allows only one bitmap to be<br />

associated with a button.<br />

A rectangle with<br />

dashed border will<br />

appear whenever the<br />

control has current<br />

focus<br />

Figure 4-5. Bitmap check box and radio buttons implemented using class CButton<br />

To improve this, we can use class CBitmapButton to create both check box and radio button. By doing<br />

this, the button’s focused state will be implemented using the bitmap image provided by the programmer<br />

instead of drawing a rectangle with dashed border over button’s face. Since class CBitmapButton supports<br />

only push button implementation, we need to change the bitmap by ourselves to imitate check box and<br />

radio button.<br />

Sample 4.4\Btn is based on sample 4.3\Btn. In this sample, three new buttons are added to the dialog<br />

template: one will be implemented as a check box; the other two will be implemented as radio buttons. All<br />

of them will be based on class CBitmapButton.<br />

First, we need a Boolean type variable for each check box and radio button to represent its current<br />

state. This variable toggles between TRUE and FALSE, indicating if the button is currently checked or<br />

unchecked. When the state of a button changes, we re-associate the button with an alternate bitmap and<br />

paint the bitmap button again.<br />

Since we use push button to implement check box and radio button, we can not call function<br />

CButton::GetCheck(…) to examine if the button is currently checked or not. This is because a push button<br />

will automatically resume to the “up” state after the mouse is released.<br />

In the sample application, three new buttons are added to the dialog template IDD_BTN_DIALOG, and<br />

their corresponding IDs are IDC_BMP_CHECK, IDC_BMP_RADIO_A, IDC_BMP_RADIO_B respectively. Also, they<br />

all have a “Owner draw” style. Besides the new controls, two new bitmap resources are also added to the<br />

application, which will be used to implement button’s “Checked” and “Unchecked” states. The IDs of the<br />

new bitmap resources are IDB_BITMAP_BTNCHECK and IDB_BITMAP_BTNUNCHECK. The difference between the<br />

new bitmaps and two old ones (whose IDs are IDB_BITMAP_CHECK and IDB_BITMAP_UNCHECK) is that the<br />

new bitmaps have a 3-D effect. In sample 4.2\Btn, the check box and radio buttons are implemented using<br />

class CButton, which automatically adds 3-D effect to the controls. Since we want the controls to be<br />

implemented solely by programmer-provided bitmaps, we need to add 3-D effect by ourselves.<br />

In the sample application, three new CBitmapButton type variables are declared in class CBtnDlg. Also,<br />

a new member function SetRadioBitmap() is added to associate bitmaps with the two radio buttons. This<br />

function will be called when one of the radio buttons is clicked by mouse. For the check box, associating<br />

bitmap with it is relatively simple, so it is implemented within the message handler. Besides this, a new<br />

Boolean type variable CBtnDlg::m_bBmpCheck is declared to indicate the current state of the check box, and<br />

an unsigned integer CBtnDlg::m_uBmpRadio is declared to indicate which radio button is being selected.<br />

For each button, WM_COMMAND message handler is added through using Class Wizard. These message<br />

handlers are CBtnDlg::OnBmpCheck(), CBtnDlg::OnBmpRadioA() and CBtnDlg::OnBmpRadioB()<br />

respectively. The following code fragment shows the new members added to the class:<br />

75

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

Saved successfully!

Ooh no, something went wrong!