Programowanie 2. Język C++. Wykład 5. 1 5.1 Środowisko .Net

Transkrypt

Programowanie 2. Język C++. Wykład 5. 1 5.1 Środowisko .Net
Programowanie 2. Język C++. Wykład 5.
5.1 Środowisko .Net ........................................................................................................................................... 1
5.2 Biblioteki .Net................................................................................................................................................ 2
5.3 Rozszerzenie języka C++, standard C++/CLI ................................................................................................ 3
5.4 Delegacje ..................................................................................................................................................... 5
5.5 Zdarzenia ..................................................................................................................................................... 6
5.6 Szablon aplikacji CLR Windows Forms ......................................................................................................... 8
5.7 Formatka z przyciskiem .............................................................................................................................. 13
5.8 Formatka z paskiem narzędzi. .................................................................................................................... 15
5.1 Środowisko .Net
•
•
•
•
•
•
•
•
ASP.NET,
Windows Forms,
ADO.NET,
Windows Presentation Foundation,
Windows Communication Foundation,
Windows Workflow Foundation,
Windows CardSpace,
LINQ (Language-INtegrated Query).
Rys. 1. Struktura środowiska .Net 3.5.
S. Fraser, Pro Visual C++/CLI and the .Net 3.5 Platform, Apress, 2009.
1
Programowanie 2. Język C++. Wykład 5.
5.2 Biblioteki .Net
Przykładowe biblioteki słuŜące do budowania aplikacji okienkowych:
• Microsoft Foundation Classes (MFC),
• Windows Presentation Foundation (WPF),
• Windows Forms.
Szablony projektów Visual C++:
• Active Template Library (ATL), pozwala tworzyć aplikacje w oparciu o Component Object Model (COM).
•
Common Language Runtime (CLR)
o Windows Forms Application.
o Class Library.
o Windows Forms Control Library
•
General, standardowy projekt Visual Studio .NET.
•
Microsoft Foundation Classes (MFC), pozwala tworzyć aplikacje WIN32, aplikacje internetowe:
o MFC DLL, dynamic link library (DLL), pozwala tworzyć biblioteki dynamiczne korzystające z bibliotek MFC.
o MFC ActiveX Control, szablon projektu umoŜliwia tworzenie kontrolek ActiveX korzystających z bibliotek
MFC.
o MFC ISAPI Extension (Internet Services API), umoŜliwia tworzenie rozszerzeń serwerów WWW w formie
bibliotek dynamicznych korzystających z bibliotek MFC.
•
Extended stored procedure, umoŜliwia tworzenie procedur składowanych dla SQL Server’a.
• Win32 project, umoŜliwia tworzenie aplikacji Win32 API.
2
Programowanie 2. Język C++. Wykład 5.
5.3 Rozszerzenie języka C++, standard C++/CLI
C++/CLI - standard firmy Microsoft, rozszerzenie standardu ISO/IEC 14882:2003 języka C++.
W październiku 2003 powstała grupa zadaniowa TG5, pełna nazwa ECMA Technical Committee 39 (TC39) Task
Group 5 (TG5), w celu opracowania standardu C++/CLI.
Specyfikacja C++/CLI
Ecma/TC39-TG5/2004/25, C++/CLI Language Specification, Working Draft 1.5, Jun, 2004.
Standard C++/CLI opracowano jako element środowiska .Net.
Język C++ w standardzie C++/CLI rozszerzono o dodatkowe:
• słowa kluczowe
• typy klas
• wyjątki
• obszary nazw
• biblioteki
• mechanizm zarządzania pamięcią na stercie za pomocą ‘garbage collector’a’.
Przykład 1. Aplikacja konsolowa (Visual C++, szablon CLR).
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"program C++/CLI"); // unicode literal string
return 0;
}
Typy danych:
• typy wartościowe (zmienne)
o typy wbudowane,
o zmienne definiowalne,
o enumeratory,
• typy referencyjne
o uchwyty (handles),
o interfejsy,
o klasy referencyjne,
o tablice,
o obudowane zmienne (boxed value types).
Przykład 2. Definicje w standardzie C++/CLI.
handle – uchwyt, ‘obiekt referencyjny’. Dla dowolnego typu CLI T, T^ h; deklaruje uchwyt h typu T, h wskazuje na
obiekt zdefiniowany na stercie CLI (CLI heap).
Uchwyt moŜna ponownie przypisać (rebind).
Uchwyt musi wskazywać na cały obiekt.
heap, CLI - sterta CLI, obszar pamięci będący pod kontrolą ‘zbieracza śmieci (garbage collector’a)
‘Garbage collector’ jest elementem wirtualnej maszyny CLI ‘Virtual Execution System’.
Obiekty na stercie CLI definiuje się za pomocą słowa kluczowego gcnew.
heap, native – sterta, obszar pamięci w której obiekty alokowane są dynamicznie za pomocą polecenia new.
event – zdarzenie, element który umoŜliwia obiektowi lub klasie wysłanie notyfikacji.
3
Programowanie 2. Język C++. Wykład 5.
Składnia: uchwyt
typ_uchwytu ^ nazwa_uchwytu;
^ - ang. carat
Przykład 3. Definicja uchwytu typu typu A na stercie.
A ^a = gcnew A();
a->f();
a->i;
delete a;
Przykład 4. Sterta CLI, sterta, operatory new, gcnew.
Tworzy instancje klasy (obiekt) na stercie (CLI heap) i zwraca wskaźnik do
operator new
obiektu.
operator gcnew
Wprowadzony w .NET version 2.0, zastąpił operator new, tworzy instancję typu
referencyjnego na stercie i zwraca uchwyt do tej instancji.
Typy danych ref class, ref struct określają klasy i struktury typu referencyjnego, których instancje
definiowane są na stercie.
Obiekty ref class, ref struct są
• zarządzane przez ‘garbage collector’a,
• mogą posiadać properties (elementy słuŜące do wykonywania operacji na atrybutach klasy/struktury)
• nie mogą być w relacjach zaprzyjaźnienia
• muszą być w relacji dziedziczenia public.
Typy danych ref class, ref struct posiadają modyfikatory dostępu do swoich elementów:
private, public, protected, internal.
Typ internal określa dostęp dla elementów danego podzespołu.
Podzespół – zbiór plików określonych w manifeście podzespołu (MSDN).
Przykład 5. Definicja klasy ref, struktury ref.
ref class A {};
ref struct B{};
Przykład 6. Definicja zmienej typu string.
String^ s1 = "tekst";
Przykład 7. Definicja tablicy.
array<typDanych>^ nazwaTablicy;
array<String^>^ tabStr = gcnew array<String^>(7);
4
Programowanie 2. Język C++. Wykład 5.
5.4 Delegacje
Delegacja – jest klasą typy ref (ref class) której moŜna przypisać jedną (typ System::Delegate) lub wiele
metod (typ delegacji System::MulticastDelegate) a następnie wywołać te metody.
Delegacje są definiowane za pomocą słowa kluczowego delegate.
Przykład 1. Definicja delegacji i przypisanie jej adresu funkcji f1().
delegate void pDelegacja(String ^s);
pDelegacja ^pF = gcnew pDelegacja(&f1);
Przykład 2. Dodanie do delegacji funkcji f2(), usunięcie z delegacji funkcji f1().
pF = pF +
gcnew pDelegacja(&f2); // lub pF += gcnew pDelegacja(&f2);
pF = pF -
gcnew pDelegacja(&f1); // lub pF -= gcnew pDelegacja(&f1);
Przykład 3. UŜycie delegacji (aplikacja konsolowa typu CLR, cw05-01-delegacje.cpp)
#include "stdafx.h"
using namespace System;
delegate void pDelegacja(String ^s);
// deklaracja delegacji
void fG(String ^s){
// definicja funkcji globalnej
Console::WriteLine(s + " fG(String)");
}
ref class A {
public:
static void f1A(String ^s);
void f2A(String ^s);
void f3A(String ^s);
};
void A::f1A(System::String ^s){ Console::WriteLine(s + " f1A(String)"); }
void A::f2A(System::String ^s){ Console::WriteLine(s + " f2A(String)"); }
void A::f3A(System::String ^s){ Console::WriteLine(s + " f3A(String)"); }
void main(){
pDelegacja^ pFun;
// utworzenie instacji delegacji
pFun = gcnew pDelegacja(&fG);
// przypisanie pFun funkcji fG()
pFun += gcnew pDelegacja(&A::f1A); // dodanie do delegacji pFun funkcji f1A()
A ^a = gcnew A();
pFun = pFun + gcnew pDelegacja(a, &A::f2A);
pFun += gcnew pDelegacja(a, &A::f3A);
// dodanie do delegacji pFun funkcji f2A()
// dodanie do delegacji pFun funkcji f3A()
pFun->Invoke("tekst1");
// wywołanie delegacji
pFun = pFun - gcnew pDelegacja(&A::f1A); // usunięcie z delegacji pFun funkcji f1A()
pFun -= gcnew pDelegacja(a, &A::f3A);
// usunięcie z delegacji pFun funkcji f3A()
Console::WriteLine(" po usunieciu f1A(), f3A()");
pFun("tekst2");
// wywołanie delegacji
}
5
Programowanie 2. Język C++. Wykład 5.
5.5 Zdarzenia
event – zdarzenie, element który umoŜliwia obiektowi lub klasie wysłanie notyfikacji, Ecma/TC39TG5/2004/25.
Zdarzenie (event) jest uchwytem do typu delegacji poprzedzone modyfikatorem event.
Przykład 1. Definicja zdarzenia.
delegate void DelegacFun(String ^s);
ref class Event {
public:
event DelegacFun^ OnFun;
void Fun(String ^s)
{
};
OnFun(s);
}
Przykład 2. Dodanie funkcji do zdarzenia.
Event ^evnt;
evnt->OnFun += gcnew DelegacFun(this, &fun1);
evnt->OnFun += gcnew DelegacFun(this, &fun2);
Przykład 3. Wywołanie zdarzeń.
Event ^e = gcnew Event();
e->Fun("Text1");
6
Programowanie 2. Język C++. Wykład 5.
Przykład 3. Definicja zdarzeń (cw05-02-zdarzenia.cpp).
#include "stdafx.h"
using namespace System;
delegate void DelegacFun(String ^s); // deklaracja klasy typu delegate
ref class Event {
public:
event DelegacFun^ OnFun; // event musi być typu delegate
void Fun(String ^s)
{
OnFun(s);
}
};
ref class ZmienEvent {
Event ^evnt;
public:
ZmienEvent(Event ^e)
{
if (e == nullptr)
throw gcnew ArgumentNullException("blad");
evnt = e;
evnt->OnFun += gcnew DelegacFun(this, &ZmienEvent::fun1);
evnt->OnFun += gcnew DelegacFun(this, &ZmienEvent::fun2);
}
void UsunFun() {
evnt->OnFun -= gcnew DelegacFun(this, &ZmienEvent::fun2);
}
void fun1(String ^s) {
void fun2(String ^s){
Console::WriteLine(s + " fun1(String)"); }
Console::WriteLine(s + " fun2(String)"); }
};
void main(){
Event ^e = gcnew Event();
ZmienEvent ^devnt = gcnew ZmienEvent(e);
e->Fun("Text1");
// event onFun wywoluje fun1, fun2
devnt->UsunFun(); // usunięcie z onFun fun2
Console::WriteLine( "po devnt->UsunFun(); ");
e->Fun("Text2"); // event onFun wywoluje fun1
// delete devnt; // delete nie jest wymagane, CLR garbage collector usuwa obiekt
// delete e;
// delete nie jest wymagane, CLR garbage collector usuwa obiekt
}
7
Programowanie 2. Język C++. Wykład 5.
5.6 Szablon aplikacji CLR Windows Forms
Projekt: AplikacjaWf.
1. Struktura katalogowa projektu.
2. Wygląd okna projektu w Microsoft Visual Studio, plik Form1.h[Design].
8
Programowanie 2. Język C++. Wykład 5.
3. Lista typów kontrolek.
9
Programowanie 2. Język C++. Wykład 5.
5. Kontrolki Windows Forms
10
Programowanie 2. Język C++. Wykład 5.
6. Zawartość i rola plików projektu.
Plik stdafx.cpp
// stdafx.cpp : source file that includes just the standard includes
// AplikacjaWf.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
Plik stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
#pragma once
// TODO: reference additional headers your program requires here
Plik AplikacjaWf.cpp
// AplikacjaWf.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace AplikacjaWf;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
11
Programowanie 2. Język C++. Wykład 5.
Plik Form1.h
#pragma once
namespace AplikacjaWf {
using
using
using
using
using
using
namespace
namespace
namespace
namespace
namespace
namespace
// obszar nazw o nazwie AplikacjaWF
System;
System::ComponentModel;
System::Collections;
System::Windows::Forms;
System::Data;
System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form // klasa Form1 dziedziczy
{
// od klasy Form
public:
Form1(void) // konstruktor klasy Form1
{
InitializeComponent();
}
protected:
~Form1()
// destruktor klasy Form1
{
if (components)
{
delete components;
}
}
private:
System::ComponentModel::Container ^components; // uchwyt typu Container
#pragma region Windows Form Designer generated code
void InitializeComponent(void) // definicja metody
{
this->SuspendLayout(); // zwieszenie tworzenia okna, zmiana atrybutów
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 273);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false); // uaktualniene wyglądu okna
}
#pragma endregion
}; // koniec klasy Form1
}
// koniec obszaru nazw AplikacjaWf
12
Programowanie 2. Język C++. Wykład 5.
5.7 Formatka z przyciskiem
Przykład 1. Aplikacja widows forms. Formatka z przyciskiem o nazwie button1.
Po naciśnięciu przycisku pojawia się Messagebox typu OK.
Dodanie do formatki obiektu o nazwie button1.
Własności formatki Form1 i obiektu button1.
13
Programowanie 2. Język C++. Wykład 5.
Plik Form1.h
#pragma once
namespace AplikacjaWf {
using namespace
using namespace
using namespace
using namespace
using namespace
using namespace
System;
System::ComponentModel;
System::Collections;
System::Windows::Forms;
System::Data;
System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{ InitializeComponent(); }
protected:
delete components; } }
~Form1(){ if (components) {
private: System::Windows::Forms::Button^ button1;
private: System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
// button1
this->button1->Location = System::Drawing::Point(188, 215);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
// Form1
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e){}
}; // koniec klasy Form1
// koniec obszaru nazw AplikacjaWf
}
Plik Form1.h (nowe elementy)
private: System::Windows::Forms::Button^
button1;
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
// button1
this->button1->Location = System::Drawing::Point(188, 215);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
// Form1
this->Controls->Add(this->button1);
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
}
14
Programowanie 2. Język C++. Wykład 5.
Plik Form1.h. Implementacja zdarzenia button1_Click()
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e){
MessageBox::Show("Tekst w MsgBoxie",
"Nazwa MsgBoxa",
MessageBoxButtons::OK,
MessageBoxIcon::Information );
}
5.8 Formatka z paskiem narzędzi.
Przykład 2. Formatka z menu: Plik, Pomoc. Menu Plik, podmenu: Zakoncz.
Menu Pomoc, podmenu: O programie ….
Po wyborze polecenia ‘Zakoncz’ formatka zamyka się.
Po wyborze polecenia ‘O programie ….’ pojawia się message box OK. z informacją o progamie.
Plik Form1.h.
#pragma once
namespace AplikacjaWf {
using
using
using
using
using
using
namespace
namespace
namespace
namespace
namespace
namespace
System;
System::ComponentModel;
System::Collections;
System::Windows::Forms;
System::Data;
System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void){
InitializeComponent();
}
protected:
~Form1() { if (components) { delete components; } }
private:
private:
private:
private:
private:
private:
System::Windows::Forms::MenuStrip^ menuGlowne;
System::Windows::Forms::ToolStripMenuItem^ plikToolStripMenuItem;
System::Windows::Forms::ToolStripMenuItem^ zakonczToolStripMenuItem;
System::Windows::Forms::ToolStripMenuItem^ pomocToolStripMenuItem;
System::Windows::Forms::ToolStripMenuItem^ oProgramieToolStripMenuItem;
System::Windows::Forms::ToolStrip^ pasekNarzedziowy;
System::ComponentModel::Container ^components;
15
Programowanie 2. Język C++. Wykład 5.
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->menuGlowne = (gcnew System::Windows::Forms::MenuStrip());
this->plikToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->zakonczToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->pomocToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->oProgramieToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->menuGlowne->SuspendLayout();
this->SuspendLayout();
// menuGlowne
this->menuGlowne->Items->AddRange(
gcnew cli::array<System::Windows::Forms::ToolStripItem^ >(2) {
this->plikToolStripMenuItem,
this->pomocToolStripMenuItem});
this->menuGlowne->Location = System::Drawing::Point(0, 0);
this->menuGlowne->Name = L"menuGlowne";
this->menuGlowne->Size = System::Drawing::Size(292, 24);
this->menuGlowne->TabIndex = 0;
this->menuGlowne->Text = L"menuStrip1";
// plikToolStripMenuItem
this->plikToolStripMenuItem->DropDownItems->AddRange(
gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->zakonczToolStripMenuItem});
this->plikToolStripMenuItem->Name = L"plikToolStripMenuItem";
this->plikToolStripMenuItem->Size = System::Drawing::Size(34, 20);
this->plikToolStripMenuItem->Text = L"&Plik";
// zakonczToolStripMenuItem
this->zakonczToolStripMenuItem->Name = L"zakonczToolStripMenuItem";
this->zakonczToolStripMenuItem->Size = System::Drawing::Size(113, 22);
this->zakonczToolStripMenuItem->Text = L"Zakoncz";
this->zakonczToolStripMenuItem->Click += gcnew System::EventHandler(
this, &Form1::zakonczToolStripMenuItem_Click);
// pomocToolStripMenuItem
this->pomocToolStripMenuItem->DropDownItems->AddRange(
gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {
this->oProgramieToolStripMenuItem});
this->pomocToolStripMenuItem->Name = L"pomocToolStripMenuItem";
this->pomocToolStripMenuItem->Size = System::Drawing::Size(50, 20);
this->pomocToolStripMenuItem->Text = L"Po&moc";
// oProgramieToolStripMenuItem
this->oProgramieToolStripMenuItem->Name = L"oProgramieToolStripMenuItem";
this->oProgramieToolStripMenuItem->Size = System::Drawing::Size(152, 22);
this->oProgramieToolStripMenuItem->Text = L"O programi&e...";
this->oProgramieToolStripMenuItem->Click += gcnew System::EventHandler(
this, &Form1::oProgramieToolStripMenuItem_Click);
// Form1
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->pasekNarzedziowy);
this->Controls->Add(this->menuGlowne);
this->MainMenuStrip = this->menuGlowne;
this->Name = L"Form1";
this->Text = L"Form1";
this->menuGlowne->ResumeLayout(false);
this->menuGlowne->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void zakonczToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
Close();
}
private: System::Void oProgramieToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
MessageBox::Show("Pomocy !!!!", "Okno Pomocy", MessageBoxButtons::OK, MessageBoxIcon::Information);
}
};
}
}
16

Podobne dokumenty