Click here to Skip to main content
15,847,583 members
Articles / Programming Languages / C++/CLI

Windows Forms in Managed C++

Rate me:
Please Sign up or sign in to vote.
2.89/5 (14 votes)
7 Aug 2003CPOL6 min read 165.2K   3.5K   19   13
An MC++ class used to create Windows Forms based application.


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.

// 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 namespace System;
using namespace System::Windows::Forms;

// __gc class garbage collected derived from Form
__gc class Hello : public Form
    // Visibility as public
        Text="Hello World";
        m_p=new Button();
        m_p->Click += new EventHandler(this, button_click);
    // Button click event
    void button_click(Object* sender, EventArgs* e)
        MessageBox::Show("Hello World!");
    //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
    // Inialize the controls
        // 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;

        // OK button properties
        color=new Button();
        // OK Button event handler
        color->Click += new EventHandler(this, okbutton_click);
        // Cancel button properties
        cancel=new Button();
        cancel->Click += new EventHandler(this, cancelbutton_click);
        // Change to the default color button
        def=new Button();
        def->Click += new EventHandler(this, defbutton_click);

        // Clear the text box when user click this button
        clear=new Button();
        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
        // 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);

        // Foreground color ratio button
        radColor1=new RadioButton();
        radColor2=new RadioButton();
        radColor3=new RadioButton();

        // Location
        // Caption of the ratiobutton
        // 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
        // Create ratio button to change background color
        radColor4=new RadioButton();
        radColor5=new RadioButton();
        radColor6=new RadioButton();

        // Specify the location
        // Initiaize text in radio button

        // 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

    //Change forcolor to red
    void radColor1_Click(Object *sender,EventArgs* e)
    //Change forcolor to Blue
    void radColor2_Click(Object *sender,EventArgs* e)
    //Change forcolor to green
    void radColor3_Click(Object *sender,EventArgs* e)
    // Change backcolor to Red
    void radColor4_Click(Object *sender,EventArgs* e)
    // Change backcolor to Blue
    void radColor5_Click(Object *sender,EventArgs* e)
    // Change backcolor to green
    void radColor6_Click(Object *sender,EventArgs* e)
    // Initialize the color properties,
    // background as white and forground as black
    void defbutton_click(Object *sender,EventArgs* e)
    // 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!");
    // When user click cancel button the application terminited 
    void cancelbutton_click(Object* sender, EventArgs* e)
    // Free the memory
    virtual ~SNotepad()
    // 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:


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.


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
India India
Selvam has worked on several technologies like Java, Python, Big data, VC++, MFC, Windows API and Weblogic server. He takes a lot of interest in reading technical articles and enjoys writing them too. He has been awarded as a Microsoft Community Star in 2004, MVP in 2005-06, SCJP 5.0 in 2009, Microsoft Community Contributor(MCC) 2011.

Big Data
o Google Professional Data Engineer 2021
o Confluent Certified Developer for Apache Kafka 2020
o Datastax Apache Cassandra 3.x Developer Associate Certification 2020
✓ Cloud
o Google Professional Cloud Architect 2021
o Microsoft Certified: Azure Solutions Architect Expert 2020
o AWS Certified Solutions Architect - Associate 2020
✓ Oracle Certified Master, Java EE 6 Enterprise Architect (OCMEA) 2018

Github :
Web site:

Comments and Discussions

GeneralMy vote of 4 Pin
Stephen Wickland23-Sep-10 15:25
Stephen Wickland23-Sep-10 15:25 
GeneralIs it possible to call a window form from a c program Pin
jenica8128-Sep-09 23:30
jenica8128-Sep-09 23:30 
QuestionIs it possible to load Form from resource (.RC)? Pin
Behzad Ebrahimi25-Jul-08 19:53
Behzad Ebrahimi25-Jul-08 19:53 
GeneralMC++ Looks Like Delphi -.- Pin
redjes31-Aug-05 7:51
redjes31-Aug-05 7:51 
GeneralJust a small comment Pin
Unclickable button2-Feb-05 13:03
sussUnclickable button2-Feb-05 13:03 
GeneralCEdit (C++/MFC) Pin
Anonymous9-Apr-04 1:30
Anonymous9-Apr-04 1:30 
GeneralRe: just a question Pin
Selvam R21-Jan-04 14:03
professionalSelvam R21-Jan-04 14:03 
Questionhow to open a file? Pin
mitil2039048230427-Nov-03 19:05
mitil2039048230427-Nov-03 19:05 
GeneralByPassing Console Pin
Atif Mushtaq15-Aug-03 22:05
Atif Mushtaq15-Aug-03 22:05 
GeneralRe: ByPassing Console Pin
Selvam R2-Jan-04 4:50
professionalSelvam R2-Jan-04 4:50 
GeneralSugestion Pin
Brian Olej25-Jul-03 10:30
Brian Olej25-Jul-03 10:30 
GeneralRe: Sugestion Pin
Selvam R8-Aug-03 6:24
professionalSelvam R8-Aug-03 6:24 
GeneralRe: Sugestion Pin
Brian Olej9-Aug-03 6:35
Brian Olej9-Aug-03 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.