Laboratorium 13
1. Cel ćwiczenia
Celem ćwiczenia jest zapoznanie się z obsługą C++ w środowisku Microsoft Visual C++.
2. Przykłady
Zad1
Przykład programu który po kliknięciu przycisku tworzy dynamicznie kopie okna głównego.
#pragma once
namespace dynamiczne {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
int licznik = 1;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::Button^ button2;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Utwórz";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(108, 13);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 1;
this->button2->Text = L"Zamknij";
this->button2->UseVisualStyleBackColor = true;
this->button2->Visible = false;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(194, 48);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
this->MaximizeBox = false;
this->MinimizeBox = false;
this->Name = L"Form1";
this->ShowIcon = false;
this->Text = L"Głowne";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Form1 ^obiekt;
obiekt = gcnew Form1;
obiekt->ControlBox = false;
obiekt->button2->Visible = true;
obiekt->Text = "Okno nr " + Convert::ToString (licznik);
obiekt->Show();
licznik++;
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
Close();
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
Zad2
Po wciśnięciu przycisku ma się pojawić okno z wyborem: Tak, Nie, Anuluj. Po kliknięciu na dowolny przycisk ma się pojawić stosowny komunikat informujący nas które polecenie zaznaczyliśmy.
#pragma once
namespace okno_dialogowe {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(83, 146);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(110, 44);
this->button1->TabIndex = 0;
this->button1->Text = L"Przycisk";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(35, 42);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(225, 65);
this->textBox1->TabIndex = 1;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(290, 212);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Okno dialogowe";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
System::Windows::Forms::DialogResult odp =
MessageBox::Show("Wybierz opcję : ",
"Pytanie", MessageBoxButtons::YesNoCancel, MessageBoxIcon::Question);
switch (odp) {
case System::Windows::Forms::DialogResult::Yes :
textBox1->AppendText("Wybrano odpowiedź TAK" +
System::Environment::NewLine);
break;
case System::Windows::Forms::DialogResult::No :
textBox1->AppendText("Wybrano odpowiedź Nie" +
System::Environment::NewLine);
break;
case System::Windows::Forms::DialogResult::Cancel :
textBox1->AppendText("Wybrano odpowiedź Anuluj" +
System::Environment::NewLine);
break;
}
};
}
3. Program ćwiczenia
Zad3
Aplikacja w której po wciśnięciu przycisku Formularz uruchamia się okno do wprowadzania danych takich jak: Wiek, Imię, Nazwisko i Płeć. Po zaakceptowaniu wprowadzonych danych, dane te wyświetlają się w oknie głównym.
Poniżej ilustracja odzwierciedlająca pożądaną aplikację.
Zad4
Aplikacja notatnik, powinna posiadać Menu z podstawowymi poleceniami edytora tekstu które spełniają swoje funkcje, oraz Menu kontekstowe.
6
Podstawy Informatyki