⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch10.htm

📁 Learning language of Visual C++6
💻 HTM
📖 第 1 页 / 共 5 页
字号:
   {
      SetTimer(1, 500, NULL);
      m_timer = TRUE;
   }
   CView::OnLButtonDown(nFlags, point);
}
</PRE>
<P>This code enables users to turn the timer on or off with a click. The parameter
of 500 in the SetTimer call is the number of milliseconds between WM_TIMER messages:
This timer will send a message twice a second.</P>
<P>

<DL>
	<DT></DT>
	<DD><B>5. </B>In case a timer is still going when the view closes, you should override
	OnDestroy() to kill the timer. Right-click CCommonView in ClassView yet again and
	choose Add Windows Message Handler. Select WM_DESTROY and click Add and Edit. Replace
	the TODO comment with this line:
	<P>
</DL>

<PRE>KillTimer(1);
</PRE>

<DL>
	<DT></DT>
	<DD><B>6. </B>Now, catch the timer messages. Open ClassWizard and, as before, scroll
	through the list of messages in the far right list box. WM_TIMER is the second-to-last
	message in the alphabetic list, so drag the elevator all the way to the bottom and
	select WM_TIMER. Click Add Function and then click Edit Code. Replace the TODO comment
	with this line:
	<P>
</DL>

<PRE>m_progressBar.StepIt();
</PRE>
<P>The StepIt() function increments the progress bar control's value by the step
rate, causing new blocks to be displayed in the control as the control's value setting
counts upward. When the control reaches its maximum, it automatically starts over.</P>


<BLOCKQUOTE>
	<P>
<HR>
<strong>NOTE:</strong>otice that no CProgressCtrl member functions control the size or number
	of blocks that will fit into the control. These attributes are indirectly controlled
	by the size of the control.&#160;
<HR>


</BLOCKQUOTE>

<P>Build Common and execute it to see the progress bar in action. Be sure to try
stopping the timer as well as starting it.</P>
<P>
<H2><A NAME="Heading5"></A>The Slider Control</H2>
<P>Many times in a program you might need the user to enter a value within a specific
range. For this sort of task, you use MFC's CSliderCtrl class to create a slider
(also called <I>trackbar</I>) control. For example, suppose you need the user to
enter a percentage. In this case, you want the user to enter values only in the range
of 0-100. Other values would be invalid and could cause problems in your program.</P>
<P>By using the slider control, you can force the user to enter a value in the specified
range. Although the user can accidentally enter a wrong value (a value that doesn't
accomplish what the user wants to do), there is no way to enter an invalid value
(one that brings your program crashing down like a stone wall in an earthquake).</P>
<P>For a percentage, you create a slider control with a minimum value of 0 and a
maximum value of 100. Moreover, to make the control easier to position, you might
want to place tick marks at each setting that's a multiple of 10, providing 11 tick
marks in all (including the one at 0). Common creates exactly this type of slider.</P>
<P>To use a slider, the user clicks the slider's slot. This moves the slider forward
or backward, and often the selected value appears near the control. When a slider
has the focus, the user can also control it with the Up and Down arrow keys and the
Page Up and Page Down keys.</P>
<P>
<H3><A NAME="Heading6"></A>Creating the Trackbar</H3>
<P>You are going to need a resource symbol for the trackbar control, so just as you
did for the progress bar, choose View, Resource Symbols and click New. Enter IDC_TRACKBAR
for the resource ID Name and accept the suggested Value. In CCommonView::OnCreate(),
add a call to CreateTrackbar(). Then add the new member function as you added CreateProgressBar()
and type in the code in Listing 10.3.</P>
<P>
<H4>Listing 10.3&#160;&#160;CommonView.cpp--CCommonView::CreateTrackBar()</H4>
<PRE>void CCommonView::CreateTrackbar()
{
    m_trackbar.Create(WS_CHILD | WS_VISIBLE | WS_BORDER |
        TBS_AUTOTICKS | TBS_BOTH | TBS_HORZ,
        CRect(270, 40, 450, 80), this, IDC_TRACKBAR);
    m_trackbar.SetRange(0, 100, TRUE);
    m_trackbar.SetTicFreq(10);
    m_trackbar.SetLineSize(1);
    m_trackbar.SetPageSize(10);
</PRE>
<PRE>}
</PRE>
<P>As with the progress bar, the first step is to create the slider control by calling
its Create() member function. This function's four arguments are the control's style
flags, the control's size (as a CRect object), a pointer to the control's parent
window, and the control's ID. The style constants include the same constants that
you would use for creating any type of window, with the addition of special styles
used with sliders. Table 10.1 lists these special styles.</P>
<P>
<H4>Table 10.1&#160;&#160;Slider Styles</H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"><B>Style</B></TD>
		<TD ALIGN="LEFT"><B>Description</B></TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">TBS_AUTOTICKS		</TD>
		<TD ALIGN="LEFT">Enables the slider to automatically draw its tick marks		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">TBS_BOTH		</TD>
		<TD ALIGN="LEFT">Draws tick marks on both sides of the slider		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">TBS_BOTTOM		</TD>
		<TD ALIGN="LEFT">Draws tick marks on the bottom of a horizontal slider		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">TBS_ENABLESELRANGE		</TD>
		<TD ALIGN="LEFT">Enables a slider to display a subrange of values		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">TBS_HORZ		</TD>
		<TD ALIGN="LEFT">Draws the slider horizontally		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">TBS_LEFT		</TD>
		<TD ALIGN="LEFT">Draws tick marks on the left side of a vertical slider		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">TBS_NOTICKS		</TD>
		<TD ALIGN="LEFT">Draws a slider with no tick marks		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">TBS_RIGHT		</TD>
		<TD ALIGN="LEFT">Draws tick marks on the right side of a vertical slider		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">TBS_TOP		</TD>
		<TD ALIGN="LEFT">Draws tick marks on the top of a horizontal slider		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">TBS_VERT		</TD>
		<TD ALIGN="LEFT">Draws a vertical slider		</TD>
	</TR>
