CppForm.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:8k
- // 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 CppForm : public Form {
- 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;
- System::Windows::Forms::ContextMenu* popupMenu;
- MenuItem* item_p1;
- MenuItem* item_p2;
- MenuItem* item_p3;
- 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:
- 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.
- MessageBox::Show(S"The About menu item", S"Menu");
- }
- else if (pSender == item2)
- {
- // Exit from the application.
- Application::Exit();
- }
- 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.
- theLabel->Font = new System::Drawing::Font(S"Verdana", 16, FontStyle::Italic);
- 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...");
- item2 = new MenuItem("E&xit");
- fileMenu->MenuItems->Add(item1);
- fileMenu->MenuItems->Add(item2);
- // Connect event handler to the menu items.
- item1->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;
- }