📄 ch03.htm
字号:
<TD>
<P>Draws the slider horizontally</P>
<TR>
<TD>
<pre><font color="#008000">TBS_LEFT</font></pre>
<TD>
<P>Draws tick marks on the left side of a vertical slider</P>
<TR>
<TD>
<pre><font color="#008000">TBS_NOTICKS</font></pre>
<TD>
<P>Draws a slider with no tick marks</P>
<TR>
<TD>
<pre><font color="#008000">TBS_RIGHT</font></pre>
<TD>
<P>Draws tick marks on the right side of a vertical slider</P>
<TR>
<TD>
<pre><font color="#008000">TBS_TOP</font></pre>
<TD>
<P>Draws tick marks on the top of a horizontal slider</P>
<TR>
<TD>
<pre><font color="#008000">TBS_VERT</font></pre>
<TD>
<P>Draws a vertical slider</P></TABLE>
<P><B><I>Initializing the Trackbar</I></B></P>
<P>After the slider control is created, it must be initialized. The <font color="#008000">CSliderCtrl</font> class features many member functions that enable you to initialize and manipulate the control. Those member functions and their descriptions are
listed in Table 3.3.</P>
<P><I>Table 3.3—Member Functions of the </I>CSliderCtrl<I> Class</I></P>
<TABLE BORDER>
<TR>
<TD>
<P><B>Function</B></P>
<TD>
<P><B>Description</B></P>
<TR>
<TD>
<pre><font color="#008000">ClearSel()</font></pre>
<TD>
<P>Clears a selection from the control</P>
<TR>
<TD>
<pre><font color="#008000">ClearTics()</font></pre>
<TD>
<P>Clears tick marks from the control</P>
<TR>
<TD>
<pre><font color="#008000">Create()</font></pre>
<TD>
<P>Creates a slider control</P>
<TR>
<TD>
<pre><font color="#008000">GetChannelRect()</font></pre>
<TD>
<P>Gets the size of the control's slider</P>
<TR>
<TD>
<pre><font color="#008000">GetLineSize()</font></pre>
<TD>
<P>Gets the control's line size</P>
<TR>
<TD>
<pre><font color="#008000">GetNumTics()</font></pre>
<TD>
<P>Gets the number of tick marks</P>
<TR>
<TD>
<pre><font color="#008000">GetPageSize()</font></pre>
<TD>
<P>Gets the control's page size</P>
<TR>
<TD>
<pre><font color="#008000">GetPos()</font></pre>
<TD>
<P>Gets the control's position</P>
<TR>
<TD>
<pre><font color="#008000">GetRange()</font></pre>
<TD>
<P>Gets the control's minimum and maximum values</P>
<TR>
<TD>
<pre><font color="#008000">GetRangeMax()</font></pre>
<TD>
<P>Gets the control's maximum value</P>
<TR>
<TD>
<pre><font color="#008000">GetRangeMin()</font></pre>
<TD>
<P>Gets the control's minimum value</P>
<TR>
<TD>
<pre><font color="#008000">GetSelection()</font></pre>
<TD>
<P>Gets the current range selection</P>
<TR>
<TD>
<pre><font color="#008000">GetThumbRect()</font></pre>
<TD>
<P>Gets the size of the control's thumb</P>
<TR>
<TD>
<pre><font color="#008000">GetTic()</font></pre>
<TD>
<P>Gets the position of a tick mark</P>
<TR>
<TD>
<pre><font color="#008000">GetTicArray()</font></pre>
<TD>
<P>Gets all of the control's tick positions</P>
<TR>
<TD>
<pre><font color="#008000">GetTicPos()</font></pre>
<TD>
<P>Gets the client coordinates of a tick mark</P>
<TR>
<TD>
<pre><font color="#008000">SetLineSize()</font></pre>
<TD>
<P>Sets the amount to move the value up or down in response to the cursor up or cursor down key</P>
<TR>
<TD>
<pre><font color="#008000">SetPageSize()</font></pre>
<TD>
<P>Sets the amount to move the value up or down in response to the page up or page down key</P>
<TR>
<TD>
<pre><font color="#008000">SetPos()</font></pre>
<TD>
<P>Sets the control's position</P>
<TR>
<TD>
<pre><font color="#008000">SetRange()</font></pre>
<TD>
<P>Sets the control's minimum and maximum values</P>
<TR>
<TD>
<pre><font color="#008000">SetRangeMax()</font></pre>
<TD>
<P>Sets the control's maximum value</P>
<TR>
<TD>
<pre><font color="#008000">SetRangeMin()</font></pre>
<TD>
<P>Sets the control's minimum value</P>
<TR>
<TD>
<pre><font color="#008000">SetSelection()</font></pre>
<TD>
<P>Sets a selected sub-range in the control</P>
<TR>
<TD>
<pre><font color="#008000">SetTic()</font></pre>
<TD>
<P>Sets the position of a tick mark</P>
<TR>
<TD>
<pre><font color="#008000">SetTicFreq()</font></pre>
<TD>
<P>Sets the control's tick frequency</P>
<TR>
<TD>
<pre><font color="#008000">VerifyPos()</font></pre>
<TD>
<P>Determines whether the control's position is valid</P></TABLE>
<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 the Win95 Controls App application,
the program initializes the trackbar with calls to <font color="#008000">SetRange()</font>, <font color="#008000">SetTicFreq()</font>, <font color="#008000">SetLineSize()</font>, and <font color="#008000">SetPageSize()</font>, as you saw in Listing 3.3.
The call to <font color="#008000">SetRange()</font> 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 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><B><I> Manipulating the Slider</I></B></P>
<P>A slider is just a special scrollbar control. When the user moves the slider, the control generates <font color="#008000">WM_HSCROLL</font> messages, which Win95 Controls App captures in its view class's <font color="#008000">OnHScroll()</font> member
function, as shown in Listing 3.4.</P>
<P><I>Listing </I><I>3.4—Win95View.cpp—</I>CWin95View::OnHScroll()</P>
<pre><font color="#008000">void CWin95View::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> CSliderCtrl* slider = (CSliderCtrl*)pScrollBar;</font></pre>
<pre><font color="#008000"> int position = slider->GetPos();</font></pre>
<pre><font color="#008000"> char s[10];</font></pre>
<pre><font color="#008000"> wsprintf(s, "%d ", position);</font></pre>
<pre><font color="#008000"> CClientDC clientDC(this);</font></pre>
<pre><font color="#008000"> clientDC.TextOut(390, 22, s);</font></pre>
<pre><font color="#008000"> CView::OnHScroll(nSBCode, nPos, pScrollBar);</font></pre>
<pre><font color="#008000">}</font></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>
<ol>
<li><P> <font color="#008000">OnHScroll()</font>'s fourth parameter is a pointer to the scroll object that generated the <font color="#008000">WM_HSCROLL</font> message. </P>
<li><P> The function first casts this pointer to a <font color="#008000">CSliderCtrl</font> pointer, then it gets the current position of the trackbar's slider by calling the <font color="#008000">CSliderCtrl</font> member function <font
color="#008000">GetPos()</font>. </P>
<li><P> After the program has the slider's position, it converts the integer to a string and displays that string in the window.</P>
</ol>
<blockquote><p><img src="xref.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/xref.gif">
<P><B>See</B> “Drawing on the Screen” to learn how to make text appear on the screen. (<A HREF="index06.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index06.htm" target="text">Ch. 6</A>)</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -