< C++ .NET Delegates & Events 7 | Main | C++ .NET Windows Form 2 >



Introducing Windows Forms 1



  1. Windows Forms Applications

  2. Windows Forms and Designers

  3. Windows Forms vs. MFC

  4. A Word About ATL

  5. The System::Windows::Forms Namespace

  6. Creating and Using Forms

  7. Creating a Simple Form


  1. What is a Message Loop?

  2. Using Form Properties

  3. Form Relationships

  4. Placing Controls on the Form

  5. Handling Events

  6. Using Controls

  7. Controls and Events

  8. Label

  9. Button

  10. CheckBox and RadioButton

  11. Using Radio Buttons as a Group, a GroupBox

  12. ListBox and ComboBox

  13. TextBox

  14. Using Menus

  15. Menu Design

  16. More About Menus

  17. Displaying a Context Menu

  18. Very Quick Reference

Windows Forms is a powerful feature of the Microsoft Windows .NET Framework that provides a set of classes for building GUI applications. In contrast to most other GUI libraries, Windows Forms can be used from any .NET language, and you can now easily build mixed-language graphical applications. Windows Forms is a large and complex subject, encapsulating the whole of writing GUI applications. The subject is worth a book in its own right, so the following two modules can only scratch the surface to give you a taste of how Windows Forms operates and how you can use it to write GUI applications.


Windows Forms Applications


If you've ever programmed in Microsoft Visual Basic, the idea behind Windows Forms will be familiar to you. In fact, for the .NET Framework, Microsoft has taken the Visual Basic GUI programming model, complete with forms, controls, and properties, and generalized it so that it can be used from any .NET language. A Windows Forms application consists of one or more windows called forms. These might be top-level windows, child windows, or dialog boxes, and an application might support many different forms. You place controls, such as buttons and list boxes, onto a form to build the GUI for your program. A simple form is shown in the following figure.


Widows Form example


This form is a top-level window that is resizable and has a title bar; it also has Minimize, Maximize, and Close buttons on the right and a system menu button on the left. It contains a selection of common controls, including a button, radio buttons, a group box, and a combo box. You can also draw on forms using the classes in the System::Drawing namespace; we'll cover drawing on forms in another module.


Windows Forms and Designers


Although you can write code to generate forms, you will normally use designers to create the user interface. Microsoft Visual Studio .NET supports graphical GUI construction using designers, which put a form on the screen and let you drag components from a Toolbox and drop them onto the form. A Properties editor lets you set the properties of forms and controls, such as color and text and makes it easy to add event handlers. This way of developing forms will be familiar to anyone who has used Visual Basic in the past. Forms are always constructed at run time by executing code, and designers simply take what you do on the screen and generate code underneath, which will create the user interface for you at run time. If you open a Visual Basic or C# project and look at the source code, you'll see a section labeled 'Windows Form Designer generated code,' which contains that code. There was no designer for Microsoft Visual C++ included in the first release of Visual Studio .NET, so you had to create and populate forms yourself by writing code. Starting from version 7.1, it includes a C++ designer, so you can develop GUI applications in the same way that you would in Visual C# or Visual Basic .NET. In this Tutorial we will use Visual C++ .NET 2005 Express Edition with .NET Framework 2.0.x. Newer version may have more features and functionalities.





Windows Forms vs. MFC


Since its earliest days, Visual C++ has shipped with a library called MFC (Microsoft Foundation Classes). This library is used for writing C++ applications for Microsoft Windows, and it encapsulates mostly the part of the Windows API that deals with GUI programming, along with some other areas such as databases and networking. MFC has become a standard for writing Windows applications in Visual C++. The latest version ships with Visual Studio .NET, and there's a lot of MFC code still around. It has nothing to do with .NET, however, and you probably won't want to use it for new Windows development in C++. However, you still might need to use MFC when:

  1. You have an MFC application, and you aren't updating it (or can't update it) to use Windows Forms.

  2. You've got to use some existing MFC components.

  3. You really like the MFC Document-View architecture, which isn't supported in the .NET Framework.

In the vast majority of cases, though, you'll want to use the .NET Framework. The object-oriented coverage of the Windows API is more complete, and it's better object-oriented code. Plus, mixed-language programming is easy with .NET.


A Word About ATL


While on the subject of C++ libraries for Windows programming, we'll also mention the other major Microsoft offering, the Active Template Library (ATL). ATL is a C++ library for writing the smallest, fastest COM objects possible. As such, it's a very specialized library, and few people will need to use it now that the .NET Framework has made COM less of a mainstream technology. Keep in mind that the .NET Framework makes it less necessary for developers to know about and use COM because it provides technologies that make some of the traditional uses of COM (for example, general components and Microsoft ActiveX controls) less necessary. So, COM has been pushed more into the background as a specialized, mainly server-side technology.


