?? ch16.htm
字號:
16.2 shows the Controls dialog box after you add the slider control.</P><P><A NAME="02"></A><A HREF="02.htm"><B>Figure 16.2.</B></A> <I><BR>The main dialog box from the Controls project after you add a slider.</I></P><P>Open the properties dialog box for the slider control and change the resourceID to <TT>IDC_SLIDER</TT>. All other properties can remain set to their default valuesfor now. In the next section, you learn about the properties offered for slider controls.<H3><FONT COLOR="#000077"><B>Slider Control Properties</B></FONT></H3><P>The Properties dialog box for a slider control contains many of the same optionsoffered for up-down controls, as well as a few that are exclusive to slider controls.The available options include the following:<UL> <LI><I>ID</I> is used for the slider's resource ID. A default resource ID, such as <TT>IDC_SLIDER1</TT>, is supplied by Developer Studio.<BR> <BR> <LI><I>Visible</I> is used to indicate that the control is initially visible. This check box is usually checked.<BR> <BR> <LI><I>Disabled</I> is used to indicate the control should be initially disabled. This check box is usually cleared.<BR> <BR> <LI><I>Group </I>is used to mark the first control in a group. This check box is usually cleared.<BR> <BR> <LI><I>Tab Stop </I>indicates that this control can be reached by pressing Tab on the keyboard. This check box is usually checked.<BR> <BR> <LI><I>Help ID </I>indicates that a context-sensitive help ID should be generated for this control.<BR> <BR> <LI><I>Orientation</I> is used to specify if the slider is vertical or horizontal. The default value is vertical.<BR> <BR> <LI><I>Point</I> is used to indicate the position of optional tick marks. There are three options: Top/Left, Bottom/Right, or Both. The default value is Bottom/Right.<BR> <BR> <LI><I>Tick Marks</I> indicates that tick marks should be drawn for the slider. This check box is usually cleared.<BR> <BR> <LI><I>Auto Ticks</I> indicates that tick marks should be drawn at intervals along the slider control. This option check box is usually cleared.<BR> <BR> <LI><I>Enable Selection</I> enables the slider to be used to select a range of values. This check box is usually cleared.<BR> <BR> <LI><I>Border</I> is used to specify that a border should be drawn around the control. This check box is usually checked.</UL><P>In the next section, you use a slider to control a progress control. To preparefor that example, open the properties dialog box and make sure the following sliderproperties are selected:<UL> <LI>Tick Marks <LI>Auto Ticks <LI>Enable Selection</UL><H2><FONT COLOR="#000077"><B>Using the Progress Control</B></FONT></H2><P>A progress control, also known as a progress bar, is commonly used to indicatethe progress of an operation and is usually filled from left to right as the operationis completed. You can also use progress controls to indicate temperature, water level,or similar measurements. In fact, an early term for this type of control was "GasGauge," back in the old days when programmers had mules and most Windows programswere written in C.</P><P>Progress controls are used in Developer Studio to indicate the progress of savingor loading a project workspace. Progress controls are also used by the Windows Explorerwhen copying or moving files.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Progress controls are an easy way to give feedback to the user about the status of a task. Instead of waiting an unknown length of time, the user can see what portion of a job has yet to be completed. <HR></BLOCKQUOTE><P>A progress control is added to a dialog box in the same way as the up-down andslider controls discussed earlier. Using the Developer Studio dialog box editor,add a progress control to the Controls project main dialog box. Figure 16.3 showsthe main dialog box from the Controls project after the progress control has beenadded.</P><P><A NAME="03"></A><A HREF="03.htm"><B>Figure 16.3.</B> </A><I><BR>The Controls dialog box after adding the progress control.</I></P><P>After you add the control, open the properties dialog box and change the resourceID to <TT>IDC_PROGRESS</TT>. A progress control doesn't have optional propertiesother than those available on all controls:<UL> <LI><I>ID</I> is used for the progress control's resource ID. A default resource ID, such as <TT>IDC_PROGRESS1</TT>, is supplied by Developer Studio.<BR> <BR> <LI><I>Visible</I> is used to indicate that the control is initially visible. This check box is usually checked.<BR> <BR> <LI><I>Disabled</I> is used to indicate the control should be initially disabled. This check box is usually cleared.<BR> <BR> <LI><I>Group</I> is used to mark the first control in a group. This check box is usually cleared.<BR> <BR> <LI><I>Tab Stop</I> indicates that this control can be reached by pressing Tab on the keyboard. This check box is usually checked.<BR> <BR> <LI><I>Help ID</I> indicates that a context-sensitive help ID should be generated for this control.<BR> <BR> <LI><I>Border</I> is used to specify that a border should be drawn around the control. This check box is usually checked.</UL><P>For this example, you can set the progress control properties to their defaultvalues.<H3><FONT COLOR="#000077"><B>Using a Slider to Update a Progress Control</B></FONT></H3><P>In this section, you use the <TT>IDC_SLIDER</TT> slider control to change thevalue displayed by the progress control. Using ClassWizard, add two new member variablesassociated with the slider and progress controls to the <TT>CControlsDlg</TT> class.Use the values from Table 16.3 for the new controls.<H4><FONT COLOR="#000077">Table 16.3. Values for slider and progress control membervariables.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Control ID</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Variable Name</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Category</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Type</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_SLIDER</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>m_slider</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>Control</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CSliderCtrl</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_PROGRESS</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>m_progress</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>Control</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CProgressCtrl</TT></TD> </TR></TABLE><H3><FONT COLOR="#000077"><B>Initializing the Slider and Progress Controls</B></FONT></H3><P>The slider and progress controls must be initialized before you can use them.The <TT>CProgressCtrl</TT> and <TT>CSliderCtrl</TT> classes each provide a <TT>SetRange</TT>function that is used to set minimum and maximum values for their respective controls.</P><PRE><FONT COLOR="#0066FF"><TT>m_slider.SetRange( 0, 100 );</TT></FONT></PRE><P>The slider also enables tick marks to be placed along the slider control if theAutoticks check box has been selected. Use the <TT>SetTicFreq</TT> function to specifythe distance between each tick mark. To add tick marks every 10 positions, pass avalue of 10 to <TT>SetTicFreq</TT>.</P><PRE><FONT COLOR="#0066FF"><TT>m_slider.SetTicFreq( 10 );</TT></FONT></PRE><P>Listing 16.2 contains new source code for the initialization section of <TT>OnInitDialog</TT>.Add this source code just after the <TT>// TODO</TT> comment.<H4><FONT COLOR="#000077">TYPE: Listing 16.2. Initializing the controls in CControlsDlg::OnInitDialog.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>// TODO: Add extra initialization here</TT><TT>m_spin.SetRange( 0, 100 );</TT><TT>m_slider.SetRange( 0, 100 );</TT><TT>m_slider.SetTicFreq( 10 );</TT></FONT></PRE><P><TT>m_progress.SetRange( 0, 100 );</TT><H3><FONT COLOR="#000077"><B>Handling Messages from the Slider to the Progress Control</B></FONT></H3><P>When a slider is moved, it notifies its parent using <TT>WM_SCROLL</TT> and <TT>WM_HSCROLL</TT>messages. Because the slider in this example is a horizontal slider, it sends <TT>WM_HSCROLL</TT>messages to the main dialog box. Using ClassWizard, add a message-handling functionto the <TT>CControlsDlg</TT> class for the <TT>WM_HSCROLL</TT> message. The sourcecode for the <TT>OnHScroll</TT> function is provided in Listing 16.3.<H4><FONT COLOR="#000077">TYPE: Listing 16.3. Using slider scroll messages to updatethe progress control.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>void CControlsDlg::OnHScroll(UINT nSBCode, UINT nPos,</TT><TT> CScrollBar* pScrollBar )</TT><TT>{</TT><TT> int nSliderPos = m_slider.GetPos();</TT><TT> m_progress.SetPos( nSliderPos );</TT></FONT></PRE><P><TT>}</TT> The code in Listing 16.3 is called whenever the trackbar position ischanged. The <TT>CSliderCtrl::GetPos</TT> function is used to collect the currentslider position, which is then used to update the progress control using the <TT>CProgressCtrl::SetPos</TT>function.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>There are many ways to use the progress control. Many times, you will update the progress control after receiving events that you don't have direct control over. For example, when copying a large number of files, your program might update a progress control after copying each file. During an installation, you might want to update a progress control after each phase of the installation is complete. <HR></BLOCKQUOTE><P>Compile and run the Controls project. You can adjust the value displayed in theprogress control by moving the slider control. The completed project is shown inFigure 16.4.</P><P><A NAME="04"></A><A HREF="04.htm"><B>Figure 16.4.</B> </A><BR><I>The finished Controls project.</I><H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2><P>In this hour, you looked at up-down, slider, and progress controls, three of thesimpler controls offered by Windows. You examined the uses for each control and theMFC classes used to interact with them and created a small dialog box-based projectthat used all three controls.<H2><FONT COLOR="#000077"><B>Q&A</B></FONT></H2><DL> <DD><B>Q The up-down control is paired with the wrong control when I run my program--what's wrong?</B><BR> <BR> <B>A</B> The up-down control must immediately follow the buddy control in the tab order. If the tab order isn't set correctly, the up-down control will latch on to whatever control precedes it. When you are working with your dialog box in Developer Studio, press Ctrl+D to see the tab order.<BR> <BR> <B>Q Is there an easy way to increase the value displayed in the progress control by a specific amount, rather than setting a new value?</B><BR> <BR> <B>A</B> Use <TT>CProgressCtrl</TT>'s <TT>OffsetPos</TT> function:</DL><BLOCKQUOTE> <PRE><FONT COLOR="#0066FF"><TT>void CMyDlg::OnBump()</TT><TT>{</TT><TT> m_progress.OffsetPos(5);</TT><TT>}</TT></FONT></PRE></BLOCKQUOTE><PRE><FONT COLOR="#0066FF"><TT></TT></FONT></PRE><H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2><P>The Workshop is designed to help you anticipate possible questions, review whatyou've learned, and begin thinking ahead to putting your knowledge into practice.The answers to the quiz are in Appendix B, "Quiz Answers."<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3><DL> <DD>1. What property is used to change the up-down control arrows to left-right instead of up-down?<BR> <BR> 2. What property is used to indicate that the up-down control is associated with a buddy control?<BR> <BR> 3. What function is used to set the limits of the up-down control?<BR> <BR> 4. How do you specify which control is the buddy control?<BR> <BR> 5. What property is used to add division lines on the slider control?<BR> <BR> 6. What function is used to set the limits of the slider control?<BR> <BR> 7. What function is used to set the limits of the progress control?</DL><H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3><DL> <DD>1. Change the Controls project so that the value of the up-down control controls the progress control.<BR> <BR> 2. Experiment with changing the up-down control's alignment and orientation from right to left, and from vertical to horizontal.<FONT COLOR="#000077"></FONT></DL><CENTER><P><HR><A HREF="../ch15/ch15.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch17/ch17.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR><BR><BR><IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"BORDER="0"></P><P>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -