Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Windows Forms in Managed C++

0.00/5 (No votes)
7 Aug 2003 1  
An MC++ class used to create Windows Forms based application.

Introduction

Visual C++ .NET is a powerful tool to build Windows applications for C or C++ programmers. It is useful to create dynamic web applications as well as XML based web services. The Visual C++ .NET includes MFC (Microsoft Foundation Classes), ATL (Active Template Library) and STL (Static Template Library). It will support both 32-bit programming and 64-bit programming. The C++ programmer can also invoke the .NET CLR (Common Language Runtime) support with /CLR switch at compile time, with MSIL (Microsoft Intermediate Language) features. Microsoft has provided two types of C++ Libraries. They are Managed C++ and Unmanaged C++. The C++ programmer can mix managed and unmanaged codes. The managed C++ is the simplest form of C++.

The user gets unique features, managed code garbage collection, threading, and access to all .NET framework classes, and unmanaged C++ features like ActiveX� Controls. All the features are available through the Visual studio .NET IDE (Integrated Development Environment). The Visual Studio .NET has improved debugging capability like managed debugging, unmanaged debugging, multilanguage debugging and remote debugging.

Garbage Collection:

The managed code is the code targeted at the Common Language Runtime (CLR). C#, Visual Basic .NET and Jscript are managed by default. But, C++ is not managed even with /CLR switch compilation. We mention the C++ class as a __gc at the beginning. The __gc memory instance of the class is managed by the garbage collector.

Garbage collection is a mechanism that allows the system to detect when an object can no longer be accessed. It releases unused memory automatically. This mechanism is a very useful new feature in managed C++. It is possible to call native Win32 functions from the .NET framework. The __gc class is the most general class in managed C++. The __gc interface supports the COM style interface programming in C++. A __gc class shall not inherit from more than one managed class.

Any file containing metadata like *.dll, *.obj and *.exe can be referenced by C++ source files. This features gives maximum speed in the compilation time. Because, the compiled code is faster then text files. The managed C++ also supports Windows� resource types like bitmap, brush, and region.

The __gc class supports struct or class data type. The difference between struct and class is, the default visibility in struct is public and default visibility in class is private. The __gc class can use any number of interfaces. The __gc shall not use new or delete operators. This also supports visibility like private, public or protected. We do not create any copy constructor. All the __gc classes derive from System.Object root class. When we call the destructor, the memory will be cleared automatically.

MFC and Managed C++:

The MFC gives the easiest way to create C++ based Windows� GUI applications compared to native Win32 API� programming. Now, Microsoft Foundation Classes has many features and many classes have been added (more then 200 classes) with version 7.0. If we create a SDI (Simple Document Interface) or MDI (Multiple Document Interface), we just click some class wizard and we will get new applications with document architecture feature. The .NET framework class library gives a new set of classes that can be accessed by all levels of programmers in a unique way. The programming language may be different. But, the native code is same in Microsoft Common Language. MFC is a class library to build document based applications. It has some features like serialization. Windows Forms is a windowing library for N-Tier architecture.

Windows Forms:

In Windows based application, we will create at least one form. In that form, we will have some number of controls included. The main form is the parent control. Windows Forms require at least one form. It may be a message box or the Windows Form.

The simplest form in Windows is the Windows message box .The following code example gives a first view of the programming world.

#using<mscorlib.dll>
#using<System.Windows.Forms.dll> 
// Class derived from Forms

using namespace System::Windows::Forms;
int main()
{
  MessageBox::Show("Hello World");
  return 0;
}

In the above example, #using is used to import the metadata from filename.obj, filename.dll or filename.exe. The message box is the parent form of this example and has many other forms. This program has the following output:.

Example 1

Sample screenshot

If we create a simple form in managed C++, first we will create the instance of the Form class. Then the application will run on the main program. The main function is the entry point in a C++ program. Any C/C++ program should have at least one main function.

OK. We will see the next example of using forms in .NET framework using managed C++. We already saw the fundamental concepts in garbage collection. Now, we implement a class derived from __gc class.

#using<mscorlib.dll>
using namespace System;
#using<System.DLL>
#using<System.Windows.Forms.dll>
using namespace System::Windows::Forms;

// __gc class garbage collected derived from Form

