?? ch08.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 8 -- Building a Complete Application: ShowString</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="../ch07/ch07.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch09/ch09.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>- 8 -</H1>
</CENTER>
<CENTER>
<H1>Building a Complete Application: ShowString</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Building an Application That Displays a String</A>
<UL>
<LI><A HREF="#Heading2">Creating an Empty Shell with AppWizard</A>
<LI><A HREF="#Heading3">Displaying a String</A>
</UL>
<LI><A HREF="#Heading4">Building the ShowString Menus</A>
<LI><A HREF="#Heading5">Building the ShowString Dialog Boxes</A>
<UL>
<LI><A HREF="#Heading6">ShowString's About Dialog Box</A>
<LI><A HREF="#Heading7">ShowString's Options Dialog Box</A>
</UL>
<LI><A HREF="#Heading8">Making the Menu Work</A>
<UL>
<LI><A HREF="#Heading9">The Dialog Box Class</A>
<LI><A HREF="#Heading10">Catching the Message</A>
</UL>
<LI><A HREF="#Heading11">Making the Dialog Box Work</A>
<LI><A HREF="#Heading12">Adding Appearance Options to the Options Dialog Box</A>
<UL>
<LI><A HREF="#Heading13">Changing the Options Dialog Box</A>
<LI><A HREF="#Heading14">Adding Member Variables to the Dialog Box Class</A>
<LI><A HREF="#Heading15">Adding Member Variables to the Document</A>
<LI><A HREF="#Heading16">Changing OnToolsOptions()</A>
<LI><A HREF="#Heading17">Changing OnDraw()</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<H2><A NAME="Heading1"></A>Building an Application That Displays a String</H2>
<P>In this chapter you pull together the concepts demonstrated in previous chapters
to create an application that really does something. You add a menu, a menu item,
a dialog box, and persistence to an application that draws output based on user settings.
In subsequent chapters this application serves as a base for more advanced work.</P>
<P>The sample application you will build is very much like the traditional "Hello,
world!" of C programming. It simply displays a text string in the main window.
The <I>document </I>(what you save in a file) contains the string and a few settings.
There is a new menu item to bring up a dialog box to change the string and the settings,
which control the string's appearance. This is a deliberately simple application
so that the concepts of adding menu items and adding dialogs are not obscured by
trying to understand the actual brains of the application. So, bring up Developer
Studio and follow along.</P>
<P>
<H3><A NAME="Heading2"></A>Creating an Empty Shell with AppWizard</H3>
<P>First, use AppWizard to create the starter application. (Chapter 1, "Building
Your First Windows Application," covers AppWizard and creating starter applications.)
Choose File, New and the Project tab. Select an MFC AppWizard (exe) application,
name the project ShowString so that your classnames will match those shown throughout
this chapter, and click OK.</P>
<P>In Step 1 of AppWizard, it doesn't matter much whether you choose SDI or MDI,
but MDI will enable you to see for yourself how little effort is required to have
multiple documents open at once. So, choose MDI. Choose U.S. English, and then click
Next.</P>
<P>The ShowString application needs no database support and no compound document
support, so click Next on Step 2 and Step 3 without changing anything. In AppWizard's
Step 4 dialog box, select a docking toolbar, initial status bar, printing and print
preview, context-sensitive help, and 3D controls, and then click Next. Choose source
file comments and shared DLL, and then click Next. The classnames and filenames are
all fine, so click Finish. Figure 8.1 shows the final confirmation dialog box. Click
OK.</P>
<P><A HREF="javascript:popUp('08uvc01.gif')"><B>FIG. 8.1</B></A><B> </B><I>AppWizard
summarizes the design choices for ShowString.</I></P>
<P>
<H3><A NAME="Heading3"></A>Displaying a String</H3>
<P>The ShowString application displays a string that will be kept in the document.
You need to add a member variable to the document class, CShowStringDoc, and add
loading and saving code to the Serialize() function. You can initialize the string
by adding code to OnNewDocument() for the document and, in order to actually display
it, override OnDraw() for the view. Documents and views are introduced in Chapter
4, "Documents and Views."</P>
<P><B>Member Variable and Serialization  </B>Add a private variable to
the document and a public function to get the value by adding these lines to ShowStringDoc.h:</P>
<P>
<PRE>private:
CString string;
public:
CString GetString() {return string;}
</PRE>
<P>The inline function gives other parts of your application a copy of the string
to use whenever necessary but makes it impossible for other parts to change the string.</P>
<P>Next, change the skeleton CShowStringDoc::Serialize() function provided by AppWizard
to look like Listing 8.1. (Expand CShowStringDoc in ClassView and double-click Serialize()
to edit the code.) Because you used the MFC CString class, the archive has operators
<< and >> already defined, so this is a simple function to write. It
fills the archive from the string when you are saving the document and fills the
string from the archive when you are loading the document from a file. Chapter 7,
"Persistence and File I/O," introduces serialization.</P>
<P>
<H4>Listing 8.1  SHOWSTRINGDOC.CPP--CShowStringDoc::Serialize()</H4>
<PRE>void CShowStringDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << string;
}
else
{
ar >> string;
}
</PRE>
<PRE>}
</PRE>
<P><B>Initializing the String  </B>Whenever a new document is created,
you want your application to initialize string to "Hello, world!". A new
document is created when the user chooses File, New. This message is caught by CShowStringApp
(the message map is shown in Listing 8.2, you can see it yourself by scrolling toward
the top of ShowString.cpp) and handled by CWinApp::OnFileNew(). (Message maps and
message handlers are discussed in Chapter 3, "Messages and Commands.")
Starter applications generated by AppWizard call OnFileNew() to create a blank document
when they run. OnFileNew() calls the document's OnNewDocument(), which actually initializes
the member variables of the document.</P>
<P>
<H4>Listing 8.2  SHOWSTRING.CPP--Message Map</H4>
<PRE>BEGIN_MESSAGE_MAP(CShowStringApp, CWinApp)
//{{AFX_MSG_MAP(CShowStringApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - The ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file-based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
</PRE>
<PRE>END_MESSAGE_MAP()
</PRE>
<P>AppWizard gives you the simple OnNewDocument() shown in Listing 8.3. To see yours
in the editor, double-click OnNewDocument() in ClassView--you may have to expand
CshowStringDoc first.</P>
<P>
<H4>Listing 8.3  SHOWSTRINGDOC.CPP--CShowStringDoc::OnNewDocument()</H4>
<PRE>BOOL CShowStringDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
</PRE>
<PRE>}
</PRE>
<P>Take away the comments and add this line in their place:</P>
<P>
<PRE>string = "Hello, world!";
</PRE>
<P>(What else could it say, after all?) Leave the call to CDocument::OnNewDocument()
because that will handle all other work involved in making a new document.</P>
<P><B>Getting the String Onscreen  </B>As you learned in Chapter 5, "Drawing
on the Screen," a view's OnDraw() function is called whenever that view needs
to be drawn, such as when your application is first started, resized, or restored
or when a window that had been covering it is taken away. AppWizard has provided
a skeleton, shown in Listing 8.4. To edit this function, expand CShowStringView in
ClassView and then double-click OnDraw().</P>
<P>
<H4>Listing 8.4  SHOWSTRINGVIEW.CPP--CShowStringView::OnDraw()</H4>
<PRE>void CShowStringView::OnDraw(CDC* pDC)
{
CShowStringDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
</PRE>
<PRE>}
</PRE>
<P>OnDraw() takes a pointer to a device context, as discussed in Chapter 5. The device
context class, CDC, has a member function called DrawText() that draws text onscreen.
It is declared like this:</P>
<P>
<PRE>int DrawText( const CString& <I>str</I>, LPRECT <I>lpRect</I>, UINT <I>nFormat</I> )
</PRE>
<BLOCKQUOTE>
<P>
<HR>
<B>See</B> "Understanding Device Contexts," <A HREF="../ch05/ch05.htm"><B>ch.
5</B></A>
<HR>
</BLOCKQUOTE>
<P>The CString to be passed to this function is going to be the string from the document
class, which can be accessed as pDoc->GetString(). The lpRect is the client rectangle
of the view, returned by GetClientRect(). Finally, nFormat is the way the string
should display; for example, DT_CENTER means that the text should be centered from
left to right within the view. DT_VCENTER means that the text should be centered
up and down, but this works only for single lines of text that are identified with
DT_SINGLELINE. Multiple format flags can be combined with |, so DT_CENTER|DT_VCENTER|DT_SINGLELINE
is the nFormat that you want. The drawing code to be added to CShowStringView::OnDraw()
looks like this:</P>
<P>
<PRE>CRect rect;
GetClientRect(&rect);
pDC->DrawText(pDoc->GetString(), &rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
</PRE>
<P>This sets up a CRect and passes its address to GetClientRect(), which sets the
CRect to the client area of the view. DrawText() draws the document's string in the
rectangle, centered vertically and horizontally.</P>
<P>At this point, the application should display the string properly. Build and execute
it, and you will see something like Figure 8.2. You have a lot of functionality--menus,
toolbars, status bar, and so on--but nothing that any other Windows application doesn't
have, yet. Starting with the next section, that changes.</P>
<P><A HREF="javascript:popUp('08uvc02.gif')"><B>FIG. 8.2</B></A><B> </B><I>ShowString
starts simply, with the usual greeting.</I></P>
<P>
<H2><A NAME="Heading4"></A>Building the ShowString Menus</H2>
<P>AppWizard creates two menus for you, shown in the ResourceView window in Figure
8.3. IDR_MAINFRAME is the menu shown when no file is open; IDR_SHOWSTTYPE is the
menu shown when a ShowString document is open. Notice that IDR_MAINFRAME has no Window
menus and that the File menu is much shorter than the one on the IDR_SHOWSTTYPE menu,
with only New, Open, Print Setup, recent files, and Exit items.</P>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -