Laboratorium 15
1. Cel ćwiczenia
Celem ćwiczenia jest zapoznanie się z obsługą C++ w środowisku Microsoft Visual C++.
2. Przykłady
Zad1
Przykład aplikacji logowanie, gdzie należy wpisać poprawny Login i Hasło aby dostać informację o wprowadzeniu prawidłowych danych. W przeciwnym wypadku dostaniemy komunikat iż wprowadzone przez nas dane są nieprawidłowe.
#pragma once
namespace logowanie {
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::Label^ label1;
protected:
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::TextBox^ login;
private: System::Windows::Forms::TextBox^ haslo;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Timer^ timer1;
private: System::Windows::Forms::ProgressBar^ progressBar1;
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
#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->components = (gcnew System::ComponentModel::Container());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->login = (gcnew System::Windows::Forms::TextBox());
this->haslo = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(12, 48);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(33, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"Login";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(12, 81);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(36, 13);
this->label2->TabIndex = 1;
this->label2->Text = L"Hasło";
//
// login
//
this->login->Location = System::Drawing::Point(53, 45);
this->login->Name = L"login";
this->login->Size = System::Drawing::Size(100, 20);
this->login->TabIndex = 2;
//
// haslo
//
this->haslo->Location = System::Drawing::Point(53, 78);
this->haslo->Name = L"haslo";
this->haslo->PasswordChar = '*';
this->haslo->Size = System::Drawing::Size(100, 20);
this->haslo->TabIndex = 3;
this->haslo->TextChanged += gcnew System::EventHandler(this, &Form1::textBox2_TextChanged);
//
// button1
//
this->button1->Location = System::Drawing::Point(68, 156);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 4;
this->button1->Text = L"Zaloguj";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(68, 185);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 5;
this->button2->Text = L"Koniec";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// timer1
//
this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
//
// progressBar1
//
this->progressBar1->Location = System::Drawing::Point(53, 127);
this->progressBar1->Name = L"progressBar1";
this->progressBar1->Size = System::Drawing::Size(100, 23);
this->progressBar1->TabIndex = 6;
this->progressBar1->Click += gcnew System::EventHandler(this, &Form1::progressBar1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(208, 242);
this->Controls->Add(this->progressBar1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->haslo);
this->Controls->Add(this->login);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::SizableToolWindow;
this->Name = L"Form1";
this->Text = L"Logowanie";
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 textBox2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
if(progressBar1->Value <=99) // Warunek z progress barem
{
this->progressBar1->Value++; // Dodajemy 1 do progress bara
}
else
{
this->timer1->Stop(); // Wyłącznie timera
if(login->Text == "Student" && haslo->Text == "student1") // Wybieramy login i hasło
{
MessageBox::Show("Dane są poprawne. Jestes zalogowany jako: "+login->Text); // Wyswietlenie komunikatu ze dane sa prawidlowe
progressBar1->Value = 0; // Zerowanie progress baru
}
else
{
MessageBox::Show("Login lub hasło są nieprawidłowe."); // Wyswietlenie komunikatu ze dane sa zle
progressBar1->Value = 0; // Zerowanie progress baru
login->Text = ""; // Wyzerowanie loginu(puste pole)
haslo->Text = ""; // Wyzerowanie hasła(puste pole)
}
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
this->timer1->Start(); // Włączamy timera
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
Close(); // Funkcja zamykająca program
}
private: System::Void progressBar1_Click(System::Object^ sender, System::EventArgs^ e) {
}
};
}
Zad2
Przykładowa aplikacja za zadanie ma wypisywać w polu tekstowym wyraz który został wpisany w pole Tekst z częstotliwością czasu jaki został ustawiony w polu Czas (przy czym wartości wpisana 3000 = 3 sekundy). Przycisk Start służy do uruchomienia działania aplikacji, Stop do zatrzymania a przycisk OK do ustawienia (akceptowania) wprowadzonego czasu.
Aby było widoczne działanie aplikacji, należy uruchomić edytor tekstu (np. notatnik) i po wystartowaniu aplikacji, ustawieniu czasu postawić kursor myszy na polu tekstowym edytora tekstu (kliknąć myszą w polu edytora).
#pragma once
namespace spamer {
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::Timer^ timer1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::Label^ Czas;
private: System::Windows::Forms::Label^ Tekst;
private: System::Windows::Forms::Button^ Start;
private: System::Windows::Forms::Button^ Stop;
private: System::Windows::Forms::Button^ Ustaw;
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
#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->components = (gcnew System::ComponentModel::Container());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->Czas = (gcnew System::Windows::Forms::Label());
this->Tekst = (gcnew System::Windows::Forms::Label());
this->Start = (gcnew System::Windows::Forms::Button());
this->Stop = (gcnew System::Windows::Forms::Button());
this->Ustaw = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// timer1
//
this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(61, 26);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 20);
this->textBox1->TabIndex = 0;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(61, 74);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(100, 20);
this->textBox2->TabIndex = 1;
//
// Czas
//
this->Czas->AutoSize = true;
this->Czas->Location = System::Drawing::Point(13, 32);
this->Czas->Name = L"Czas";
this->Czas->Size = System::Drawing::Size(30, 13);
this->Czas->TabIndex = 2;
this->Czas->Text = L"Czas";
//
// Tekst
//
this->Tekst->AutoSize = true;
this->Tekst->Location = System::Drawing::Point(13, 81);
this->Tekst->Name = L"Tekst";
this->Tekst->Size = System::Drawing::Size(34, 13);
this->Tekst->TabIndex = 3;
this->Tekst->Text = L"Tekst";
//
// Start
//
this->Start->Location = System::Drawing::Point(182, 26);
this->Start->Name = L"Start";
this->Start->Size = System::Drawing::Size(75, 23);
this->Start->TabIndex = 4;
this->Start->Text = L"Start";
this->Start->UseVisualStyleBackColor = true;
this->Start->Click += gcnew System::EventHandler(this, &Form1::Start_Click);
//
// Stop
//
this->Stop->Location = System::Drawing::Point(182, 71);
this->Stop->Name = L"Stop";
this->Stop->Size = System::Drawing::Size(75, 23);
this->Stop->TabIndex = 5;
this->Stop->Text = L"Stop";
this->Stop->UseVisualStyleBackColor = true;
this->Stop->Click += gcnew System::EventHandler(this, &Form1::Stop_Click);
//
// Ustaw
//
this->Ustaw->Location = System::Drawing::Point(71, 112);
this->Ustaw->Name = L"Ustaw";
this->Ustaw->Size = System::Drawing::Size(75, 23);
this->Ustaw->TabIndex = 6;
this->Ustaw->Text = L"OK";
this->Ustaw->UseVisualStyleBackColor = true;
this->Ustaw->Click += gcnew System::EventHandler(this, &Form1::Ustaw_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 158);
this->Controls->Add(this->Ustaw);
this->Controls->Add(this->Stop);
this->Controls->Add(this->Start);
this->Controls->Add(this->Tekst);
this->Controls->Add(this->Czas);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Name = L"Form1";
this->Text = L"Form1";
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 Start_Click(System::Object^ sender, System::EventArgs^ e)
{
timer1->Start(); // Włączamy timera
}
private: System::Void Stop_Click(System::Object^ sender, System::EventArgs^ e)
{
timer1->Stop(); // Wyłączamy timera
}
private: System::Void Ustaw_Click(System::Object^ sender, System::EventArgs^ e)
{
timer1->Interval = int::Parse(textBox1->Text); // Czas w ms
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
SendKeys::Send(textBox2->Text); // Wypisanie tekstu jaki ustawimy
SendKeys::Send("{ENTER}"); // Za każdym kliknięciem naciskaj "ENTER"
}
};
}
Zad3
Przykładowa aplikacja za zadanie ma mnożyć wpisane przez użytkownika wartości. Odpowiednie wartości pól przy odpowiednim wciśnięciu przycisku.
#pragma once
namespace funkcja {
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>
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:
/// <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->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(12, 19);
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(13, 50);
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(13, 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(53, 19);
this->Wpis1->Name = L"Wpis1";
this->Wpis1->Size = System::Drawing::Size(58, 20);
this->Wpis1->TabIndex = 3;
this->Wpis1->Text = L"1";
this->Wpis1->TextChanged += gcnew System::EventHandler(this, &Form1::Wpis1_TextChanged);
//
// Wpis2
//
this->Wpis2->Location = System::Drawing::Point(53, 50);
this->Wpis2->Name = L"Wpis2";
this->Wpis2->Size = System::Drawing::Size(58, 20);
this->Wpis2->TabIndex = 4;
this->Wpis2->Text = L"2";
this->Wpis2->TextChanged += gcnew System::EventHandler(this, &Form1::Wpis2_TextChanged);
//
// Wpis3
//
this->Wpis3->Location = System::Drawing::Point(53, 80);
this->Wpis3->Name = L"Wpis3";
this->Wpis3->Size = System::Drawing::Size(58, 20);
this->Wpis3->TabIndex = 5;
this->Wpis3->Text = L"3";
//
// button1
//
this->button1->Location = System::Drawing::Point(136, 19);
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(136, 50);
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(136, 80);
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(247, 130);
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 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);
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
mnozenie (Convert::ToInt16(Wpis2->Text), Convert::ToInt16(Wpis3->Text));
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
mnozenie (Convert::ToInt16(Wpis1->Text), Convert::ToInt16(Wpis3->Text));
}
private: System::Void Wpis1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void Wpis2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
};
}
3. Program ćwiczenia
Zad4
Należy wykonać aplikację która będzie wykonywać odpowiednie operacje przy wciśnięciu konkretnego przycisku. Czyli, jeden przycisk jest odpowiedzialny za operację dzielenia, drugi za operację dodawania a trzeci za operację odejmowania. Przy wyświetlanym wyniku należy dodać informację z jakieś operacji jest ten wynik. Aplikacja ma być również wyposażona w przycisk który będzie zamykał aplikację.
Poniżej zdjęcie aplikacji.
Zad5
Należy wykonać aplikację która będzie dodawała wartości heksadecymalne (szesnastkowe) i następnie konwertowało wynik do postaci dziesiętnej. Poniżej zdjęcie aplikacji.
1
Podstawy Informatyki