如前所述,控制台应用程序让你能够把应用程序的商务逻辑从DOS环境迁移到Windows环境。你也可以迁移某些(甚至是绝大多数)显示逻辑,但也需要一些MFC提供的功能。本质上讲,控制台应用程序就像添加了一些特性的DOS应用程序。编码之后,需要彻底地测试控制台应用程序,以确保从DOS迁移到Windows的各种特性依然能够正确地工作。
让我们探讨一个相当简单的控制台应用程序,看看在这个程序中你能做些什么。这个示例中,我们并不把程序的功能看得很重要,只是要了解应该如何完成示例。当然,第一步的工作是创建程序框架。按下述步骤进行操作:
1. 如果你还没有启动Visual C++,那么启动Visual C++。
2. 使用File|New命令显示如下图所示的New对话框。注意,我已经选择了Projects选项卡并加亮了要在本例中使用的工程类型。

3. 当选择了Win32 Console Application后,在Project Name域中键入程序名称。这个样本程序使用的名称为Console。你或许还需要修改一下Location域的内容,那么单击一下该域旁边的浏览按钮,系统会显示Choose Directory对话框,在这个对话框中选择应用程序的存放目录。
4. 单击OK按钮。你会看到Win32 Console Application - Step 1 of 1对话框,如下图所示。注意,在这个对话框中有几个应用程序类型供选择。这也是VisualC++ 6.0的一个新特性。以前版本的Visual C++简单地创建一个空的工程。现在到了选择要创建哪种类型工程的时候了(即使空工程也要进行选择)。

5. (必要时)选择An Empty Project,然后单击Finish。你会看到一个NewProject Information对话框,它告诉你都选择了哪些选项。
6. 单击OK按钮创建示例程序。
在该工程真正能够运行之前,还需要完成其它一些步骤。该工程需要MFC类。当你把对该类的支持加入到上面的应用程序向导的设置中时,再次使用该向导把DOS应用程序转换到Windows下时所需的重写代码的代码量就会显著减少。选择Project | Settings命令显示Project Settings对话框,选择该对话框的General选项卡,并在Settings For组合框中选择All Configurations,在MicrosoftFoundation Classes组合框中选择Use MFC in a Shared DLL选项。单击OK完成设置工作。
现在到了给示例程序添加代码的时候了。需要完成的第一件事是向工程中添加一个文件,让我们看看添加过程。
1. 使用File | New命令打开New对话框,选择File选项卡,其中显示了文件类型的完整列表,包括Resource Template以及像Icon File这样的各种图像文件。
2. 加亮C++ Source File选项。由于我们要在样本代码中添加一些类,因此要选择C++源文件选项。
3. 在File Name域中键入Console(Visual C++会自动添加上正确的文件扩展名)。
4. 单击OK。系统显示一个空的C++源文件。
现在我们有了一个要使用的空文件,该添加一些代码了。程序列表2.1显示了该示例的C++源程序。注意,其中它既直接包括了一些C代码,也包括了一些C++代码。我这样做的目的就是为了让读者看清楚在这种环境下系统是如何工作的。
程序列表2.1
#include
Class CDrawBox : public CObject
{
public :
// Draws the box.
void DoDraw(char * string)
};
void CDrawBox :: DoDraw(char * cValue)
{
int iCount; //Loop counter
int iSpace; //Amount of spaces to add for string.
// Draw the top of the box
fprintf(stdout,"\311");
for (iCount = 1; iCount <= 78 ; iCount ++)
{
fprintf(stdout,"\315");
}
fprintf(stdout,"\273");
// Figure out the center of the string, then display it
// with the box sides.
iSpace = (80 - strlen(cValue)) / 2;
fprintf(stdout,"\272");
for (iCount = 1; iCount <= iSpaces ; iCount ++)
{
fprintf(stdout," ");
}
fprintf(stdout, "%s", cValue);
// Compensate for odd sized strings, then complete the side.
if ((strlen(cValue) % 2) == 1)
{
iSpaces --;
}
for (iCount = 1; iCount <= iSpaces ; iCount ++)
{
fprintf(stdout, " ");
}
fprintf(stdout,"\272");
// Draw the bottom of the box
fprintf(stdout, "\310");
for (iCount = 1; iCount <= 78 ; iCount ++)
{
fprintf(stdout, "\315");
}
fprintf(stdout, "\274\n");
}
int main(int argc, char ** argv)
{
char * cName;
// Name of person typed at command line.
char * cLocale;
// Program execution location.
CTime oMyTime;
// A time object.
CString cDate;
// String used to hold time and date.
CDrawBox oMyDraw;
// Special text display.
// See if we have enough command line arguments.
if ( argc != 2)
{
fprintf(stderr, "Type the program name followed by your name.\n");
return 1;
}
// Get the command line arguments
cLocale = argv[0
