📄 ch28.htm
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Chapter 28</TITLE></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><B>Chapter 28</B></H2>
<H2><B>Future Explorations</B></H2>
<hr>
<P>There are a number of topics that have not been covered elsewhere in this book, but that are well known to experienced Visual C++ programmers. They are best explored once you have experience with Developer Studio, MFC, and C++ programming. This chapter
has just enough to show you how interesting these topics are, and to encourage you to explore them yourself in the months and years to come.</P>
<ul>
<P><B> </B><B><li></B><B> </B><B>Creating </B><B>c</B><B>onsole </B><B>a</B><B>pplications</B></P>
<P> A console application is not a Window program. It runs a character-based, non-GUI interface.</P>
<P><B> </B><B><li></B><B> </B><B>32-bit Dlls</B></P>
<P> Follow the example program to see how easy writing and using 32-bit DLLs can be.</P>
<P><B> </B><B><li></B><B> </B><B>Sending </B><B>m</B><B>essages and </B><B>c</B><B>ommands</B></P>
<P> An integral part of an event driven operating system like Windows is its Messaging capability. This section shows how to extend that capability with messages of your own devising.</P>
<P><B> </B><B><li></B><B> </B><B>International </B><B>i</B><B>ssues and the Unicode </B><B>s</B><B>tandard</B></P>
<P> Unicode is a 2 byte character set standard enabling programs to work with international languages whose alphabet requires more than 8-bits of storage. MFC makes support for Unicode easy, and this section shows how.</P>
</ul>
<P><B>Creating Console Applications</B></P>
<P>A console application looks very much like a DOS application, though it runs in a resizable window. It has a strictly character-based interface with cursor keys rather than mouse movement. You use the Console API and character based I/O functions like
<font color="#008000">printf()</font> and <font color="#008000">scanf()</font> to interact with the user.</P>
<P><B>Creating a Console Executable</B></P>
<P>A console application is still executed from the DOS command line or by choosing Start, <U>R</U>un and typing the full name of the application. Console applications are probably still among the easiest programs to create and this version of the
compiler supports them directly.</P>
<P>Let's walk together through the few steps necessary to create a basic console application and then we'll explore some beneficial uses of creating these kinds of applications. The first console application we'll create is a spin on the classic 'Hello,
World!' that Kernighan and Ritchie (the creators of C++'s ancestor C) created in the 1970s.</P>
<P>Open the Microsoft Developer Studio and follow these steps to create a console application:</P>
<ol>
<li><P> In the Microsoft Developer Studio, select <U>F</U>ile, <U>N</U>ew.</P>
<li><P> In the New dialog box, click the Projects tab to bring up the now familiar New Project dialog box. (If it isn't familiar, go back to <A HREF="index01.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index01.htm" target="text">Chapter 1</A>, "Building Your First Application."</P>
<li><P> <U>N</U>ame the project HelloWorld, set an appropriate folder for the project, and choose <font color="#008000">Win32 Console </font><font color="#008000">Application</font> from the list on the left. </P>
<li><P> Click OK.</P>
</ol>
<P>The project is created immediately: no wizard dialog boxes appear and there e are no further questions to answer. Now you need to create source and header files and add them to the project. This sample will all fit in one file. Follow these steps:</P>
<ol>
<li><P> Select <U>F</U>ile, <U>N</U>ew from the file menu and click the File tab</P>
<li><P> Leave the Add To Projectbox selected; the new file will be added to the project.</P>
<li><P> Choose C++ Source File from the box on the left.</P>
<li><P> Enter HelloWorld as the filename—the extension .cpp will be added automatically. </P>
<li><P> The New dialog box should resemble Figure 28.1. Click OK.</P>
</ol>
<A HREF="CCfigs01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch28/CCfigs01.gif"><b>Fig. 28.1</b></A>
<P><I>Create a C++ source file for your console application.</I></P>
<P>A blank text file is created and named for you, and added to the project, all in one step. This is a big improvement from previous versions of Visual C++, where you created a file with a name like Text1, renamed it to HelloWorld.cpp (or whatever name
you wished) and then added it to the project.</P>
<P>Add the code in Listing 28.1 to the new file.</P>
<P><I>Listing 28.1</I><I>—</I><I>HelloWorld.Cpp</I></P>
<pre><font color="#008000">#include <iostream.h></font></pre>
<pre><font color="#008000">int main()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> cout << "Hello from the console!"<< endl;</font></pre>
<pre><font color="#008000"> return 0;</font></pre>
<pre><font color="#008000">}</font></pre>
<P>Choose <U>B</U>uild, <U>G</U>o to compile, link, and execute the program. You should see a DOS box appear that resembles Figure 28.2. The line <font color="#008000">P</font><font color="#008000">ress any key to continue</font> is generated by the
system and gives you a chance to read your output before the DOS box disappears.</P>
<A HREF="CCfigs02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch28/CCfigs02.gif"><b>Fig. 28.2</b></A>
<P><I>Your application appears to be a DOS program.</I></P>
<P><B>Writing an</B><B> Object</B><B>-</B><B>Oriented Console Application</B></P>
<P>The HelloWorld application is clearly C++ and would not compile in a C compiler, which does not support stream based I/O with <font color="#008000">cout</font>, but it's not object oriented—there's not an object in it. Replace the code in
HelloWorld.cpp with the lines in Listing 28.2.</P>
<P><I>Listing 28.2</I><I>—</I><I>HelloWorld.Cpp—With Objects</I></P>
<pre><font color="#008000">// HelloWorld.cpp</font></pre>
<pre><font color="#008000">//</font></pre>
<pre><font color="#008000">#include <iostream.h></font></pre>
<pre><font color="#008000">#include <afx.h></font></pre>
<pre><font color="#008000">class Hello</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">private:</font></pre>
<pre><font color="#008000"> CString message;</font></pre>
<pre><font color="#008000">public:</font></pre>
<pre><font color="#008000"> Hello();</font></pre>
<pre><font color="#008000"> void display();</font></pre>
<pre><font color="#008000">};</font></pre>
<pre><font color="#008000">Hello::Hello()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> message = "Hello from the console!";</font></pre>
<pre><font color="#008000">}</font></pre>
<pre><font color="#008000">void Hello::display()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> cout << message << endl;</font></pre>
<pre><font color="#008000">}</font></pre>
<pre><font color="#008000">int main()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> Hello hello;</font></pre>
<pre><font color="#008000"> hello.display();</font></pre>
<pre><font color="#008000"> return 0;</font></pre>
<pre><font color="#008000">}</font></pre>
<P>Now this is an object-oriented program, and what's more it uses CString, an MFC class. To do so it must include <afx.h>. If you build the project now, you will get linker error messages that refer to _beginthreadex and _endthreadex. By default,
console applications are single-threaded, but MFC is multithreaded. By including afx.h and bringing in MFC, this application is making itself incompatible with the single-threaded default. To fix this, choose Project Settings and click the C/C++ tab. From
the drop-down box at the top of the dialog box, choose Code Generation. In the drop-down list box labeled Use Runtime <U>L</U>ibrary, choose Debug Multithreaded. (The completed dialog box is shown in Figure 28.3.) Click OK, and rebuild the project. </P>
<A HREF="CCfigs03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch28/CCfigs03.gif"><b>Fig. 28.3</b></A>
<P><I>Make your console application multithreaded so that it can </I><I>use MFC.</I></P>
<P>The output of this object-oriented program is just like that of the previous program—this is just a sample. But you see that console applications can use MFC, can be built around objects, and can be quite small. They must have a<font
color="#008000"> main()</font> function and it is this function that is called by the operating system when you run the application.</P>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>Although this application is small, Visual C++ creates a lot of overhead files. The Debug directory occupies about 6.5 M, of which almost a full megabyte is HelloWorld.exe. The rest is the MFC libraries—they aren't small.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P><B>Scaffolding Discrete Algorithms</B></P>
<P>The best argument for anyone to build a DOS application these days is to <I>scaffold</I> small code fragments or single objects. This refers to building a temporary framework around the code you want to test. (Some developers call this a <I>test
harness</I>.) The simplest possible framework is a console application like the one you just built.</P>
<P>To scaffold an object or function, you should do the following:</P>
<ol>
<li><P> Create a new console application just for the scaffolding process.</P>
<li><P> Add a <font color="#008000">main()</font> function to the .CPP file you plan to scaffold.</P>
<li><P> Include the header file for the object or function to be tested</P>
<li><P> Write code that exercises the function or object in a variety of test cases and include it in <font color="#008000">main()</font>.</P>
</ol>
<P>Having followed those steps, you can now test the code thoroughly, focusing only on the performance characteristics and correctness of this small piece of your large project. Scaffolding holds true to the canon of software development which states:
"Design in the large and program in the small."</P>
<P>By applying a scaffold to any algorithm you are helping to ensure the accuracy in the small. Remember there are additional benefits involved too: by placing the scaffold code directly into the module you are clearly documenting that the code has been
tested, and how to use it. You make it available for further testing, debugging, or extending at a later date.</P>
<H3><B>Creating and Using a 32-bit Dynamic Link</B><B>-</B><B>Library</B></H3>
<P>Dynamic-link libraries (DLLs) are the backbone of the Windows 95 and Windows NT operating systems. Windows 95 uses Kernel32.Dll, User32.Dll, and Gdi32.Dll to perform the vast majority of its work, and you can use them as well. The Microsoft Visual C++
On-line Books are a good source of information for API functions found in these three DLLs.</P>
<P>Another tool for poking around in Windows applications is the DumpBin utility in \MSDEV\BIN. This utility is a command line program that shows you the imports and exports of executable files and dynamic link libraries. The following listing is an
excerpted example of the output produced when using DumpBin to examine the executable file for Spy++, one of the utilities provided with Visual C++.</P>
<P><I>Listing 28.3—Output from Dumpbin</I></P>
<pre><font color="#008000">dumpbin -imports spyxx.exe</font></pre>
<pre><font color="#008000">Microsoft (R) COFF Binary File Dumper Version 3.00.5270</font></pre>
<pre><font color="#008000">Copyright (C) Microsoft Corp 1992-1995. All rights reserved.</font></pre>
<pre><font color="#008000">Dump of file spyxx.exe</font></pre>
<pre><font color="#008000">File Type: EXECUTABLE IMAGE</font></pre>
<pre><font color="#008000"> Section contains the following Imports</font></pre>
<pre><font color="#008000"> USER32.dll</font></pre>
<pre><font color="#008000"> 167 LoadCursorA</font></pre>
<pre><font color="#008000"> 135 GetWindowTextA</font></pre>
<pre><font color="#008000"> 1DF SetDlgItemTextW</font></pre>
<pre><font color="#008000"> 153 IsChild</font></pre>
<pre><font color="#008000"> D7 GetClassLongA</font></pre>
<pre><font color="#008000"> D8 GetClassLongW</font></pre>
<pre><font color="#008000"> C5 FillRect</font></pre>
<pre><font color="#008000"> 165 LoadBitmapA</font></pre>
<pre><font color="#008000"> 16B LoadIconA</font></pre>
<pre><font color="#008000"> E6 GetDC</font></pre>
<pre><font color="#008000"> 1F4 SetRectEmpty</font></pre>
<pre><font color="#008000"> 15B IsRectEmpty</font></pre>
<pre><font color="#008000"> 1F3 SetRect</font></pre>
<pre><font color="#008000"> 141 InflateRect</font></pre>
<pre><font color="#008000"> 1FE SetTimer</font></pre>
<pre><font color="#008000"> 162 KillTimer</font></pre>
<pre><font color="#008000"> 1CF SetActiveWindow</font></pre>
<pre><font color="#008000"> 249 wsprintfA</font></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -