Advanced MFC Programming

Advanced MFC Programming Advanced MFC Programming

math.hcmuns.edu.vn
from math.hcmuns.edu.vn More from this publisher
11.04.2014 Views

Chapter 5. Common Controls "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }; NM_UPDOWN *pNMUpDown=(NM_UPDOWN*)pNMHDR; } nNewPos=pNMUpDown->iPos+pNMUpDown->iDelta; if(nNewPos >= 0 && nNewPos SetWindowText(szNumber[nNewPos]); } *pResult=0; The buddy’s text is set by calling function CWnd::SetWindowText(…). Here variable szNumber is a twodimensional character array which stores strings “Zero”, “One”, “Two”…”Nine”. First we calculate the current position of the spin control and store the result in an integer type variable nNewPos. Then we use it as an index to table szNumber, find the appropriate string, and use it to set the text of the edit control. In dialog box’s initialization stage, we need to set the range and position of the spin control. Since the edit box will display nothing by default, we also need to set its initial text: …… BOOL CCCtlDlg::OnInitDialog() { CDialog::OnInitDialog(); ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_STR))->SetRange(0, 9); ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_STR))->SetPos(0); GetDlgItem(IDC_EDIT_STR)->SetWindowText("Zero"); } return TRUE; With the above implementation, the spin’s buddy control will display text instead of numbers. 5.4 Bitmap Button Buddy String text is not the only appearance a buddy control can have. We can also implement a buddy that displays bitmaps. Because bitmap button can be easily implemented to display images, we can use it to implement spin’s buddy control rather than using edit box. Sample 5.4\CCtl demonstrates how to implement bitmap button buddy. It is based on sample 5.3\CCtl, with a new spin control IDC_SPIN_BMP and a new bitmap button IDC_BUTTON_BMP added to the application. The procedure of creating a bitmap button buddy is almost the same with creating an edit box buddy. The only difference here is that instead of creating an edit box resource, we need to add a button resource, and set its “Owner draw” style. In the sample, four bitmaps are prepared to implement the bitmap button. All of them have integer resource IDs, which are listed as follows: IDB_BITMAP_SMILE_1, IDB_BITMAP_SMILE_2, IDB_BITMAP_SMILE_3, IDB_BITMAP_SMILE_4. A CBitmapButton type variable is declared in class CCCtlDlg. In the dialog box’s initialization stage, functions CWnd::SubclassDlgItem(…), CBitmapButton::LoadBitmaps(…) and CBitmapButton:: SizeToContent() are called to initialize the bitmap button. Also, the range of the spin control is set from 0 to 3, and its initial position is set to 0: …… BOOL CCCtlDlg::OnInitDialog() { CDialog::OnInitDialog(); m_bmpBtn.SubclassDlgItem(IDC_BUTTON_BMP, this); m_bmpBtn.LoadBitmaps(IDB_BITMAP_SMILE_1); m_bmpBtn.SizeToContent(); ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_BMP))->SetRange(0, 3); ((CSpinButtonCtrl *)GetDlgItem(IDC_SPIN_BMP))->SetPos(0); return TRUE; 96

