Click here to Skip to main content
6,597,576 members and growing! (19,139 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Windows Forms     Beginner

Managed C++ and Windows Forms

By Nishant Sivakumar

An introduction to Windows Forms using Managed C++
C++/CLI, Windows, .NET 1.0, Visual Studio, Dev
Posted:25 Oct 2001
Views:109,832
Bookmarked:33 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
40 votes for this article.
Popularity: 6.57 Rating: 4.10 out of 5

1

2

3
2 votes, 13.3%
4
13 votes, 86.7%
5

Introduction

One big complaint I have about Visual Studio .NET is that it does not support a GUI designer tool for Managed C++. Its not going to be easy to design a heavily-GUI oriented program in Managed C++. But then we can always try.  In this article I'll try and give an introduction to using Windows Forms in your Managed C++ applications.

Lets jump straight into our first program. Use the App Wizard to generate a basic Managed C++ application for you. I used the name sample01 for my project and so my main file is sample01.cpp. In your case this filename will depend on the name you chose for your project. Make the required changes to your cpp source so that you have a file looking like the listing below.

Program 1

#include "stdafx.h"


#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Windows::Forms;

//we derive a class from the Form class

//notice how we don't need any header files

__gc class MyForm : public Form
{
};

int __stdcall WinMain()
{
    //this will start a message loop for our form	

    Application::Run(new MyForm());
    return 0;
}

Well, as you can see, I have made several changes to the App wizard generated code. First I have added all those dll references and their corresponding namespace references. I have added a new managed class called MyForm which is derived from System::Windows::Forms::Form.

I have replaced wmain with WinMain so that we won't have to see that ugly console window behind our beautiful Form. I have prefixed it with __stdcall otherwise you'll see a warning. That's how the Win 32 API has defined WinMain(). Then we have used the static function Run of the Application class. This will start the standard application message loop.

Well, go ahead. Build and run it. You'll see your first Windows Form. Use Ctrl-F5 to run it. If you click on the play button the program will run in debug mode which is considerably slower.

Program 2

#include "stdafx.h"


#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Windows::Forms;

//we derive a class from the Form class

//notice how we don't need any header files

__gc class MyForm : public Form
{
public:
    MyForm();	
    Label *m_label;
};

int __stdcall WinMain()
{
    //this will start a message loop for our form

    Application::Run(new MyForm());
    return 0;
}

MyForm ::MyForm()
{
    //set some form properties/fields

    this->Text = "Elmer Fudd";
    this->FormBorderStyle=FormBorderStyle::Fixed3D;
    this->MaximizeBox=false;
    this->ClientSize=System::Drawing::Size(400,300);

    //create the Label object and set its properties/fields

    m_label=new Label();
    m_label->Text="Keep wewy wewy quiet. I am huntin wabbit";
    m_label->Size=System::Drawing::Size(300,50);
    m_label->Location=Point(5,5);	

    //add the label to the form

    this->Controls->Add(m_label);
}

Well, now we have made several changes more. We have added a Label member to our Form derived class and also a constructor. In the constructor we set some properties and fields of our form. We give it a title. We fix its border and make it non-sizable. We disable the maximize box. And also set its size.

We then create our Label object and set its text, size and location. Then we add the label to the form by adding it to the form's controls collection. Build and run it. You should see a more lively window now that, it has a title and some text on it.

Program 3

#include "stdafx.h"


#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Windows::Forms;

//we derive a class from the Form class

//notice how we don't need any header files

__gc class MyForm : public Form
{
public:
    MyForm();	
    Label *m_label;
    Button *btn;
    TextBox *txt1;
    void btn_Click(Object *sender, System::EventArgs* e);
};

int __stdcall WinMain()
{
    //this will start a message loop for our form

    Application::Run(new MyForm());
    return 0;
}

MyForm ::MyForm()
{
    //set some form properties/fields

    this->Text = "Elmer Fudd";
    this->FormBorderStyle=FormBorderStyle::Fixed3D;
    this->MaximizeBox=false;
    this->ClientSize=System::Drawing::Size(400,300);

    //create the Label object and set its properties/fields

    m_label=new Label();
    m_label->Text="Keep wewy wewy quiet. I am huntin wabbit";
    m_label->Size=System::Drawing::Size(300,50);
    m_label->Location=Point(5,5);	

    //add the label to the form

    this->Controls->Add(m_label);

    btn=new Button();
    btn->Text="Click me dude!";
    btn->Location=Point(5,90);
    btn->Size=System::Drawing::Size(100,22);
    btn->add_Click(new EventHandler(this,&MyForm::btn_Click));

    //add the button

    this->Controls->Add(btn);

    txt1=new TextBox();
    txt1->ReadOnly=true;
    txt1->Size=System::Drawing::Size(200,22);
    txt1->Location=Point(130,90);

    //add the text box

    this->Controls->Add(txt1);
}

//the event handler for button click

void MyForm::btn_Click(Object *sender, System::EventArgs *e)
{
    //we show the current date/time in the text box

    DateTime dt=System::DateTime::Now;
    txt1->Text=dt.ToString();
}

Well, now we have added a button, a text box and an event handler for the button. We have written our btn_Click member function using the expected parameters of the delegate that is associated with the click event. We use add_Click to add our event handler to the list of functions that get notified of the event. And as you can see, in the handler we have written code to put the current date and time into the text box.

Build and run the program. You'll see that each time you click the button, the text box gets updated with the current date and time. Easy, huh?

Conclusion

You can build more complex programs but the basic idea is the same. Event handling is done via virtual functions which you can override. But I still have a faint hope that when Microsoft finally releases the next version of VS.NET, they'll have a CodeDOM like gadget for Managed C++.

Thank You

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

About the Author

Nishant Sivakumar


Member
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com

Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.

Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.

Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.
Location: United States United States

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
GeneralHow does it work??? PinmemberMegidolaon22:21 23 Feb '09  
Generalcannot view C# inherited Form in designer having refernce to C+/CLI wrapper Pinmemberramki_mars2:02 24 Apr '08  
QuestionMEMORYY HOGGERRR PinmemberManni Singh1:36 20 Mar '07  
Generalsplash screen in managed extension C++ Pinmemberasim_moon22:15 20 Jun '04  
GeneralThanks PinmemberAlex Farber22:16 9 Apr '03  
GeneralRe: Thanks Pinmembertom763:00 14 Jan '04  
GeneralGreat PinmemberMatt Newman9:34 20 Nov '02  
Generaldll PinmemberMazdak23:21 14 Mar '02  
GeneralRe: dll PinmemberNish [BusterBoy]23:36 14 Mar '02  
GeneralWinForm [modified] PinmemberFazlul Kabir6:22 26 Oct '01  
GeneralRe: WinForm PinmemberNish [BusterBoy]6:26 26 Oct '01  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Oct 2001
Editor: Nishant Sivakumar
Copyright 2001 by Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project