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

📄 ch03.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>



<H3><A ID="I6" NAME="I6"><B> The Up-Down Control</B></A></H3>

<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 buddy edit control or justa buddy control, displays the value to the user. </P>

<P>In the Win95 Controls App 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><B><I> Creating the Up-Down Control</I></B></P>

<P>In the Win95 Controls App application, the up-down control is created in the <font color="#008000">CreateUpDownCtrl()</font> local member function, which the program calls from the view class's <font color="#008000">OnCreate()</font> function. In <font 
color="#008000">CreateUpDownCtrl()</font>, as shown in Listing 3.5, the program creates the up-down control by first creating the associated <I>buddy</I> control to which the up-down control communicates its current value.</P>

<P><I>Listing </I><I>3.5&#151;Win95View.cpp&#151;</I>CWin95View::CreateUpDownCtrl()</P>

<pre><font color="#008000">void CWin95View::CreateUpDownCtrl()</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    m_buddyEdit.Create(WS_CHILD | WS_VISIBLE | WS_BORDER,</font></pre>

<pre><font color="#008000">        CRect(50, 120, 110, 160), this, IDC_BUDDYEDIT);</font></pre>

<pre><font color="#008000">    m_upDown.Create(WS_CHILD | WS_VISIBLE | WS_BORDER |</font></pre>

<pre><font color="#008000">        UDS_ALIGNRIGHT | UDS_SETBUDDYINT | UDS_ARROWKEYS,</font></pre>

<pre><font color="#008000">        CRect(0, 0, 0, 0), this, IDC_UPDOWN);</font></pre>

<pre><font color="#008000">    m_upDown.SetBuddy(&amp;m_buddyEdit);</font></pre>

<pre><font color="#008000">    m_upDown.SetRange(1, 100);</font></pre>

<pre><font color="#008000">    m_upDown.SetPos(50);</font></pre>

<pre><font color="#008000">}</font></pre>

<P>In most cases, including this one, the buddy control is an edit box, created by calling the <font color="#008000">CEdit</font> class's <font color="#008000">Create()</font> 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, <font color="#008000">m_buddyEdit</font> is an object of the <font color="#008000">CEdit</font> 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 <font color="#008000">Create()</font> 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 <font 
color="#008000">CSpinButtonCtrl</font> class, of which <font color="#008000">m_upDown</font> is an object, however, defines special styles to be used with up-down controls. Table 3.4 lists these special styles.</P>

<P><I>Table 3.4&#151;Up-Down Control Styles</I></P>

<TABLE BORDER>

<TR>

<TD>

<P><B>Styles</B></P>

<TD>

<P><B>Description</B></P>

<TR>

<TD>

<pre><font color="#008000">UDS_ALIGNLEFT</font></pre>

<TD>

<P>Places the up-down control on the left edge of the buddy control</P>

<TR>

<TD>

<pre><font color="#008000">UDS_ALIGNRIGHT</font></pre>

<TD>

<P>Places the up-down control on the right edge of the buddy control</P>

<TR>

<TD>

<pre><font color="#008000">UDS_ARROWKEYS</font></pre>

<TD>

<P>Enables the user to change the control's values using the keyboard's Up and Down arrow keys</P>

<TR>

<TD>

<pre><font color="#008000">UDS_AUTOBUDDY</font></pre>

<TD>

<P>Makes the previous window the buddy control</P>

<TR>

<TD>

<pre><font color="#008000">UDS_HORZ</font></pre>

<TD>

<P>Creates a horizontal up-down control</P>

<TR>

<TD>

<pre><font color="#008000">UDS_NOTHOUSANDS</font></pre>

<TD>

<P>Eliminates separators between each set of three digits</P>

<TR>

<TD>

<pre><font color="#008000">UDS_SETBUDDYINT</font></pre>

<TD>

<P>Displays the control's value in the buddy control</P>

<TR>

<TD>

<pre><font color="#008000">UDS_WRAP</font></pre>

<TD>

<P>Causes the control's value to wrap around to its minimum when the maximum is reached, and vice versa</P></TABLE>

<P>After the up-down control is created, it must be initialized. The <font color="#008000">CSpinButtonCtrl</font> class features member functions that enable you to initialize and manipulate the control, as listed in Table 3.5.</P>

<P><I>Table 3.5&#151;</I>CSpinButtonCtrl<I> Member Functions</I></P>

<TABLE BORDER>

<TR>

<TD>

<P><B>Function</B></P>

<TD>

<P><B>Description</B></P>

<TR>

<TD>

<pre><font color="#008000">Create()</font></pre>

<TD>

<P>Creates the up-down control</P>

<TR>

<TD>

<pre><font color="#008000">GetAccel()</font></pre>

<TD>

<P>Gets the control's speed</P>

<TR>

<TD>

<pre><font color="#008000">GetBase()</font></pre>

<TD>

<P>Gets the control's numerical base</P>

<TR>

<TD>

<pre><font color="#008000">GetBuddy()</font></pre>

<TD>

<P>Gets a pointer to the control's buddy control</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">SetAccel()</font></pre>

<TD>

<P>Sets the control's speed</P>

<TR>

<TD>

<pre><font color="#008000">SetBase()</font></pre>

<TD>

