?? ch10.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Ch 10 -- Common Controls</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ch09/ch09.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch11/ch11.htm"><IMG
SRC="../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>
<HR>
</CENTER>
<CENTER>
<H1>- 10 -</H1>
<H1>Common Controls</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">The Progress Bar Control</A>
<UL>
<LI><A HREF="#Heading2">Creating the Progress Bar</A>
<LI><A HREF="#Heading3">Initializing the Progress Bar</A>
<LI><A HREF="#Heading4">Manipulating the Progress Bar</A>
</UL>
<LI><A HREF="#Heading5">The Slider Control</A>
<UL>
<LI><A HREF="#Heading6">Creating the Trackbar</A>
<LI><A HREF="#Heading7">Initializing the Trackbar</A>
<LI><A HREF="#Heading8">Manipulating the Slider</A>
</UL>
<LI><A HREF="#Heading9">The Up-Down Control</A>
<UL>
<LI><A HREF="#Heading10">Creating the Up-Down Control</A>
</UL>
<LI><A HREF="#Heading11">The Image List Control</A>
<UL>
<LI><A HREF="#Heading12">Creating the Image List</A>
<LI><A HREF="#Heading13">Initializing the Image List</A>
</UL>
<LI><A HREF="#Heading14">The List View Control</A>
<UL>
<LI><A HREF="#Heading15">Creating the List View</A>
<LI><A HREF="#Heading16">Creating the List View's Columns</A>
<LI><A HREF="#Heading17">Creating the List View's Items</A>
<LI><A HREF="#Heading18">Manipulating the List View</A>
</UL>
<LI><A HREF="#Heading19">The Tree View Control</A>
<UL>
<LI><A HREF="#Heading20">Creating the Tree View</A>
<LI><A HREF="#Heading21">Creating the Tree View's Items</A>
<LI><A HREF="#Heading22">Manipulating the Tree View</A>
</UL>
<LI><A HREF="#Heading23">The Rich Edit Control</A>
<UL>
<LI><A HREF="#Heading24">Creating the Rich Edit Control</A>
<LI><A HREF="#Heading25">Initializing the Rich Edit Control</A>
<LI><A HREF="#Heading26">Manipulating the Rich Edit Control</A>
</UL>
<LI><A HREF="#Heading27">IP Address Control</A>
<LI><A HREF="#Heading28">The Date Picker Control</A>
<LI><A HREF="#Heading29">Month Calendar Control</A>
<LI><A HREF="#Heading30">Scrolling the View</A>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<P>As a Windows user, you're accustomed to seeing controls such as buttons, list
boxes, menus, and edit boxes. As Windows developed, however, Microsoft noticed that
developers routinely create other types of controls in their programs: toolbars,
status bars, progress bars, tree views, and others. To make life easier for Windows
programmers, Microsoft included these popular controls as part of the operating environment
of Windows 95 (as well as later versions of Windows NT and then Windows 98). Now
Windows programmers no longer need to create from scratch their own versions of these
controls. This chapter introduces you to many of the 32-bit Windows common controls.
The toolbar and status bar controls are covered in Chapter 9, "Status Bars and
Toolbars," and property sheets are covered in Chapter 12, "Property Pages
and Sheets."</P>
<P>This chapter's sample program is called Common. It demonstrates nine of the Windows
95 common controls: the progress bar, slider, up-down, list view, tree view, rich
edit, IP address, date picker, and month calendar controls, all of which are shown
in Figure 10.1. In the following sections, you learn the basics of creating and using
these controls in your own applications.</P>
<P><A HREF="javascript:popUp('10uvc01.gif')"><B>FIG. 10.1</B></A><B> </B><I>The Common
sample application demonstrates nine Windows 95 common controls.</I></P>
<P>To make Common, create a new project with AppWizard and name it <B>Common</B>.
Choose a single-document interface (SDI) application in Step 1 and accept all the
defaults until Step 6. Drop down the Base Class box and choose CScrollView from the
list. This ensures that users can see all the controls in the view, even if they
have to scroll to do so. Click Finish and then OK to complete the process.</P>
<P>The controls themselves are declared as data members of the view class. Double-click
CCommonView in ClassView to edit the header file and add the lines in Listing 10.1
in the Attributes section. As you can see, the progress bar is an object of the CProgressCtrl
class. It's discussed in the next section, and the other controls are discussed in
later sections of this chapter.</P>
<P>
<H4>Listing 10.1  CommonView.h--Declaring the Controls</H4>
<PRE>protected:
//Progress Bar
CProgressCtrl m_progressBar;
//Trackbar or Slider
CSliderCtrl m_trackbar;
BOOL m_timer;
// Up-Down or Spinner
CSpinButtonCtrl m_upDown;
CEdit m_buddyEdit;
// List View
CListCtrl m_listView;
CImageList m_smallImageList;
CImageList m_largeImageList;
CButton m_smallButton;
CButton m_largeButton;
CButton m_listButton;
CButton m_reportButton;
// Tree View
CTreeCtrl m_treeView;
CImageList m_treeImageList;
// Rich Edit
CRichEditCtrl m_richEdit;
CButton m_boldButton;
CButton m_leftButton;
CButton m_centerButton;
CButton m_rightButton;
// IP Address
CIPAddressCtrl m_ipaddress;
// Date Picker
CDateTimeCtrl m_date;
// Month Calendar
</PRE>
<PRE> CMonthCalCtrl m_month;
</PRE>
<P>Expand the CCommonView class. Double-click CCommonView::OnDraw() in ClassView
and replace the TODO comment with these lines:</P>
<P>
<PRE>pDC->TextOut(20, 22, "Progress Bar Control");
pDC->TextOut(270, 22, "Trackbar Control:");
pDC->TextOut(20, 102, "Up-Down Control");
pDC->TextOut(160, 102, "List View Control");
pDC->TextOut(20, 240, "Tree View Control");
pDC->TextOut(180, 240, "Rich Edit Control");
pDC->TextOut(470, 22, "IP Address Control");
pDC->TextOut(470, 102, "Date Picker Control");
pDC->TextOut(470, 240, "Month Calendar Control");
</PRE>
<P>These label the controls that you will add to CCommonView in this chapter.</P>
<P>
<H2><A NAME="Heading1"></A>The Progress Bar Control</H2>
<P>The common control that's probably easiest to use is the progress bar, which is
nothing more than a rectangle that slowly fills in with colored blocks. The more
colored blocks that are filled in, the closer the task is to being complete. When
the progress bar is completely filled in, the task associated with the progress bar
is also complete. You might use a progress bar to show the status of a sorting operation
or to give the user visual feedback about a large file that's being loaded.</P>
<P>
<H3><A NAME="Heading2"></A>Creating the Progress Bar</H3>
<P>Before you can use a progress bar, you must create it. Often in an MFC program,
the controls are created as part of a dialog box. However, Common displays its controls
in the application's main window, the view of this single-document interface (SDI)
application. Documents and views are introduced in Chapter 4, "Documents and
Views." All the controls are created in the view class OnCreate() function,
which responds to the WM_CREATE Windows message. To set up this function, right-click
CCommonView in ClassView and choose Add Windows Message Handler. Choose WM_CREATE
from the list on the left and click Add and Edit. Add this line in place of the TODO
comment:</P>
<P>
<PRE>CreateProgressBar();
</PRE>
<P>Right-click CCommonView in ClassView again and this time choose Add Member Function.
Enter <B>void</B> for the Function Type and enter <B>CreateProgressBar()</B> for
the Function Declaration. Leave the access as Public. Click OK to add the function;
then add the code in Listing 10.2.</P>
<P>
<H4>Listing 10.2  CommonView.cpp--CCommonView::CreateProgressBar()</H4>
<PRE>void CCommonView::CreateProgressBar()
{
m_progressBar.Create(WS_CHILD | WS_VISIBLE | WS_BORDER,
CRect(20, 40, 250, 80), this, IDC_PROGRESSBAR);
m_progressBar.SetRange(1, 100);
m_progressBar.SetStep(10);
m_progressBar.SetPos(50);
m_timer = FALSE;
</PRE>
<PRE>}
</PRE>
<P>CreateProgressBar() first creates the progress bar control by calling the control's
Create() 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 resource ID, IDC_PROGRESSBAR, is added by hand. To add
resource symbols to your own applications, choose View, Resource Symbols and click
the New button. Type in a resource ID Name, such as IDC_PROGRESSBAR, and accept the
default Value Visual Studio provides.</P>
<P>The style constants are the same constants that you use for creating any type
of window (a control is nothing more than a special kind of window, after all). In
this case, you need at least the following:</P>
<P>
<UL>
<LI>WS_CHILD  Indicates that the control is a child window
<P>
<LI>WS_VISIBLE  Ensures that the user can see the control
</UL>
<P>The WS_BORDER is a nice addition because it adds a dark border around the control,
setting it off from the rest of the window.</P>
<P>
<H3><A NAME="Heading3"></A>Initializing the Progress Bar</H3>
<P>To initialize the control, CCommonView::CreateProgressBar() calls SetRange(),
SetStep(), and SetPos(). Because the range and the step rate are related, a control
with a range of 1-10 and a step rate of 1 works almost identically to a control with
a range of 1-100 and a step rate of 10.</P>
<P>When this sample application starts, the progress bar is already half filled with
colored blocks. (This is purely for aesthetic reasons. Usually a progress bar begins
its life empty.) It's half full because CreateProgressBar() calls SetPos() with the
value of 50, which is the midpoint of the control's range.</P>
<P>
<H3><A NAME="Heading4"></A>Manipulating the Progress Bar</H3>
<P>Normally you update a progress bar as a long task moves toward completion. In
this sample, you will fake it by using a timer. When the user clicks in the background
of the view, start a timer that generates WM_TIMER messages periodically. Catch these
messages and advance the progress bar. Here's what to do:</P>
<P>
<DL>
<DT></DT>
<DD><B>1. </B>Open ClassWizard. Make sure that CCommonView is selected in the upper-right
drop- down box.
<P>
<DT></DT>
<DD><B>2. </B>Scroll most of the way through the list box on the right until you
find WM_LBUTTONDOWN, the message generated when the user clicks on the view. Select
it.
<P>
<DT></DT>
<DD><B>3. </B>Click Add Function; then click Edit Code.
<P>
<DT></DT>
<DD><B>4. </B>Edit OnLButtonDown() so that it looks like this:
<P>
</DL>
<PRE>void CCommonView::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_timer)
{
KillTimer(1);
m_timer = FALSE;
}
else
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -