Building a Managed .NET Framework Application (2)

December 2, 2009

The next step is to declare a class that represents the form for the application. Select Project, Add Class from the main menu. In the list of available templates in the dialog that is displayed, select the Generic C++ Class template and click the Open button. Name the class CHelloNETForm and specify the base class Form, the .NET Framework’s class found in the System::Windows::Forms namespace, which provides the Windows Form functionality. You may get an error message explaining that Visual Studio .NET cannot find the Form base class. Click Yes to continue adding the class. Performing the previous steps sets up a new file and skeleton code for your class. The next steps will transform your generic class into a managed C++ .NET class.

Click the Class View tab next to the Solution Explorer tab on the right side of the IDE window. Expand the project tree and locate the Classes item and expand that also. You should now see your CHelloNETForm class. Right-click the class and select Add, Add Variable from the context menu. You will now be presented with the Add Variable dialog. Set the access to protected, the variable type to Button*, and the variable name to m_pbtnMessage. Then click Finish. Add another Button* variable named m_pbtnDone and a Label* variable named m_pstMessage.

The variables you just added are .NET Framework classes within the Forms namespace. Controls, however, need to have a container object to hold them. Add another variable of type System::ComponentModel::Container* with the name m_pComponents and the same protected access level.

Taken From: SAMS-Tech Yourself MS Visual C++.NET in 24 Hours

0

Building a Managed .NET Framework Application (1)

November 29, 2009

Switching gears to the .NET Framework and building the same type of application as a managed C++ application is a bit different, as you will see. The first step is the same: Select to build a new project from the main menu, only this time select Managed C++ Application as the type and name the application HelloNET.

Notice that you don’t receive any wizard to ask what settings you would like to have in your application, as you did with the MFC application. The C++ .NET applications are generated with a script that builds a basic application framework that is a console “Hello World” application. One difference between Visual C++ .NET and the other .NET languages is that there is no form editor for Visual C++ .NET to edit Windows Forms, the basis of building user interfaces in .NET. Although it may seem daunting at first having to hand-code the user interface, you’ll find that working with the .NET Framework Forms classes is rather intuitive.

If you want to learn C# also, you can actually use a dummy application in C# that does provide the Windows Form editor to build your forms and then port the code into VC++ .NET. You have to know how to do the porting, but it isn’t that difficult with a little practice—and it saves time on designing your forms

Taken From: SAMS-Tech Yourself MS Visual C++.NET in 24 Hours

0

Building an MFC Application (3)

November 26, 2009

Assign a member variable to the static text control by right-clicking the control and selecting Add Variable from the context menu. The Add Member Variable Wizard, shown in Figure 3.5, is displayed. Edit the member variable properties, as shown, and finish the wizard.

One final step in the dialog editor is to double-click the Message button to add a handler for when the button is clicked. This is a nice new feature added to Visual Studio .NET. When you double-click objects within the dialog editor and other form editors, the most appropriate message map entry and method definition is added to your application. In this case, the ON_BN_CLICKED message map entry and the CHelloMFCDlg::OnBnClickedOK() method are added. Because this button is the OK button, you could have overridden the OnOK() method to handle the selection; however, this method works for all buttons and other control types.

Change the OnBnClickedOK() method to what is shown in the following code to set the message text to a message:

void CHelloMFCDlg::OnBnClickedOk()
{
m_MessageST.SetWindowText(”Hello from the world of MFC.”);
}

Compile and run the HelloMFC application by selecting the Debug, Start menu item from the menu bar or by pressing the F5 key. Clicking the Message button within the application displays your message, as shown in Figure 3.6.

If you are already familiar with building MFC applications in previous versions of Visual Studio, the HelloMFC application should have been simple for you to create within Visual Studio .NET. What you’ve created, however, is an unmanaged C++ project. This application has nothing to do with .NET and, as such, does not run within the .NET runtime. Any memory allocation, versioning issues, and other things that benefit .NET applications will have to be done manually using this type of project. To run in the .NET runtime, you need a managed C++ application.

Taken From: SAMS-Tech Yourself MS Visual C++.NET in 24 Hours

0

Building an MFC Application (2)

November 23, 2009

Select the Application Type section in the wizard and change the settings to match those shown in Figure 3.3. These selections will result in creating a dialog-based application when you click the Finish button.

The first thing you will see after dismissing the settings dialog is the dialog editor. If your dialog requires any controls, you can easily select them from the toolbox displayed on the left. For this project, however, we are just going to change the controls that are already present.

First of all, select the button labeled OK. On the right side of the IDE, you should see a window titled Properties. This is a departure from the old way of doing things in Visual C++, which required editing properties through small tool windows. Properties are now categorized and always displayed without having to select a menu item or click a toolbar button. Change the property value labeled Caption to Message by typing the new value in and pressing Enter. You should see the text on the button change as you do this.