Chapter 5. Common Controls } The initially selected bitmap is IDC_BITMAP_SMILE_1. We should not load bitmaps for other states (“down”, “focused” and “disabled”) because the purpose of this button is to display images rather than executing commands. We need to change the currently loaded image upon receiving UDN_DELTAPOS notification. To change button’s associated bitmap, in the sample application, a UDN_DELTAPOS message handler is added for IDC_SPIN_BMP, which is implemented as follows: void CCCtlDlg::OnDeltaposSpinBmp(NMHDR* pNMHDR, LRESULT* pResult) { int nNewPos; NM_UPDOWN *pNMUpDown=(NM_UPDOWN*)pNMHDR; } nNewPos=pNMUpDown->iPos+pNMUpDown->iDelta; switch(nNewPos) { case 0: { m_bmpBtn.LoadBitmaps(IDB_BITMAP_SMILE_1); break; } case 1: { m_bmpBtn.LoadBitmaps(IDB_BITMAP_SMILE_2); break; } case 2: { m_bmpBtn.LoadBitmaps(IDB_BITMAP_SMILE_3); break; } case 3: { m_bmpBtn.LoadBitmaps(IDB_BITMAP_SMILE_4); break; } } m_bmpBtn.Invalidate(); *pResult=0; Although we say that the bitmap button is the buddy of the spin control, in the above implementation we see that they do not have special relationship. A spin control needs a buddy only in the case when we want the text in the buddy window to be updated automatically. If we implement this in UDN_DELTAPOS message handler, the buddy loses its meaning because we can actually set text for any control. Although this is true, here we still treat the bitmap button as the buddy of spin control because the bitmap button is under the control of the spin. 5.5 Slider A slider is a control that allows the user to select a value from pre-defined range using mouse or keyboard. A slider can be customized to have many different styles: we can put tick marks on it, set its starting and ending ranges, make the tick marks distributed linearly or non-linearly. Besides these attributes, we can also set the line size and page size of a slider, which decide the minimum distance the slider moves when the user clicks mouse on slider’s rail or hit arrow keys of the keyboard. Including Slider Control in the Application Sample 5.5\CCtl demonstrates how to use the slider control. It is a standard dialog based application generated by the Application Wizard. In the dialog box, three different sliders are implemented, they are used to show how to set tick marks, page size, line size, and implement other customizations. 97

Chapter 5. Common Controls<br />

}<br />

The initially selected bitmap is IDC_BITMAP_SMILE_1. We should not load bitmaps for other states<br />

(“down”, “focused” and “disabled”) because the purpose of this button is to display images rather than<br />

executing commands. We need to change the currently loaded image upon receiving UDN_DELTAPOS<br />

notification. To change button’s associated bitmap, in the sample application, a UDN_DELTAPOS message<br />

handler is added for IDC_SPIN_BMP, which is implemented as follows:<br />

void CCCtlDlg::OnDeltaposSpinBmp(NMHDR* pNMHDR, LRESULT* pResult)<br />

{<br />

int nNewPos;<br />

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

}<br />

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

switch(nNewPos)<br />

{<br />

case 0:<br />

{<br />

m_bmpBtn.LoadBitmaps(IDB_BITMAP_SMILE_1);<br />

break;<br />

}<br />

case 1:<br />

{<br />

m_bmpBtn.LoadBitmaps(IDB_BITMAP_SMILE_2);<br />

break;<br />

}<br />

case 2:<br />

{<br />

m_bmpBtn.LoadBitmaps(IDB_BITMAP_SMILE_3);<br />

break;<br />

}<br />

case 3:<br />

{<br />

m_bmpBtn.LoadBitmaps(IDB_BITMAP_SMILE_4);<br />

break;<br />

}<br />

}<br />

m_bmpBtn.Invalidate();<br />

*pResult=0;<br />

Although we say that the bitmap button is the buddy of the spin control, in the above implementation<br />

we see that they do not have special relationship. A spin control needs a buddy only in the case when we<br />

want the text in the buddy window to be updated automatically. If we implement this in UDN_DELTAPOS<br />

message handler, the buddy loses its meaning because we can actually set text for any control. Although<br />

this is true, here we still treat the bitmap button as the buddy of spin control because the bitmap button is<br />

under the control of the spin.<br />

5.5 Slider<br />

A slider is a control that allows the user to select a value from pre-defined range using mouse or<br />

keyboard. A slider can be customized to have many different styles: we can put tick marks on it, set its<br />

starting and ending ranges, make the tick marks distributed linearly or non-linearly. Besides these<br />

attributes, we can also set the line size and page size of a slider, which decide the minimum distance the<br />

slider moves when the user clicks mouse on slider’s rail or hit arrow keys of the keyboard.<br />

Including Slider Control in the Application<br />

Sample 5.5\CCtl demonstrates how to use the slider control. It is a standard dialog based application<br />

generated by the Application Wizard. In the dialog box, three different sliders are implemented, they are<br />

used to show how to set tick marks, page size, line size, and implement other customizations.<br />

97

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

Saved successfully!

Ooh no, something went wrong!