?? ch01.htm
字號:
<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>If you do choose to use another editor to create your source files, make sure the files are stored as ASCII, also known as "plain text" files. The Visual C++ compiler cannot process files that have special formatting characters embedded in them, such as the files created by word- processing programs. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Using Editor Commands</B></FONT></H3><P>A large set of editing commands are available from the keyboard. Although mosteditor commands are also available from the menu or toolbar, the following commandsare frequently used from the keyboard:<UL> <LI><I>Undo</I>, which reverses the previous editor action, is performed by pressing Ctrl+Z on the keyboard. The number of undo steps that can be performed is configurable in the Options dialog box.<BR> <BR> <LI><I>Redo</I>, which is used to reverse an undo, is performed by pressing Ctrl+Y.<BR> <BR> <LI><I>LineCut</I>, which removes or "cuts" the current line and places it on the Clipboard, is performed by pressing Ctrl+L.<BR> <BR> <LI><I>Cut </I>removes any marked text from the editor and places it on the Clipboard. This command is performed by pressing Ctrl+X.<BR> <BR> <LI><I>Copy</I> copies any marked text to the Clipboard but, unlike the Cut command, doesn't remove the text from the editor. If no text is marked, the current line is copied. This command is performed by pressing Ctrl+C.<BR> <BR> <LI><I>Paste </I>copies the Clipboard contents into the editor at the insertion point. This command is performed by pressing Ctrl+V.</UL><P>This is only a small list of the available keyboard commands. To see a completelist, select Keyboard Map... from the Help menu. A list of the current keyboard commandbindings is displayed, as shown in Figure 1.4.</P><P><A NAME="04"></A><A HREF="04.htm"><B>Figure 1.4.</B></A> <I><BR>An example of keyboard command bindings in Developer Studio.</I><H2><FONT COLOR="#000077"><B>Creating Your First C++ Program</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>console-mode</I> applicationis a character-based program that runs in a DOS window.</P><P>For your first Visual C++ program, you will build a console-mode program thatdisplays a Hello World greeting. Console-mode programs are often simpler to buildthan Windows applications, and this example will take you through the steps of buildingand running a program built with Visual C++.<H3><FONT COLOR="#000077"><B>Starting Your First Program</B></FONT></H3><P>The first stage in writing your first Visual C++ program is to create a project.Follow these steps:<DL> <DD>1. Choose File|New from the main menu. The New dialog box will be displayed.<BR> <BR> 2. Select the Projects tab, and then click the Win32 Console Application icon from the list box.<BR> <BR> 3. Specify Hello as the project name in the Project name box; a default location for your project will automatically be entered in the Location box (see Figure 1.5).</DL><P><A NAME="05"></A><A HREF="05.htm"><B>Figure 1.5.</B> </A><I><BR>The New Projects dialog box for the Hello project.</I><DL> <DD>4. Click OK to create the project.</DL><H3><FONT COLOR="#000077"><B>Editing Your First C++ Source File</B></FONT></H3><P>The most important parts of any C++ program are the source files. Although thesample program provided in Listing 1.1 is very short, it contains many of the elementspresent in all C++ programs.<H4><FONT COLOR="#000077">TYPE: Listing 1.1. A simple C++ console-mode program.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>// Hello world example</TT><TT>#include <iostream></TT><TT>using namespace std;</TT><TT>int main()</TT><TT>{</TT><TT> cout << "Hello World!" << endl;</TT><TT>}</TT></FONT></PRE><P>Open a new source file document and type the program exactly as shown in Listing1.1. As discussed earlier, there are two ways to open a new source file for editing:<UL> <LI>Click the New Text File icon on the toolbar.<BR> <BR> <LI>Select File|New from the main menu, and select C++ Source File from the New dialog box under the Files tab.</UL><P>If you open a new file for editing while a project is open, you have the optionof automatically adding the file to the currently open project. To take advantageof this option, make sure the Add to Project: check box is checked, and provide aname for the file in the dialog box (in this case use <TT>Hello.cpp</TT>).<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>When using C++, remember that capitalization is important. For example, MAIN and main are two different names to the compiler. White space, such as the number of spaces before a word such as <TT>cout</TT>, is not significant to the compiler. White space is often used to help make programs more readable. <HR></P> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>If you used the toolbar's New Source File icon to create your new source file, syntax highlighting will not be provided until the file is saved and the file is given a name. This is because the Developer Studio editor uses the file extension to determine the file type, and it does not know what type of file is being edited. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Saving a Source File</B></FONT></H3><P>After you have entered the program in Listing 1.1, save the source file in yourproject's directory as <TT>Hello.cpp</TT>. To save the contents of the editor, clickthe Save icon on the toolbar. The Save icon looks like a small floppy disk. You canalso press Ctrl+S or select Save from the File menu.</P><P>When updating a previously saved source file, you don't see a dialog box, andno further action is needed on your part. The existing file is updated using thecurrent contents of the editor. If you save a new file, you see the Save As dialogbox, and you must choose a location and filename for the new source file. Save thecontents of Listing 1.1 in the <TT>C:\</TT> directory using the name <TT>CFoo.cpp</TT>.After saving the file, close <TT>CFoo.cpp</TT> by selecting Close from the File menu.</P><P>To save a file under a new name, select Save As from the File menu or press F12.Enter the new path and filename using the Save As dialog box as described previously.</P><P>If you have not yet added the source file to the project, follow these steps:<DL> <DD>1. Select Project|Add To Project|Files... from the main menu. This will display the Insert Files into Project dialog box.<BR> <BR> 2. Select the <TT>Hello.cpp</TT> source file and then click OK.</DL><BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Visual C++ requires that your C++ source files have a <TT>.CPP</TT> file extension. This helps Developer Studio properly compile your source code, as well as provide the proper syntax highlighting.<BR> Other types of files also have standard extensions. For example, C source files must use the <TT>.C</TT> extension. Other file extensions will be discussed as they are introduced. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Building the Hello Project</B></FONT></H3><P>Compile the Hello project by selecting Build|Build Hello.exe from the main menu(or press F7). If you entered Listing 1.1 correctly, the project is built with noerrors, and the last line in the status window reads as follows:</P><PRE><FONT COLOR="#0066FF"><TT>HELLO.exe - 0 error(s), 0 warning(s)</TT></FONT></PRE><BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>You can also build the Hello project by clicking the Build button on the toolbar. The toolbar was shown in Figure 1.3. <HR></BLOCKQUOTE><P>If errors or warnings are displayed in the Build status window, there is probablyan error in the source file. Check your source file again for missing semicolons,quotes, or braces.<H3><FONT COLOR="#000077"><B>Running Your First C++ Program</B></FONT></H3><P>To run the Hello program, open a DOS window and change the working directory tothe project's directory. By default, this directory is</P><PRE><FONT COLOR="#0066FF"><TT>C:\Program File\DevStudio\MyProjects\Hello</TT></FONT></PRE><P>On some machines, filenames may be truncated, so the path on your machine mightbe something like</P><PRE><FONT COLOR="#0066FF"><TT>C:\progra~1\devstudio\myprojects\hello</TT></FONT></PRE><P>You'll see a subdirectory named <TT>DEBUG</TT>. The Visual C++ IDE puts all theexecutable and intermediate files into this directory by default. Change to the <TT>DEBUG</TT>directory and execute the <TT>Hello.exe</TT> program by typing the following at theDOS prompt:</P><PRE><FONT COLOR="#0066FF"><TT>HELLO</TT></FONT></PRE><P>The program loads and then displays <TT>Hello World!</TT>. That's all there isto it.</P><P>All of the console mode or DOS programs used as examples in this book should becompiled and executed just like <TT>Hello.exe</TT>. You'll always create a project,add files to it, and then build the project. After the application is built, youthen go out to DOS and execute the program.<H2><FONT COLOR="#000077"><B>Creating a Windows Program Using AppWizard</B></FONT></H2><P>AppWizard is a tool that generates an MFC project based on options that you select.AppWizard creates all the source files required to make a skeleton project that servesas a starting point for your program. You can use AppWizard to create single-document,multiple-document, or dialog box-based applications.</P><P>AppWizard creates all the source files required to build a skeleton Windows application.It also configures a project for you and allows you to specify the project directory.Although an AppWizard project is a skeleton of a future project, it uses the MFCclass library to include the following functions:<UL> <LI>Automatic support for the common Windows dialog boxes, including Print, File Open, and File Save As<BR> <BR> <LI>Dockable toolbars<BR> <BR> <LI>A status bar<BR> <BR> <LI>Optional MAPI, ODBC, and OLE support
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -