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 />

can find out whether the current state of the check box is “Checked” or “Unchecked”. Based on this<br />

information, we can decide which bitmap should be used.<br />

Sample 4.2\Btn demonstrates the above method. It is based on sample 4.1\Btn. In the sample, three<br />

new buttons are added: one of them is implemented as a check box; the rest are implemented as radio<br />

buttons. The following describes how the bitmap check box and radio buttons are implemented in the<br />

sample:<br />

1) Add a check box and two radio buttons to the dialog template. Name the IDs of new controls<br />

IDC_CHECK, IDC_RADIO_A and IDC_RADIO_B respectively. In the property sheet that lets us customize<br />

control’s properties, check “Bitmap” check box (Figure 4-4).<br />

Figure 4-4. Set properties for bitmap check box control<br />

2) Add two bitmap resources, one for checked state and one for unchecked state. Their resource IDs are<br />

ID_BITMAP_CHECK and ID_BITMAO_UNCHECK respectively. The bitmaps must have a same size.<br />

3) Declare two CBitmap type variables m_bmpCheck and m_bmpUnCheck in class CBtnDlg, in function<br />

CBtnDlg::OnInitDlg(), call CBitmap::LoadBitmap(…) to load the two bitmap resources. Then call<br />

function CButton::SetBitmap(…) to set bitmap for the check box and radio buttons. In the sample, all<br />

of the new controls are initialized to unchecked state (In order to do this, we need to associate buttons<br />

with m_bmpUnCheck instead of m_bmpCheck). The following code fragment shows the modified class<br />

CBtnDlg and the function CBtnDlg::OnInitDialog(…):<br />

……<br />

……<br />

……<br />

……<br />

……<br />

class CBtnDlg : public CDialog<br />

{<br />

protected:<br />

};<br />

CBitmap m_bmpCheck;<br />

CBitmap m_bmpUnCheck;<br />

BOOL CBtnDlg::OnInitDialog()<br />

{<br />

}<br />

m_bmpCheck.LoadBitmap(IDB_BITMAP_CHECK);<br />

m_bmpUnCheck.LoadBitmap(IDB_BITMAP_UNCHECK);<br />

((CButton *)GetDlgItem(IDC_CHECK))->SetBitmap<br />

(<br />

(HBITMAP)m_bmpUnCheck.GetSafeHandle()<br />

);<br />

((CButton *)GetDlgItem(IDC_RADIO_A))->SetBitmap<br />

(<br />

(HBITMAP)m_bmpUnCheck.GetSafeHandle()<br />

);<br />

((CButton *)GetDlgItem(IDC_RADIO_B))->SetBitmap<br />

(<br />

(HBITMAP)m_bmpUnCheck.GetSafeHandle()<br />

);<br />

4) Declare a new member function CBtnDlg::SetCheckBitmap(…). We will use it to set a button’s bitmap<br />

according to its current state. The function has one parameter nID that identifies the control. Within the<br />

72

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

Saved successfully!

Ooh no, something went wrong!