Laboratorium 12
1. Cel ćwiczenia
Celem ćwiczenia jest zapoznanie się z obsługą C++ w środowisku Microsoft Visual C++.
2. Przykłady
Zad1
Przykład aplikacji która zawiera dwa przyciski. Jeden z przycisków za zadanie ma wyświetlić nowe okno z komunikatem „Dzień dobry”, drugi zamyka aplikację.
#pragma once
namespace test {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
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;
private: System::Windows::Forms::Button^ button2;
protected:
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(52, 117);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(147, 59);
this->button1->TabIndex = 0;
this->button1->Text = L"ZAKOŃCZ";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(52, 24);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(147, 62);
this->button2->TabIndex = 2;
this->button2->Text = L"TEST";
this->button2->UseVisualStyleBackColor = true;
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->BackgroundImageLayout = System::Windows::Forms::ImageLayout::None;
this->ClientSize = System::Drawing::Size(261, 219);
this->ControlBox = false;
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
this->Name = L"Form1";
this->Text = L"Pierwszy program";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Close();
}
private: System::Void domainUpDown1_SelectedItemChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
MessageBox::Show ("Dzień dobry");
}
};
}
Zad2
Przykładowa aplikacja za zadanie ma zapamiętać wprowadzenie dziesięciu wartości liczbowych, następnie po wciśnięciu odpowiedniego przycisku wyświetlenie wszystkich zapamiętanych wartości.
#pragma once
namespace zmienne_tablicowe {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
int liczby[10];
int licznik = 0;
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::Label^ label1;
protected:
private: System::Windows::Forms::Label^ wynik;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
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->label1 = (gcnew System::Windows::Forms::Label());
this->wynik = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(40, 22);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(53, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"Liczba nr.";
//
// wynik
//
this->wynik->AutoSize = true;
this->wynik->Location = System::Drawing::Point(99, 21);
this->wynik->Name = L"wynik";
this->wynik->Size = System::Drawing::Size(10, 13);
this->wynik->TabIndex = 1;
this->wynik->Text = L" ";
//
// button1
//
this->button1->Location = System::Drawing::Point(39, 86);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 2;
this->button1->Text = L"Zapamiętaj";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(12, 128);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(132, 23);
this->button2->TabIndex = 3;
this->button2->Text = L"Pokaż wszystkie liczby";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(34, 48);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 20);
this->textBox1->TabIndex = 4;
this->textBox1->Text = L"1";
this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(170, 191);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->wynik);
this->Controls->Add(this->label1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedToolWindow;
this->Name = L"Form1";
this->Text = L" Zmienne";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
for (int i=0;i<=9;i++)
{
liczby[i] = -1;
};
licznik = 0;
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
liczby[licznik] = Convert::ToInt16 (textBox1->Text);
licznik++;
if (licznik>9)
{
licznik = 0;
};
wynik->Text = Convert::ToString (licznik);
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
for (int i=0;i<=9;i++)
{
MessageBox::Show (Convert::ToString(liczby[i]),"Wartość liczby nr" +Convert::ToString(i));
}
}
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
};
}
Zad3
Przykładowa aplikacja za zadanie ma pomnożenie wpisanych przez użytkownika wartości liczbowych do pól tekstowych i zwróceniu ich iloczynu w nowym oknie po wciśnięciu odpowiedniego przycisku.
#pragma once
namespace Funkcje {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
long mnozenie(int par1,int par2)
{
long wynik;
wynik = par1*par2;
MessageBox::Show (Convert::ToString(wynik),"Wynik");
return (wynik);
};
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::Label^ label1;
protected:
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::TextBox^ wpis1;
private: System::Windows::Forms::TextBox^ wpis2;
private: System::Windows::Forms::TextBox^ wpis3;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->wpis1 = (gcnew System::Windows::Forms::TextBox());
this->wpis2 = (gcnew System::Windows::Forms::TextBox());
this->wpis3 = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(24, 13);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(23, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"A =";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(24, 44);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(23, 13);
this->label2->TabIndex = 1;
this->label2->Text = L"B =";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(24, 80);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(23, 13);
this->label3->TabIndex = 2;
this->label3->Text = L"C =";
//
// wpis1
//
this->wpis1->Location = System::Drawing::Point(70, 13);
this->wpis1->Name = L"wpis1";
this->wpis1->Size = System::Drawing::Size(100, 20);
this->wpis1->TabIndex = 3;
this->wpis1->Text = L"1";
//
// wpis2
//
this->wpis2->Location = System::Drawing::Point(70, 44);
this->wpis2->Name = L"wpis2";
this->wpis2->Size = System::Drawing::Size(100, 20);
this->wpis2->TabIndex = 4;
this->wpis2->Text = L"2";
//
// wpis3
//
this->wpis3->Location = System::Drawing::Point(70, 77);
this->wpis3->Name = L"wpis3";
this->wpis3->Size = System::Drawing::Size(100, 20);
this->wpis3->TabIndex = 5;
this->wpis3->Text = L"3";
//
// button1
//
this->button1->Location = System::Drawing::Point(27, 131);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 6;
this->button1->Text = L"A * B";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(27, 169);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 7;
this->button2->Text = L"B * C";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// button3
//
this->button3->Location = System::Drawing::Point(27, 208);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 23);
this->button3->TabIndex = 8;
this->button3->Text = L"A * C";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(186, 248);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->wpis3);
this->Controls->Add(this->wpis2);
this->Controls->Add(this->wpis1);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedToolWindow;
this->Name = L"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"Funkcja";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
mnozenie (Convert::ToInt16(wpis1->Text), Convert::ToInt16(wpis3->Text));
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
int zm_a;
int zm_b;
long zm_wynik;
zm_a = Convert::ToInt16 (wpis1->Text);
zm_b = Convert::ToInt16 (wpis2->Text);
zm_wynik = mnozenie (zm_a,zm_b);
MessageBox::Show (Convert::ToString(zm_wynik),"Wartość zwrócona przez funkcję");
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
mnozenie (Convert::ToInt16(wpis2->Text), Convert::ToInt16(wpis3->Text));
}
};
}
3. Program ćwiczenia
Zad4
Należy wykonać aplikację która będzie wyświetlać następujące komunikaty:
po najechaniu myszą na element Panel wyświetli tekst informujący o tym zdarzeniu
po kliknięciu myszą na element Panel wyświetli nowe okno z informacją o tym zdarzeniu
po najechaniu myszą na element textBox również wyświetli odpowiedni tekst
przy próbie zmiany rozmiaru okna głównego będzie informacja o tym zdarzeniu
Poniżej zdjęcie aplikacji.
Zad5
Należy wykonać aplikację kalkulator. Poniżej zdjęcie aplikacji.
1
Podstawy Informatyki