</TABLE>

<H3><A NAME="Heading7"></A>Initializing the Trackbar</H3>
<P>Usually, when you create a slider control, you want to set the control's range
and tick frequency. If the user is going to use the control from the keyboard, you
also need to set the control's line and page size. In Common, the program initializes
the trackbar with calls to SetRange(), SetTicFreq(), SetLineSize(), and SetPageSize(),
as you saw in Listing 10.3. The call to SetRange() sets the trackbar's minimum and
maximum values to 0 and 100. The arguments are the minimum value, the maximum value,
and a Boolean value indicating whether the slider should redraw itself after setting
the range. Notice that the tick frequency and page size are then set to be the same.
This isn't absolutely required, but it's a very good idea. Most people assume that
the tick marks indicate the size of a page, and you will confuse your users if the
tick marks are more or less than a page apart.</P>
<P>A number of other functions can change the size of your slider, the size of the
thumb, the current selection, and more. You can find all the details in the online
documentation.</P>
<P>
<H3><A NAME="Heading8"></A>Manipulating the Slider</H3>
<P>A slider is really just a special scrollbar control. When the user moves the slider,
the control generates WM_HSCROLL messages, which you will arrange to catch. Open
ClassWizard, select the Message Maps tab, make sure CCommonView is selected in the
upper-right box, and find WM_HSCROLL in the list on the right. Select it, click Add
Function, and then click Edit Code. Type in the code in Listing 10.4.</P>
<P>
<H4>Listing 10.4&#160;&#160;CommonView.cpp--CCommonView::OnHScroll()</H4>
<PRE>void CCommonView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    CSliderCtrl* slider = (CSliderCtrl*)pScrollBar;
    int position = slider-&gt;GetPos();
    char s[10];
    wsprintf(s, &quot;%d   &quot;, position);
    CClientDC clientDC(this);
    clientDC.TextOut(390, 22, s);
    CScrollView::OnHScroll(nSBCode, nPos, pScrollBar);
</PRE>
<PRE>}
</PRE>
<P>Looking at this code, you see that the control itself doesn't display the current
position as a number nearby; it's the OnHScroll() function that displays the number.
Here's how it works:</P>
<P>

<DL>
	<DT></DT>
	<DD><B>1. </B>OnHScroll()'s fourth parameter is a pointer to the scroll object that
	generated the WM_HSCROLL message.
	<P>
	<DT></DT>
	<DD><B>2. </B>The function first casts this pointer to a CSliderCtrl pointer; then
	it gets the current position of the trackbar's slider by calling the CSliderCtrl
	member function GetPos().
	<P>
	<DT></DT>
	<DD><B>3. </B>After the program has the slider's position, it converts the integer
	to a string and displays that string in the window with TextOut().
	<P>