__gc class Hello : public Form
{
    // Visibility as public

public:
    Hello()
    {
        Text="Hello World";
        m_p=new Button();
        m_p->Text="Click";
        m_p->Top=120;
        m_p->Left=100;
        m_p->Click += new EventHandler(this, button_click);
        this->Controls->Add(m_p);
    }
    // Button click event

    void button_click(Object* sender, EventArgs* e)
    {
        MessageBox::Show("Hello World!");
    }
private:
    //Button class as a private

    Button *m_p;
}; 

// Main function 

int main() 
{ 
    Application::Run(new Hello);
    return 0;
}
// end of program

The class Hello is derived from Form class. All the classes are derived from System.Object root class. So, Hello class contains the basic functionality of Form class. In Hello class constructor, we mention the form title, change the background color, and etc. We add child controls like Button, TextBox, ListBox and other Windows controls.

Comparing MFC, the managed C++ using Windows Forms easily creates the controls. In our example, Button class is declared as m_p, then we add the Click event. In the Click event, the message box is displayed.

The output in the above example is:

Sample screenshot

Example 2

If we click the button, the event occurs. So, the message box is displayed.

In the above two examples, we saw the basic functionality in managed C++. In our final example, we will implement some of the features. The form contains the TextBox, GroupBox, Button and RadioButtons. In that text box, we will type any character, the right hand side GroupBox contains the color changed in the foreground and background.

The class Snotepad is derived from Form class and it supports __gc also. If we click the background color as blue, then the TextBox color will be changed. The foreground color is changed also when we click the foreground RadioButton.

// __gc class garbage collected SNotepad derived from Class Form 

