Advanced MFC Programming
Advanced MFC Programming Advanced MFC Programming
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
- Page 61 and 62: Chapter 2. Menu void CMenuDoc::OnEd
- Page 63 and 64: Chapter 2. Menu } In the sample, bi
- Page 65 and 66: Chapter 2. Menu Message Mapping for
- Page 67 and 68: Chapter 2. Menu We can implement bi
- Page 69 and 70: Chapter 2. Menu CDC *ptrDC; CDC dcM
- Page 71 and 72: Chapter 2. Menu Because the popup m
- Page 73 and 74: Chapter 3. Splitter Window Chapter
- Page 75 and 76: Chapter 3. Splitter Window function
- Page 77 and 78: Chapter 3. Splitter Window BOOL CSp
- Page 79 and 80: Chapter 3. Splitter Window In the a
- Page 81 and 82: Chapter 3. Splitter Window Sample S
- Page 83 and 84: Chapter 3. Splitter Window WM_LBUTT
- Page 85 and 86: Chapter 4. Button Chapter 4 Buttons
- Page 87 and 88: Chapter 4. Button Use string text a
- Page 89 and 90: Chapter 4. Button function, first t
- Page 91 and 92: Chapter 4. Button Now we can remove
- Page 93 and 94: Chapter 4. Button } ( m_bBmpCheck ?
- Page 95 and 96: Chapter 4. Button using bit-wise AN
- Page 97 and 98: Chapter 4. Button Overriding Functi
- Page 99 and 100: Chapter 4. Button …… First pBit
- Page 101 and 102: Chapter 4. Button Using Class MCBit
- Page 103 and 104: Chapter 4. Button The button’s ID
- Page 105 and 106: Chapter 4. Button However, since me
- Page 107 and 108: Chapter 4. Button Here, we use subc
- Page 109 and 110: Chapter 5. Common Controls command
- Page 111: Chapter 5. Common Controls ((CSpinB
- Page 115 and 116: Chapter 5. Common Controls …… B
- Page 117 and 118: Chapter 5. Common Controls The “S
- Page 119 and 120: Chapter 5. Common Controls button
- Page 121 and 122: Chapter 5. Common Controls In this
- Page 123 and 124: Chapter 5. Common Controls (CBN_CLO
- Page 125 and 126: Chapter 5. Common Controls compare
- Page 127 and 128: Chapter 5. Common Controls } ptrEdi
- Page 129 and 130: Chapter 5. Common Controls ……
- Page 131 and 132: Chapter 5. Common Controls void CWn
- Page 133 and 134: Chapter 5. Common Controls Five loc
- Page 135 and 136: Chapter 5. Common Controls This fun
- Page 137 and 138: Chapter 5. Common Controls After cr
- Page 139 and 140: Chapter 5. Common Controls typedef
- Page 141 and 142: Chapter 5. Common Controls Using th
- Page 143 and 144: Chapter 5. Common Controls Paramete
- Page 145 and 146: Chapter 5. Common Controls void MCT
- Page 147 and 148: Chapter 5. Common Controls modifica
- Page 149 and 150: Chapter 5. Common Controls In funct
- Page 151 and 152: Chapter 5. Common Controls Changing
- Page 153 and 154: Chapter 5. Common Controls m_tabCtr
- Page 155 and 156: Chapter 5. Common Controls Custom R
- Page 157 and 158: Chapter 6. Dialog Box Chapter 6 Dia
- Page 159 and 160: Chapter 6. Dialog Box void CMLDialo
- Page 161 and 162: Chapter 6. Dialog Box } Function CP
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