<P>Sets the control's numerical base (10 for decimal, 16 for hex)</P>

<TR>

<TD>

<pre><font color="#008000">SetBuddy()</font></pre>

<TD>

<P>Sets the control's buddy control</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></TABLE>

<P>This chapter&#146;s sample application establishes the up-down control with calls to <font color="#008000">SetBuddy()</font>, <font color="#008000">SetRange()</font>, and<font color="#008000"> SetPos()</font>. Thanks to the <font 
color="#008000">UDS_SETBUDDYINT</font> flag passed to <font color="#008000">Create()</font> and the call to the control's <font color="#008000">SetBuddy()</font> member function, Win95 Controls App does not need to do anything else to have the control's 
value appear on the screen. The control handles its buddy automatically.</P>

<H3><A ID="I7" NAME="I7"><B> The Image List Control</B></A></H3>

<P>Often in programs you need to use images that are related in some way. For example, your application might have a toolbar with many command buttons, each of which uses a bitmap for its icon. In a case like this, it would be great to have some sort of 
program object that could not only hold the bitmaps, but also organize them so that they can be accessed easily. That's exactly what an image list control does for you&#151;it stores a list of related images. You can use the images any way that you see fit 
in your program. Several Windows 95 controls rely on image lists. These controls are:</P>

<ul>

<li> List view controls</P>

<li> Tree view controls</P>

<li> Property pages</P>

<li> Toolbars</P>

</ul>

<P>You will undoubtedly come up with many other uses for image lists. You might, for example, have an animation sequence that you'd like to display in a window. An image list is the perfect storage place for the frames that make up the animation, because 
you can easily access any frame just by using an index.</P>

<P>If the word <I>index</I> makes you think of arrays, you're beginning to understand how an image list stores images. An image list is very similar to an array that holds pictures rather than integers or floating-point numbers. Just as with an array, you 
initialize each &#147;element&#148; of an image list and thereafter can access any part of the &#147;array&#148; by using an index.</P>

<P>You won't, however, ever see an image list control in your running application in the same way that you can see a status bar or a progress bar control. This is because (again, similar to an array) an image list is only a storage structure for pictures. 
You can display the images stored in an image list, but you can't display the image list itself. Figure 3.2 shows how an image list is organized.</P>

<A HREF="Dfigs02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch03/Dfigs02.gif"><b>Fig. 3.2</b></A>

<P><I>An image list is much like an array of pictures.</I></P>

<P><B><I>Creating the Image List</I></B></P>

<P>In the Win95 Controls App application, image lists are used with the list view and tree view controls, so the image lists for the controls are created in the <font color="#008000">CreateListView()</font> and <font 
color="#008000">CreateTreeView()</font> local member functions, which the program calls from <font color="#008000">CWin95View::OnCreate()</font>. You&#146;ll see those functions shortly, but because they are quite long, this section presents the two 
function calls that are relevant to the image list. It is created with a call to its <font color="#008000">Create()</font> member function, like this:</P>

<pre><font color="#008000">m_smallImageList.Create(16, 16, FALSE, 1, 0);</font></pre>

<P>The <font color="#008000">Create()</font> function's five arguments are:</P>

<ul>

<li> The width of the pictures in the control</P>

<li> The height of the pictures</P>

<li> A Boolean value indicating whether or not the images contain a mask</P>

<li> The number of images initially in the list</P>

<li> The number of images by which the list can dynamically grow</P>

</ul>

<P>This last value is 0 to indicate that the list is not allowed to grow during runtime. The <font color="#008000">Create()</font> function is overloaded in the <font color="#008000">CImageList</font> class so that you can create image lists in various 
ways. You can find the other versions of <font color="#008000">Create()</font> in your Visual C++ online documentation.</P>

<P><B><I> Initializing the Image List</I></B></P>

<P>After you create an image list, you will want to add images to it. After all, an empty image list isn't of much use. The easiest way to add the images is to include the images as part of your application's resource file and load them from there. For 
example, the following code shows how Win95 Controls App loads an icon into one of its image lists:</P>

<pre><font color="#008000">HICON hIcon = ::LoadIcon (AfxGetResourceHandle(),</font></pre>

<pre><font color="#008000">    MAKEINTRESOURCE(IDI_ICON1));</font></pre>

<pre><font color="#008000">m_smallImageList.Add(hIcon);</font></pre>

<P>Here, the program first gets a handle to the icon. Then, it adds the icon to the image list by calling the image list's <font color="#008000">Add()</font> member function. You might create these icons by choosing <U>I</U>nsert, <U>R</U>esource, 
double-clicking Icon, and then editing the new blank icon in the Resource Editor.</P>

<P>Table 3.6 lists other member functions you can use to manipulate an object of the <font color="#008000">CImageList</font> class. As you can see, you have a lot of control over an image list if you really want to dig in.</P>

<P><I>Table 3.6&#151;Member Functions of the </I>CImageList<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">Add()</font></pre>

<TD>

<P>Adds an image to the image list</P>

<TR>

<TD>

<pre><font color="#008000">Attach()</font></pre>

<TD>

<P>Attaches an existing image list to an object of the <font color="#008000">CImageList</font> class</P>

<TR>

<TD>

⌨️ 快捷键说明

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