CppForm.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:14k
- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- #using <System.dll>
- #using <System.Windows.Forms.dll>
- #using <System.Drawing.dll>
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Windows::Forms;
- using namespace System::Drawing;
- __gc public class AboutBox : public Form {
- public:
- AboutBox()
- {
- // Set some form parameters.
- Text = S"About CppForm";
- FormBorderStyle = FormBorderStyle::Fixed3D;
- Size = System::Drawing::Size(300,150);
-
- // Add labels to the form.
- Label* label1 = new Label();
- label1->Text = "The CppForm Application";
- label1->Size = System::Drawing::Size(label1->PreferredWidth,
- label1->PreferredHeight);
- label1->Location = Point(20, 30);
- Label* label2 = new Label();
- label2->Text = "Julian Templeman, 2001";
- label2->Size = System::Drawing::Size(label1->PreferredWidth,
- label1->PreferredHeight);
- label2->Location = Point(20, 30+label1->PreferredHeight+10);
- // Add the labels to the form.
- Controls->Add(label1);
- Controls->Add(label2);
- // Add the OK button.
- OKButton = new Button();
- OKButton->Text = S"OK";
- OKButton->DialogResult = System::Windows::Forms::DialogResult::OK;
- OKButton->Size = System::Drawing::Size(40,25);
- OKButton->Location = Point(240,85);
- // Add the button to the form.
- Controls->Add(OKButton);
- }
- private:
- Button* OKButton;
- };
- __gc public class MyDialog : public Form {
- public:
- MyDialog()
- {
- // Set the form parameters
- Text = S"Personal Details";
- FormBorderStyle = FormBorderStyle::Fixed3D;
- Size = System::Drawing::Size(280,200);
- // Create the controls
- nameLabel = new Label();
- nameBox = new TextBox();
- phoneLabel = new Label();
- phoneBox = new TextBox();
- deptLabel = new Label();
- deptCombo = new ComboBox();
- OKBtn = new Button();
- CancelBtn = new Button();
-
- // nameLabel
- nameLabel->Location = Point(16, 24);
- nameLabel->Size = System::Drawing::Size(48, 16);
- nameLabel->Text = "Name:";
- nameLabel->TextAlign = ContentAlignment::MiddleRight;
- // nameBox
- nameBox->Location = Point(72, 24);
- nameBox->Size = System::Drawing::Size(152, 20);
- nameBox->Text = "";
- // phoneLabel
- phoneLabel->Location = Point(16, 56);
- phoneLabel->Size = System::Drawing::Size(48, 16);
- phoneLabel->Text = "Phone:";
- phoneLabel->TextAlign = ContentAlignment::MiddleRight;
- // phoneBox
- phoneBox->Location = Point(72, 56);
- phoneBox->Size = System::Drawing::Size(152, 20);
- phoneBox->Text = "";
- // deptLabel
- deptLabel->Location = Point(16, 88);
- deptLabel->Size = System::Drawing::Size(48, 16);
- deptLabel->Text = "Dept:";
- deptLabel->TextAlign = ContentAlignment::MiddleRight;
- // deptCombo
- deptCombo->DropDownWidth = 121;
- deptCombo->Location = Point(72, 88);
- deptCombo->Size = System::Drawing::Size(121, 21);
- deptCombo->Items->Add(S"IT");
- deptCombo->Items->Add(S"Admin");
- deptCombo->Items->Add(S"R&D");
- deptCombo->Items->Add(S"Catering");
- deptCombo->Items->Add(S"Manufacturing");
- // OKButton
- OKBtn->Location = Point(104, 136);
- OKBtn->Name = "OKButton";
- OKBtn->Text = "OK";
- OKBtn->DialogResult = DialogResult::OK;
- // CancelButton
- CancelBtn->Location = Point(192, 136);
- CancelBtn->Name = "CancelButton";
- CancelBtn->Text = "Cancel";
- CancelBtn->DialogResult = DialogResult::Cancel;
- // Add the controls
- Controls->Add(nameBox);
- Controls->Add(phoneBox);
- Controls->Add(nameLabel);
- Controls->Add(phoneLabel);
- Controls->Add(deptLabel);
- Controls->Add(deptCombo);
- Controls->Add(OKBtn);
- Controls->Add(CancelBtn);
- AcceptButton = OKBtn;
- CancelButton = CancelBtn;
- }
- public:
- void setName(String* theName) { nameBox->Text = theName; }
- String* getName() { return nameBox->Text; }
- void setPhone(String* thePhone) { phoneBox->Text = thePhone; }
- String* getPhone() { return phoneBox->Text; }
- void setDept(int theDept) { deptCombo->SelectedIndex = theDept; }
- int getDept() { return deptCombo->SelectedIndex; }
- private:
- Label* nameLabel;
- TextBox* nameBox;
- Label* phoneLabel;
- TextBox* phoneBox;
- Label* deptLabel;
- ComboBox* deptCombo;
- Button* OKBtn;
- Button* CancelBtn;
- };
- __gc public class CppForm : public Form {
- public:
- CppForm()
- {
- // Set the form caption.
- Text = S"Test Form";
- // Set the border style to 3D fixed.
- FormBorderStyle = FormBorderStyle::Fixed3D;
- // Set up controls on the form.
- Setup_Menu();
- Setup_Context_Menu();
- Setup_Buttons();
- Setup_Label();
- Setup_Group();
- Setup_Combo();
- Setup_Text();
- // Resize the form so it has room for a text box.
- Size = System::Drawing::Size(450, 300);
- }
- private:
- Button* btn1;
- Button* btn2;
- Label* theLabel;
- GroupBox* gbox;
- RadioButton* rb1;
- RadioButton* rb2;
- RadioButton* rb3;
- ComboBox* combo1;
- TextBox* text1;
- MainMenu* menuBar;
- MenuItem* fileMenu;
- MenuItem* item1;
- MenuItem* item2;
- MenuItem* item11;
- MenuItem* fontDlgItem;
- System::Windows::Forms::ContextMenu* popupMenu;
- MenuItem* item_p1;
- MenuItem* item_p2;
- MenuItem* item_p3;
- System::Drawing::Font* labelFont;
- private:
- void Btn1_Clicked(Object* pSender, EventArgs* pArgs)
- {
- MessageBox::Show(S"It worked!", S"Message...");
- }
- private:
- void Btn2_Clicked(Object* pSender, EventArgs* pArgs)
- {
- MessageBox::Show(S"It also worked!", S"Message...");
- }
- private:
- void Combo1_SelChanged(Object* pSender, EventArgs* pArgs)
- {
- if (pSender == combo1)
- {
- String* ps = String::Concat(S"New index is ",
- __box(combo1->SelectedIndex)->ToString());
- MessageBox::Show(ps, S"Index Change");
- }
- }
- private:
- void MenuItem_Clicked(Object* pSender, EventArgs* pArgs)
- {
- if (pSender == item1)
- {
- // Display the About box.
- AboutBox* box = new AboutBox();
- box->ShowDialog();
- }
- else if (pSender == item2)
- {
- // Exit from the application.
- Application::Exit();
- }
- else if (pSender == item11)
- {
- // Create the dialog
- MyDialog* box = new MyDialog();
- // Fill in the initial data
- box->setName(S"Joe Bloggs");
- box->setPhone(S"555-0155");
- box->setDept(1);
- // Show the dialog
- if (box->ShowDialog() == DialogResult::OK)
- {
- // If it came back with OK, display the name
- MessageBox::Show(box->getName(), S"Name was...");
- }
- }
- else if (pSender == fontDlgItem)
- {
- FontDialog* fd = new FontDialog();
- fd->Font = labelFont;
- if (fd->ShowDialog() == DialogResult::OK)
- {
- MessageBox::Show(fd->Font->Name, S"Name was...");
- labelFont = fd->Font;
- theLabel->Font = labelFont;
- }
- }
- else
- MessageBox::Show(S"Some other menu item", S"Menu");
- }
- private:
- void Radio_Clicked(Object* pSender, EventArgs* pArgs)
- {
- if (pSender == rb1)
- text1->Text = "You have selected the Visual Basic option.";
- else if (pSender == rb2)
- text1->Text = "You have selected the C# option.";
- else if (pSender == rb3)
- text1->Text = "You have selected the C++ option.rnrn"
- "Here is another line";
- }
- private:
- void Setup_Buttons()
- {
- // Add an OK button.
- btn1 = new Button();
- btn1->Text = S"OK";
- btn1->Size = System::Drawing::Size(70, 25);
- btn1->Location = Point(130, 225);
- btn1->Click += new EventHandler(this, &CppForm::Btn1_Clicked);
- Controls->Add(btn1);
- // Add a Cancel button.
- btn2 = new Button();
- btn2->Text = S"Cancel";
- btn2->Size = System::Drawing::Size(70, 25);
- btn2->Location = Point(210, 225);
- btn2->Click += new EventHandler(this, &CppForm::Btn2_Clicked);
- Controls->Add(btn2);
- }
- private:
- void Setup_Combo ()
- {
- // Setup combo box.
- combo1 = new ComboBox();
- combo1->DropDownStyle = ComboBoxStyle::DropDownList;
- combo1->Location = Point(20,180);
-
- // Set the width of the combo box based on the preferred width of the text
- // "Intermediate" (the longest string the combo's list will contain), plus
- // twenty pixels for the drop-down button at the right of the combo's text box.
- Label* l1 = new Label();
- l1->Text = S"Intermediate";
- combo1->Width = l1->PreferredWidth + 20;
-
- // Fill the combo box with items and set its default selection.
- combo1->Items->Add(S"Beginner");
- combo1->Items->Add(S"Intermediate");
- combo1->Items->Add(S"Advanced");
- combo1->SelectedIndex = 0;
- // Add the combo box to the form.
- Controls->Add(combo1);
- // Set the event handler for the combo box SelectedIndexChanged event.
- combo1->SelectedIndexChanged += new EventHandler(this, &CppForm::Combo1_SelChanged);
- }
- private:
- void Setup_Context_Menu ()
- {
- // Create the context menu.
- popupMenu = new System::Windows::Forms::ContextMenu();
- // Create the items in the context menu.
- item_p1 = new MenuItem("One");
- popupMenu->MenuItems->Add(item_p1);
- item_p2 = new MenuItem("Two");
- popupMenu->MenuItems->Add(item_p2);
- item_p3 = new MenuItem("Three");
- popupMenu->MenuItems->Add(item_p3);
- ContextMenu = popupMenu;
- }
- private:
- void Setup_Group ()
- {
- // Create the group box.
- gbox = new GroupBox();
- gbox->Text = S"Language";
- gbox->Location = Point(20, 60);
- // Create the radio buttons.
- rb1 = new RadioButton();
- rb1->Text = S"Visual Basic";
- rb1->Location = Point(10,15);
- rb2 = new RadioButton();
- rb2->Text = S"C#";
- rb2->Location = Point(10,40);
- rb3 = new RadioButton();
- rb3->Text = S"C++";
- rb3->Location = Point(10,65);
- rb3->Checked = true;
- // Add radio buttons to the group box.
- gbox->Controls->Add(rb1);
- gbox->Controls->Add(rb2);
- gbox->Controls->Add(rb3);
- // Add group box to the form.
- Controls->Add(gbox);
- // Connect event handler to the radio button controls.
- rb1->Click += new EventHandler(this, &CppForm::Radio_Clicked);
- rb2->Click += new EventHandler(this, &CppForm::Radio_Clicked);
- rb3->Click += new EventHandler(this, &CppForm::Radio_Clicked);
- }
- private:
- void Setup_Label()
- {
- theLabel = new Label();
- theLabel->AutoSize = true;
- theLabel->Text = S" Acme Consulting, Inc.";
- // Set the font.
- labelFont = new System::Drawing::Font(S"Verdana", 16, FontStyle::Italic);
- theLabel->Font = labelFont;
- theLabel->ForeColor = Color::Black;
- // Set location and size.
- theLabel->Location = Point(20,20);
- theLabel->Size = System::Drawing::Size(theLabel->PreferredWidth,
- theLabel->PreferredHeight);
- // Add an image.
- Bitmap* theImage = new Bitmap("SAVE.BMP");
- theLabel->Image = theImage;
- theLabel->ImageAlign = ContentAlignment::MiddleLeft;
- // Add the label to the form's controls.
- Controls->Add(theLabel);
- }
- private:
- void Setup_Menu()
- {
- // Create the main menu bar.
- menuBar = new MainMenu();
- // Create the File menu.
- fileMenu = new MenuItem("&File");
- menuBar->MenuItems->Add(fileMenu);
- // Create menu items and add them.
- item1 = new MenuItem("&About...");
- item11 = new MenuItem("&MyDialog...");
- fontDlgItem = new MenuItem("&Choose Font...");
- item2 = new MenuItem("E&xit");
- fileMenu->MenuItems->Add(item1);
- fileMenu->MenuItems->Add(item11);
- fileMenu->MenuItems->Add(fontDlgItem);
- fileMenu->MenuItems->Add(item2);
- // Connect event handler to the menu items.
- item1->Click += new EventHandler(this, &CppForm::MenuItem_Clicked);
- item11->Click += new EventHandler(this, &CppForm::MenuItem_Clicked);
- fontDlgItem->Click += new EventHandler(this, &CppForm::MenuItem_Clicked);
- item2->Click += new EventHandler(this, &CppForm::MenuItem_Clicked);
- // Add the main menu to the form.
- Menu = menuBar;
- }
- private:
- void Setup_Text()
- {
- text1 = new TextBox();
- text1->Location = Point(250,60);
- text1->Size = System::Drawing::Size(100,150);
- text1->Multiline = true;
- Controls->Add(text1);
- }
- };
- // This is the entry point for this application
- #ifdef _UNICODE
- int wmain(void)
- #else
- int main(void)
- #endif
- {
- Console::WriteLine(S"Forms Example");
- // Create a form.
- Application::Run(new CppForm());
- return 0;
- }