__gc class SNotepad : public Form 
{
    // Visibility as public

public:
    // Inialize the controls

    SNotepad()
    {
        // Display the title in the parent form

        Text="Hello Windows Controls ";
        // Main From properties

        Bounds = Rectangle(200,200,200, 200);
        Visible = false;
        MaximizeBox =false;
        ClientSize = System::Drawing::Size(600,400);
        // Textbox properties

        txtBox = new TextBox();
        txtBox->Font = new System::Drawing::Font( new 
                 FontFamily( S"Times New Roman"), 14.0f );
        txtBox->Size = System::Drawing::Size(400, 600);
        txtBox->TabIndex = 1;
        txtBox->Multiline = true;
        txtBox->ScrollBars = ScrollBars::Both;
        this->Controls->Add(txtBox);

        // OK button properties

        color=new Button();
        color->Text="Ok";
        color->Top=320;
        color->Left=420;
        this->Controls->Add(color);
        // OK Button event handler

        color->Click += new EventHandler(this, okbutton_click);
        // Cancel button properties

        cancel=new Button();
        cancel->Text="Cancel";
        cancel->Top=320;
        cancel->Left=500;
        this->Controls->Add(cancel);
        cancel->Click += new EventHandler(this, cancelbutton_click);
        // Change to the default color button

        def=new Button();
        def->Text="Default";
        def->Left=420;
        def->Top=350;
        this->Controls->Add(def);
        def->Click += new EventHandler(this, defbutton_click);

        // Clear the text box when user click this button

        clear=new Button();
        clear->Text="Clear";
        clear->Top=350;
        clear->Left=500;
        this->Controls->Add(clear);
        clear->Click += new EventHandler(this, clearbutton_click);

        // Groupbox contains foreground color properties

        grpColor=new GroupBox();
        grpColor->Location = Point(410, 30);
        grpColor->TabStop = false;
        grpColor->Text = S"Foreground Color : ";
        grpColor->Size = System::Drawing::Size(150,120);
        // Add to the main form

        this->Controls->Add(grpColor);
        // Group box contains background color properties

        grp=new GroupBox();
        grp->Location = Point(410,170);
        grp->TabStop = false;
        grp->Text = S"Background Color : ";
        grp->Size = System::Drawing::Size(150,120);
        this->Controls->Add(grp);

        // Foreground color ratio button

        radColor1=new RadioButton();
        radColor2=new RadioButton();
        radColor3=new RadioButton();

        // Location

        radColor1->Location=Point(15,15);
        radColor2->Location=Point(15,45);
        radColor3->Location=Point(15,75);
        // Caption of the ratiobutton

        radColor1->Text="Red";
        radColor2->Text="Blue";
        radColor3->Text="Green";
        // Handler to the button

        radColor1->Click +=new EventHandler(this,radColor1_Click);
        radColor2->Click +=new EventHandler(this,radColor2_Click);
        radColor3->Click +=new EventHandler(this,radColor3_Click);
        // Add to the groupbox

        grpColor->Controls->Add(radColor1);
        grpColor->Controls->Add(radColor2);
        grpColor->Controls->Add(radColor3);
        // Create ratio button to change background color

        radColor4=new RadioButton();
        radColor5=new RadioButton();
        radColor6=new RadioButton();

        // Specify the location

        radColor4->Location=Point(15,15);
        radColor5->Location=Point(15,45);
        radColor6->Location=Point(15,75);
        // Initiaize text in radio button

        radColor4->Text="Red";
        radColor5->Text="Blue";
        radColor6->Text="Green";

        // Add event handler

        radColor4->Click +=new EventHandler(this,radColor4_Click);
        radColor5->Click +=new EventHandler(this,radColor5_Click);
        radColor6->Click +=new EventHandler(this,radColor6_Click);

        // Add to the group box

        grp->Controls->Add(radColor4);
        grp->Controls->Add(radColor5);
        grp->Controls->Add(radColor6);
    }

    //Change forcolor to red

    void radColor1_Click(Object *sender,EventArgs* e)
    {
        txtBox->ForeColor=Color::Red;
    } 
    //Change forcolor to Blue

    void radColor2_Click(Object *sender,EventArgs* e)
    {
        txtBox->ForeColor=Color::Blue;
    }
    //Change forcolor to green

    void radColor3_Click(Object *sender,EventArgs* e)
    {
        txtBox->ForeColor=Color::Green;
    }
    // Change backcolor to Red

    void radColor4_Click(Object *sender,EventArgs* e)
    {
        txtBox->BackColor=Color::Red;
    }
    // Change backcolor to Blue

    void radColor5_Click(Object *sender,EventArgs* e)
    {
        txtBox->BackColor=Color::Blue;
    }
    // Change backcolor to green

    void radColor6_Click(Object *sender,EventArgs* e)
    {
        txtBox->BackColor=Color::Green;
    }
    // Initialize the color properties,

    // background as white and forground as black

    void defbutton_click(Object *sender,EventArgs* e)
    {
        txtBox->BackColor=Color::White;
        txtBox->ForeColor=Color::Black;
    }
    // Clear the TextBox

    void clearbutton_click(Object *sender,EventArgs* e)
    {
        txtBox->Text=" ";
    }
    // When the user click ok button, the messagebox

    // displayed and Close the form

    void okbutton_click(Object* sender, EventArgs* e)
    {
        MessageBox::Show("Now You Close this application!");
        Application::Exit();
    }
    // When user click cancel button the application terminited 

    void cancelbutton_click(Object* sender, EventArgs* e)
    {
        Application::Exit();
    }
    // Free the memory

    virtual ~SNotepad()
    {
    }
 
private: 
    // Declare the Control Variables

    TextBox * txtBox;
    Button *color;
    Button *cancel;
    GroupBox* grpColor;
    RadioButton *radColor1;
    RadioButton *radColor2;
    RadioButton *radColor3;
    GroupBox* grp;
    RadioButton *radColor4;
    RadioButton *radColor5;
    RadioButton *radColor6;
    Button *clear;
    Button *def;
};

The output of this example is:

Example3

Sample screenshot

If we click the Clear button, the text box clears the messages. If we click OK button, it will display the message box with OK button. If click OK, the application is terminated. If we click Cancel button, it terminates directly.

Conclusion:

From the above examples, a beginner programmer will be benefited. Because the examples are easy to understand the basic concepts of managed C++. These examples are use .NET framework from command line. I did not use the .NET IDE (Integrated Development Environment). It will work fine in command line with /CLR compiler option. Working in C++ can let you write faster code than working in C# or VB.NET. If you are comfortable working in C++, there are real benefits for you. The .NET Framework class library provides a vast, rich and uniform class library. The MFC and Managed C++ are designed for different purposes. The .NET framework classes have automatic security and uniform manner.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here