MessageBoxTest
// MessageBoxTest.cpp
#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;
#include "MyForm.h"
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// OK notice
MessageBox::Show( "Testing a notice", "MessageBox test 1",
MessageBoxButtons::OK, MessageBoxIcon::Exclamation );
// Yes No
String^ message = "Testing Yes or No\n Yes will start WinForm";
String^ caption = "MessageBox test 2";
MessageBoxButtons buttons = MessageBoxButtons::YesNo;
System::Windows::Forms::DialogResult result;
result = MessageBox::Show( message, caption, buttons );
if ( result == ::DialogResult::Yes ) {
Application::Run(gcnew MyForm()); // message loop
} else {
MessageBox::Show( "Testing just one string");
}
return 0;
}
cat MyForm.h
// MyForm.h
ref class MyForm : public Form
{
public:
MyForm();
Label ^m_label;
Button ^btn;
TextBox ^txt1;
void btn_Click(Object ^sender, System::EventArgs^ e);
void btn_Click2(Object ^sender, System::EventArgs^ e);
};
cat MyForm.cpp
// MyForm.cpp
#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;
#include "MyForm.h"
MyForm ::MyForm()
{
//set some form properties/fields
this->Text = "sample06";
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=gcnew Label();
m_label->Text="The quick brown fox jumps over the lazy dog.";
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=gcnew Button();
btn->Text="Test Button";
btn->Location=Point(5,90);
btn->Size=System::Drawing::Size(100,22);
btn->Click += gcnew EventHandler( this, &MyForm::btn_Click);
//add the button
this->Controls->Add(btn);
txt1=gcnew TextBox();
txt1->ReadOnly=true;
txt1->Size=System::Drawing::Size(200,22);
txt1->Location=Point(130,90);
//add the text box
this->Controls->Add(txt1);
// another button
btn=gcnew Button();
btn->Text="Test MessageBox";
btn->Location=Point(5,190);
btn->Size=System::Drawing::Size(200,22);
btn->Click += gcnew EventHandler( this, &MyForm::btn_Click2);
//add the button
this->Controls->Add(btn);
}
//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();
}
void MyForm::btn_Click2(Object ^sender, System::EventArgs ^e)
{
// test message box with its owner(this)
MessageBox::Show(this,"Test");
String^ message = "Yes will close the WinForm";
String^ caption = "MessageBox Test";
MessageBoxButtons buttons = MessageBoxButtons::YesNo;
MessageBoxIcon icon = MessageBoxIcon::Question;
MessageBoxDefaultButton def = MessageBoxDefaultButton::Button1;
MessageBoxOptions opt = MessageBoxOptions::RightAlign;
System::Windows::Forms::DialogResult result;
// Displays the MessageBox.
result = MessageBox::Show(this, message, caption, buttons,icon, def, opt
);
if (result == ::DialogResult::Yes) {
// Closes the parent form.
this->Close();
}
}
cat makefile
TARGET=MessageBoxTest
all: $(TARGET).exe
.PHONY: all test mmm clean
.SUFFIXES: .obj
LOPT=-ENTRY:main -SUBSYSTEM:WINDOWS
OBJS=$(TARGET).obj MyForm.obj
$(TARGET).obj: $(TARGET).cpp MyForm.h
MyForm.obj: MyForm.cpp MyForm.h
$(TARGET).exe: $(OBJS)
link $(LOPT) $(OBJS)
.cpp.obj:
cl -c -Wall -Od -clr $<
test: $(TARGET).exe
./$(TARGET).exe &
clean:
-rm $(TARGET).exe *.obj *~ $(TARGET).manifest
mmm:
cat $(TARGET).cpp
cat MyForm.h
cat MyForm.cpp
@echo
cat makefile
html:
cat /dev/clipboard | code2html.exe | unix2dos