Creating a Simple Windows Form


Using Visual C++'s Application Wizard to create a simple dialog-based application using MFC is a relatively easy task. Just by setting a few configuration options, the wizard creates a dialog template and associated source code for the class that uses that template. An instance of the class is then used as the main window for the application.

Creating the same simple application with Visual C++ .NET in managed code and thus using Windows Forms is not as simple. Because there are no resource files to define templates for dialogs and there is no integrated form designer that can be used with Visual C++ .NET, you must create Windows Forms using source code instead. However, both Visual Basic .NET and C# do have a form designer for Windows Forms. In fact, due to the similarities in syntax between C# and C++, you may even find it easy to design the interface using the C# form designer and port the code to C++.

In practice, until Microsoft provides a form designer, you will most likely develop your interface in C# or Visual Basic .NET and use those classes in your C++ applications. However, this hour's lesson will teach you how to create Windows Forms in C++. After completing this lesson, you will also understand how to create Windows Forms in VC++ .NET.

Creating a New Application

Select the File, New, Project menu item to display the New Project dialog. Select to create a new managed C++ application and name it SimpleWindowsForm. This will create a basic managed C++ application.

Open the SimpleWindowsForm.cpp file in the editor, and you will see the code shown in Listing 7.1, which is generated for you. Compiling the application at this stage produces nothing more than a "Hello World" console application.

Listing 7.1 SimpleWindowsForm.cpp File Content After First Creating the Application
 1: // This is the main project file for VC++ application project  2: // generated using an Application Wizard.  3:  4: #include "stdafx.h"  5:  6: #using <mscorlib.dll>  7: #include <tchar.h>  8:  9: using namespace System; 10: 11: // This is the entry point for this application 12: int _tmain(void) 13: { 14:    // TODO: Please replace the sample code below with your own. 15:    Console::WriteLine(S"Hello World"); 16:    return 0; 17: } 

One of the first things you see in this file, following the #include statement, is a #using preprocessor directive. Whenever you need to use a class within a namespace, you have to direct the compiler to the location of the file that contains this namespace. In Listing 7.1, you are notifying the compiler that you will be using certain namespaces and classes within the mscorlib.dll file. Now that the file containing the namespaces you need has been imported, you can then zero in on which namespaces you actually plan on using. Therefore, line 9 contains the using keyword for the System namespace.

The next piece of code, starting on line 12, may look a little confusing if you've never programmed in a Unicode environment. Unicode allows you to create applications that can be compiled to run on a single-byte system (so called because all characters are only 1 byte) or compiled to run on a double-byte system (where a single character is comprised of 2 bytes). Therefore, when Listing 7.1 is compiled, the compiler will replace the _tmain function with main if compiled for single-byte systems or wmain if compiled for double-byte character systems.

A question you may be asking is, what does this _tmain function do? Every executable application compiled for Windows has to have at least one entry point that the operating system can call in to upon the initial loading of the program. If the application runs within the console, it has a main (or _tmain) entry point. However, if your application uses a Graphical User Interface (GUI), the main entry point for that application is WinMain.


You can easily change your application to run without the console window. Include the windows.h header file following the tchar.h file's include line and change line 12 in Listing 7.1 to read as follows:

 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpszCmdLine, int nCmdShow ) 


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: