fantasy2004
December 28th, 2005, 08:17 AM
hi all!
I want to use c++/cli to dynamically create a button in a sdi application.
I know how to create using mfc. but when i use c++/cli, i cant see the button.
who can give me some sample code?
I dont know where to find the sample code, the c++/cli help in vc2005 is too little.
thanks.
NoHero
December 28th, 2005, 11:57 AM
Here is an simple example which creates a button on a form and does a MessageBox when the button was pressed:
#pragma managed
#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
__gc class MyForm : public Form
{
private:
Button *myButton;
public:
MyForm ( void )
{
this->Size.Height = 100;
this->Size.Width = 250;
this->Text = S"my sample";
myButton = new Button;
myButton->Size.Height = 20;
myButton->Size.Width = 60;
myButton->Text = S"hello world!";
// register event handler
myButton->add_Click(new EventHandler(this, MyButton_OnClick));
this->Controls->Add(myButton);
}
protected:
void MyButton_OnClick ( Object *sender, EventArgs *arg )
{
MessageBox::Show(this, L"The button was pressed!");
}
};
int Main ( String *args __gc[] )
{
MyForm *form = new MyForm;
// run application
Application::Run(form);
return 0;
}