|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionOne 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 I have replaced 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 We then create our 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 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? ConclusionYou 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
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||