Now you need to change the control ID of the static text control. Select the static text control located in the middle of the dialog in the dialog editor and change the ID property to the value IDC_MESSAGE.

Taken From: SAMS-Tech Yourself MS Visual C++.NET in 24 Hours

0

Building an MFC Application (1)

November 20, 2009

Building an MFC application with Visual Studio .NET is very similar to the way it was done with previous versions of Visual Studio. The Application Wizard is available to allow you to customize your settings, although it has a different look.

As usual with Visual Studio, first select the New, Project menu option to display the New Project dialog, as shown in Figure 3.1. Select the MFC application type and name the application HelloMFC.

Pressing the OK button in the New Project dialog brings up the MFC Application Wizard, shown in Figure 3.2. This wizard is no longer a linear process; you can now select the different sections on the left side of the wizard page and directly access the settings you want to change. The differences between this and the Application Wizard for MFC applications in the previous version of Visual Studio are mainly visual and navigational.

Taken From: SAMS-Tech Yourself MS Visual C++.NET in 24 Hours

0

Hour 3. Writing a Simple C++ .NET Program

November 17, 2009

Inevitably every programming book and programming course has you create as your first application—a simple “Hello World” program. Therefore, this hour’s lesson is dedicated to writing your first simple application with Visual C++ .NET.

The difference is, this lesson already assumes you know how to create a simple application and are at least somewhat experienced with the previous version of Visual C++. With that assumption, the lesson will take you through creating two applications: one with MFC and one using the .NET Framework. This allows you to compare the two methods of programming Windows applications with Visual C++ .NET.

In this hour you will learn:

How to build a simple application with MFC

How to build a managed application with the .NET Framework

How to study and understand the differences between the two types of applications

Taken From: SAMS-Tech Yourself MS Visual C++.NET in 24 Hours

0

Workshop

November 14, 2009

The Workshop provides quiz questions to help solidify your understanding of what was covered in this hour. Answers are provided in Appendix A, “Quiz Answers.”

Quiz
1: What is the keyword to declare a class as managed?

2: Attributes work similarly to what other feature in C++?

3: What compiler directive is required to produce an application for the .NET Framework?

Taken From: SAMS-Tech Yourself MS Visual C++.NET in 24 Hours

0

Q&A

November 11, 2009

Where do I get all the information on the new language features and the compiler and linker directives not required for .NET development?

A1: Microsoft provides a complete list of the new compiler directives in its online help that ships with Visual Studio .NET. Search for “What’s New” in the online help to bring up a list of topics.

Q2: What is an assembly?

A2: An assembly is the finished result of a .NET project. It contains what is known as metadata, which describes data types such as structures or classes, version information, and internal resources such as graphics and/or sound files. This information is stored in what is called the assembly manifest.

Q3: How do I enter command-line options to my project?

A3: If you know what option you need, select Project, Properties from the main menu. Under the Configuration Properties heading, expand the C/C++ heading and select Command Line. You can add extra command-line options in the edit box titled Additional Options, located on the right. To specify additional linker options, follow the same steps but select the Linker heading instead of the C/C++ heading mentioned earlier

Taken From: SAMS-Tech Yourself MS Visual C++.NET in 24 Hours

0

Summary

November 8, 2009

This hour you learned about the new special features in Visual C++ .NET that allow it to create managed code for the .NET Framework. Other new features have been added to the language, compiler, and linker to provide enhancements and support for 64-bit Windows development. However, they are not required for .NET development.

As you read through the other hours, you will work with the new language features you’ve become familiar with in this hour, and you will see how they fit into the bigger picture of creating .NET Framework applications and working with legacy Visual C++ applications.

Taken From: SAMS-Tech Yourself MS Visual C++.NET in 24 Hours

0

Pragmas, Compiler, and Linker Features (6)

November 5, 2009

Using New Linker Options
The new linker options allow you to customize the linking of your managed .NET Framework modules. The first, /NOASSEMBLY, produces a module that is not an assembly by itself, but can be added to an assembly. This option is redundant with the /clr:noassembly directive described earlier and has the same effect.

The /ASSEMBLYMODULE:filename linker option is used when you have a module that is not part of an assembly. This option binds the module to the specified assembly. Although the code within the assembly that the module is bound to cannot access the module code, any application that uses the resulting assembly can access everything within the assembly.

The last link option, /ASSEMBLYRESOURCE:filename, allows you to bind a .NET Framework resource file to an assembly. These resources are then accessed with the System.Resources namespace classes. The resource file has a .resources extension and is generated by the Resource File Generator (RESGEN.EXE) or in the development environment.

Taken From: SAMS-Tech Yourself MS Visual C++.NET in 24 Hours

0