The System::Windows::Forms Namespace


The Windows Forms classes are provided in two namespaces: System::Windows::Forms and System::Windows::Forms::Design. The second of these contains classes for design-time use, which typically means customizing and extending the designers used in Visual Studio .NET. Because this namespace is more advanced than we have time or space for here, we won't mention this namespace again. System::Windows::Forms is a very large namespace, containing more than 300 classes and enumerations. The following table shows some of the major members of this namespace, and you'll meet lots more during the rest of this module and when the new version published.





Provides static methods to manage applications, including starting and stopping them, and getting information about them.


Provides a way to host ActiveX controls in Windows Forms applications.


An enumeration that specifies the border style for controls. BorderStyle has three members: Fixed3D for three-dimensional borders, FixedSingle for simple line borders, and None for no border.


Represents a Windows button control.


An enumeration that specifies the appearance of a button. Members include Inactive, Pushed, and Normal.


Represents a Windows check box control.


Lets you place data onto and retrieve it from the system clipboard.


Displays a standard dialog box to let the user pick a color.


Represents a Windows combo box control.


Represents a cursor.


Displays ADO.NET data in a scrollable grid.


Represents a Windows date-time picker control.


Represents a window or a dialog box.


Represents a Windows horizontal scroll bar control.


Represents a collection of images typically used by toolbars.


Represents a Windows label control.


Represents a Windows list box control.


Displays a list of items in one of four views.


Represents a menu.


Displays a message box.


A control that can contain other controls.


Represents a Windows progress bar control.


Represents a Windows radio button control.


Provides splitter functionality to a window.


Represents a Windows status bar control.


Represents a Windows edit control.


Represents a Windows toolbar control.


Table 1


Creating and Using Forms


Because Visual C++ .NET now has a GUI designer, you no longer have to construct GUI code by hand. This section will show you how to create and use forms using Visual C++ .NET 2005 Express Edition (can be applied to Visual Studio .NET 2005 as well).


Creating a Simple Form


The following exercise shows you how to create and display a simple form.


1.        Start Visual Studio/C++ .NET, and open the New Project dialog box. Choose the CLR Projects types, and create a new Windows Forms Application project,

named CppForm.






Creating a new Windows Forms Application project, named CppForm


2.        Visual C++/Studio .NET will display a form designer window like the one shown in the following figure.


Windows Form - a form designer window


3.        Compile and run the code. A window will be displayed, as shown in the following figure.


Compiling and running the Windows form code - Windows form output


You can see how the form has all the attributes of a window: it has a title bar with a caption and a system menu button on the left; it has Minimize, Maximize, and Close buttons; and you can resize it. The size and caption are set by properties in the C++ code.


4.   In the GUI designer, right click anywhere in the form and select Properties context menu. The properties page for Form1 will be displayed. Take note the name of the object, Form1 at the top-leftmost of the properties page. You may need to refer to the object name when there are many objects in your GUI program because the properties page looks similar for all objects.


Invoking a Windows Form Properties Editor.


Windows form property page with all sort of controls


5.        Right-click the form displayed in the designer, and choose View Code from the context menu. This will display the code for Form1.h.


Displaying the code for Windows form


#pragma once


namespace CppForm {

        using namespace System;

        using namespace System::ComponentModel;

        using namespace System::Collections;

        using namespace System::Windows::Forms;

        using namespace System::Data;

        using namespace System::Drawing;


        /// <summary>

        /// Summary for Form1


        /// WARNING: If you change the name of this class, you will need to change the

        ///            Resource File Name' property for the managed resource compiler tool

        ///            associated with all .resx files this class depends on.' Otherwise,

        ///            the designers will not be able to interact properly with localized

        ///            resources associated with this form.

        /// </summary>

    public ref class Form1 : public System::Windows::Forms::Form







                //TODO: Add the constructor code here





            /// <summary>

            /// Clean up any resources being used.

            /// </summary>



                if (components)


                    delete components;





        /// <summary>

        /// Required designer variable.

        /// </summary>

System::ComponentModel::Container ^components;


#pragma region Windows Form Designer generated code

                /// <summary>

                /// Required method for Designer support - do not modify

                /// the contents of this method with the code editor.

                /// </summary>

                void InitializeComponent(void)


                    this->components = gcnew System::ComponentModel::Container();

                    this->Size = System::Drawing::Size(300,300);

                    this->Text = L"Form1";

                    this->Padding = System::Windows::Forms::Padding(0);

                    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;


#pragma endregion




If you examine the code in the InitializeComponent function, you'll see where the Size and Text properties are set. You'll see later in the module how to use the Properties editor to change these properties.


6.   Click the Close button, the one marked with an X on the far right of the title bar to close the launched window. This terminates message processing, and closes the window.



Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6



< C++ .NET Delegates & Events 7 | Main | C++ .NET Windows Form 2 >