</DL>

<P>To learn how to make text appear onscreen, refer to Chapter 5, &quot;Drawing on
the Screen.&quot; Before moving on to the next control, build Common and test it.
Click around on the slider and watch the number change.</P>


<BLOCKQUOTE>
	<P>
<HR>
<strong>TIP:</strong> If you have Windows set to Large Fonts (perhaps because you have a
	high screen resolution), the current slider value might not be displayed in quite
	the right place because the string &quot;Trackbar Control&quot; takes up more space
	on the screen with large fonts. If this happens, simply change the TextOut call to
	write the current slider value a little farther to the right.
<HR>


</BLOCKQUOTE>

<H2><A NAME="Heading9"></A>The Up-Down Control</H2>
<P>The trackbar control isn't the only way you can get a value in a predetermined
range from the user. If you don't need the trackbar for visual feedback, you can
use an up-down control, which is little more than a couple of arrows that the user
clicks to increase or decrease the control's setting. Typically, an edit control
next to the up-down control, called a <I>buddy edit</I> control or just a <I>buddy</I>
control, displays the value to the user.</P>
<P>In the Common application, you can change the setting of the up-down control by
clicking either of its arrows. When you do, the value in the attached edit box changes,
indicating the up-down control's current setting. After the control has the focus,
you can also change its value by pressing your keyboard's Up and Down arrow keys.</P>
<P>
<H3><A NAME="Heading10"></A>Creating the Up-Down Control</H3>
<P>Add another call to CCommonView::OnCreate(), this time calling it <B>CreateUpDownCtrl()</B>.
Add the member function and the code in Listing 10.5. Also add resource symbols for
IDC_BUDDYEDIT and IDC_UPDOWN.</P>
<P>
<H4>Listing 10.5&#160;&#160;CommonView.cpp--CCommonView::CreateUpDownCtrl()</H4>
<PRE>void CCommonView::CreateUpDownCtrl()
{
    m_buddyEdit.Create(WS_CHILD | WS_VISIBLE | WS_BORDER,
        CRect(50, 120, 110, 160), this, IDC_BUDDYEDIT);
    m_upDown.Create(WS_CHILD | WS_VISIBLE | WS_BORDER |
        UDS_ALIGNRIGHT | UDS_SETBUDDYINT | UDS_ARROWKEYS,
        CRect(0, 0, 0, 0), this, IDC_UPDOWN);
    m_upDown.SetBuddy(&amp;m_buddyEdit);
    m_upDown.SetRange(1, 100);
    m_upDown.SetPos(50);
</PRE>
<PRE>}
</PRE>
<P>The program creates the up-down control by first creating the associated buddy
control to which the up-down control communicates its current value. In most cases,
including this one, the buddy control is an edit box, created by calling the CEdit
class's Create() member function. This function's four arguments are the control's
style flags, the control's size, a pointer to the control's parent window, and the
control's ID. If you recall the control declarations, m_buddyEdit is an object of
the CEdit class.</P>
<P>Now that the program has created the buddy control, it can create the up-down
control in much the same way, by calling the object's Create() member function. As
you can probably guess by now, this function's four arguments are the control's style
flags, the control's size, a pointer to the control's parent window, and the control's
ID. As with most controls, the style constants include the same constants that you
use for creating any type of window. The CSpinButtonCtrl class, of which m_upDown
is an object, however, defines special styles to be used with up-down controls. Table
10.2 lists these special styles.</P>
<P>
<H4>Table 10.2&#160;&#160;Up-Down Control Styles</H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"><B>Styles</B></TD>
		<TD ALIGN="LEFT"><B>Description</B></TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">UDS_ALIGNLEFT		</TD>
		<TD ALIGN="LEFT">Places the up-down control on the left edge of the buddy control		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">UDS_ALIGNRIGHT		</TD>
		<TD ALIGN="LEFT">Places the up-down control on the right edge of the buddy control		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">UDS_ARROWKEYS		</TD>
		<TD ALIGN="LEFT">Enables the user to change the control's values by using the keyboard's Up and Down
			arrow keys		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -