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 5. Common Controls<br />

"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"<br />

};<br />

NM_UPDOWN *pNMUpDown=(NM_UPDOWN*)pNMHDR;<br />

}<br />

nNewPos=pNMUpDown->iPos+pNMUpDown->iDelta;<br />

if(nNewPos >= 0 && nNewPos SetWindowText(szNumber[nNewPos]);<br />

}<br />

*pResult=0;<br />

The buddy’s text is set by calling function CWnd::SetWindowText(…). Here variable szNumber is a twodimensional<br />

character array which stores strings “Zero”, “One”, “Two”…”Nine”. First we calculate the<br />

current position of the spin control and store the result in an integer type variable nNewPos. Then we use it<br />

as an index to table szNumber, find the appropriate string, and use it to set the text of the edit control.<br />

In dialog box’s initialization stage, we need to set the range and position of the spin control. Since the<br />

edit box will display nothing by default, we also need to set its initial text:<br />

……<br />

BOOL CCCtlDlg::OnInitDialog()<br />

{<br />

CDialog::OnInitDialog();<br />

((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_STR))->SetRange(0, 9);<br />

((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_STR))->SetPos(0);<br />

GetDlgItem(IDC_EDIT_STR)->SetWindowText("Zero");<br />

}<br />

return TRUE;<br />

With the above implementation, the spin’s buddy control will display text instead of numbers.<br />

5.4 Bitmap Button Buddy<br />

String text is not the only appearance a buddy control can have. We can also implement a buddy that<br />

displays bitmaps. Because bitmap button can be easily implemented to display images, we can use it to<br />

implement spin’s buddy control rather than using edit box.<br />

Sample 5.4\CCtl demonstrates how to implement bitmap button buddy. It is based on sample 5.3\CCtl,<br />

with a new spin control IDC_SPIN_BMP and a new bitmap button IDC_BUTTON_BMP added to the application.<br />

The procedure of creating a bitmap button buddy is almost the same with creating an edit box buddy.<br />

The only difference here is that instead of creating an edit box resource, we need to add a button resource,<br />

and set its “Owner draw” style.<br />

In the sample, four bitmaps are prepared to implement the bitmap button. All of them have integer<br />

resource IDs, which are listed as follows: IDB_BITMAP_SMILE_1, IDB_BITMAP_SMILE_2,<br />

IDB_BITMAP_SMILE_3, IDB_BITMAP_SMILE_4.<br />

A CBitmapButton type variable is declared in class CCCtlDlg. In the dialog box’s initialization stage,<br />

functions CWnd::SubclassDlgItem(…), CBitmapButton::LoadBitmaps(…) and CBitmapButton::<br />

SizeToContent() are called to initialize the bitmap button. Also, the range of the spin control is set from 0<br />

to 3, and its initial position is set to 0:<br />

……<br />

BOOL CCCtlDlg::OnInitDialog()<br />

{<br />

CDialog::OnInitDialog();<br />

m_bmpBtn.SubclassDlgItem(IDC_BUTTON_BMP, this);<br />

m_bmpBtn.LoadBitmaps(IDB_BITMAP_SMILE_1);<br />

m_bmpBtn.SizeToContent();<br />

((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_BMP))->SetRange(0, 3);<br />

((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_BMP))->SetPos(0);<br />

return TRUE;<br />

96

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

Saved successfully!

Ooh no, something went wrong!