Notatki
Transkrypt
Notatki
Programowanie 2. Język C++. Wykład 6.
6.1 Obszary nazw .Net ................................................................................................................................... 1
6.2 Obszar nazw System::Windows::Forms.................................................................................................... 2
6.3 Klasa Form. Wybrane metody i property. .................................................................................................. 3
6.4 Zadarzenia klasy Control .......................................................................................................................... 5
6.5 Formatka z przyciskiem i polem tekstowym .............................................................................................. 7
6.6 Wątki. Formatka z ‘ProgressBar’em’ ......................................................................................................... 9
6.7 Dodatek. Formatka z przyciskiem i polem tekstowym w języku C#.......................................................... 12
6.1 Obszary nazw .Net
System jest głównym w hierarchii obszarem nazw, zdefiniowanym w bibliotece mscorlib.dll.
Nazwa
System::Collections
System::Configuration
System::Data
System::Deployment
System::Diagnostics
System::DirectoryServices
System::Drawing
System::EnterpriseServices
System::Globalization
System::IO
System::Management
System::Net
System::Reflection
System::Resources
System::Runtime::InteropServices
System::Runtime::Remoting
System::Runtime::Serialization
System::Security
System::Threading
System::Web
System::Windows::Forms
System::Xml
Microsoft::Win32
Opis
Zawiera typy ArrayList, BitArray Hashtable, Queue, SortedList,
Stack, inne bardziej specjalistyczne typy danych.
dynamiczna modyfikacja plików application.config, web.config.
Zawiera klasy ADO.NET.
Umożliwia programowe uaktualnienia aplikacji.
Deklaracja: #using <System.Deployment.dll>.
Umożliwia uzyskiwanie informacji o środowisku CLR.
Deklaracja: #using <System.Directoryservices.dll>.
Zawiera elementy graficzne GDI+.
Umożliwia dostęp do obiektów COM+.
Umożliwia dostęp do danych w różnych formatach,
specyficznych dla konkretnego języka, waluty, kalendarza.
Biblioteki wejścia/wyjścia, operacje na plikach.
U dostęp do narzędzi i informacji o działających aplikacjach,
urządzeniach.
Umożliwia tworzenie aplikacji .Net.
Umożliwia dynamiczne tworzenie typów danych.
Umożliwia zarządzanie zasobami.
Usługi systemowe i COM.
Umożliwia budowę aplikacji rozproszonych (DCOM).
serializacja i deserializacja obiektów, czyli konwersji obiektów a
różne typy danych (txt, binary, …).
Umożliwia wprowadzanie zabezpieczeń do kodu, działania
aplikacji (autoryzacja, identyfikacja, szyfrowanie, …).
Umożliwia tworzenie i zarządzanie wątkami.
Umożliwia budowę aplikacji w oparciu o usługę WWW.
Umożliwia budowę aplikacji okienkowych.
U zarządzanie danymi XML.
Umożliwia budowę aplikacji win32.
1
Programowanie 2. Język C++. Wykład 6.
6.2 Obszar nazw System::Windows::Forms
Nazwa
Application
AxHost
BorderStyle
Button
ButtonState
CheckBox
Clipboard
ColorDialog
ComboBox
Cursor
DataGrid
DateTimePicker
Form
HScrollBar
ImageList
Label
ListBox
ListView
Menu
MessageBox
Panel
ProgressBar
RadioButton
Splitter
StatusBar
TextBox
ToolBar
Opis
Provides static methods to manage applications, including starting and
stopping them, and getting information about them.
Provides a way to host ActiveX controls in Windows Forms applications.
An enumeration that specifies the border style for controls. BorderStyle has
three members: Fixed3D for three-dimensional borders, FixedSingle for simple
line borders, and None for no border.
Represents a Windows button control.
An enumeration that specifies the appearance of a button. Members include
Inactive, Pushed, and Normal.
Represents a Windows check box control.
Lets you place data onto and retrieve it from the system clipboard.
Displays a standard dialog box to let the user pick a color.
Represents a Windows combo box control.
Represents a cursor.
Displays ADO.NET data in a scrollable grid.
Windows date-time picker control.
Window or a dialog box.
Windows horizontal scroll bar control.
Collection of images typically used by toolbars.
Windows label control.
Windows list box control.
Displays a list of items in one of four views.
Represents a menu.
Displays a message box.
A control that can contain other controls.
Windows progress bar control.
Windows radio button control.
Provides splitter functionality to a window.
Windows status bar control.
Windows edit control.
Windows toolbar control.
Przykład 1. Obszary nazw użyte do tworzenia okna w szablonie CLR->Windows Forms Aplication.
using
using
using
using
using
using
namespace
namespace
namespace
namespace
namespace
namespace
System;
System::ComponentModel;
System::Collections;
System::Windows::Forms;
System::Data;
System::Drawing;
2
Programowanie 2. Język C++. Wykład 6.
6.3 Klasa Form. Wybrane metody i property.
Nazwa
AcceptButton
M - Metoda
P - Property
P
Activate
M
ActiveForm
P
AutoScale
P
AutoScroll
P
CancelButton
P
ClientSize
P
Close
DesktopLocation
FormBorderStyle
M
P
P
HelpButton
P
Icon
MaximizeBox
P
P
Menu
MinimizeBox
P
P
OwnedForms
SetDesktopLocation
ShowDialog
ShowInTaskBar
P
M
M
P
Size
SizeGripStyle
P
P
TopLevel
P
TopMost
P
WindowState
P
Opis
Gets or sets a reference to the button control that corresponds to the
user pressing Enter.
Activates the window, bringing it to the front of the application’s
collection of windows.
Gets the currently active form for this application, meaning the one at
the front of the application’s collection of windows.
Gets or sets a Boolean value indicating whether the form adjusts its
size to fit the height of the font used on the form and scales its controls
accordingly. The default is true.
Gets or sets a value indicating whether the form displays scroll bars
when controls fall outside the displayable area. The default is true.
Gets or sets a reference to the button control that corresponds to the
user pressing Esc.
Gets or sets the size of the client area of the form. (The client area is
the portion of the form that excludes the title bar and borders.)
Closes the form and frees any resources the form has used.
Gets or sets the location of the form on the Windows desktop.
Gets or sets the border style of the form. The default is
FormBorderStyle::Sizeable.
Gets or sets a Boolean value indicating if the form is to display a Help
button on the title bar. The default is false.
Gets or sets the icon associated with the form.
Gets or sets a Boolean value that indicates if the form is displaying a
Maximize box on the title bar. The default is true.
Gets or sets a reference to the menu that is displayed on this form.
Gets or sets a Boolean value that indicates if the form is displaying a
Minimize box on the title bar. The default is true.
Holds the collection of child forms owned by this form, if any.
Sets the location of the form on the desktop.
Shows the form as a modal dialog box.
Gets or sets a Boolean value, which is true if the form is to be shown in
the Windows taskbar. The default is true.
Gets or sets the form’s size.
Determines how (or even whether) the sizing grip is shown at the lower
right of the form. The default is SizeGripStyle::Hide.
Gets or sets a Boolean value, which is true if the form is a top-level
window, meaning that it has no parent other than the Windows
desktop. The default is true.
Gets or sets a Boolean value, which is true if the form is a topmost
window, meaning it is always displayed on top of other windows, even
when it doesn’t have the focus. The default is false.
Gets or sets the form’s window state, which determines how the form is
displayed—minimized, maximized, or normal. The default is
FormWindowState::Normal.
3
Programowanie 2. Język C++. Wykład 6.
Przykład 1. Metody, property, zdarzenia atrybuty klasy Form użyte do tworzenia okna w szablonie
CLR->Windows Forms Aplication.
// metody
this->SuspendLayout();
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->ClientSize = System::Drawing::Size(292, 270);
this->ResumeLayout(false);
// zdarzenie
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
// property
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
// atrybuty
this->Name = L"Form1";
this->Text = L"Form1";
Implementacja metody Form1_Load()przypisanej do zdarzenia Load.
Przed utworzeniem formatki pojawi się MessageBox z komunikatem ‘Form1’.
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
MessageBox::Show("Form1", "Okno przed formatka",
MessageBoxButtons::OK, MessageBoxIcon::Information);
}
4
Programowanie 2. Język C++. Wykład 6.
6.4 Zadarzenia klasy Control
Przkład 1. Wybrane zdarzenia klasy Control (Control Events).
Click
Zachodzi gdy kontrolka jest kliknięta.
Occurs when the control is clicked.
DoubleClick
Zachodzi gdy kontrolka jest podwójnie kliknięta.
Occurs when the control is double-clicked.
Enter
Zachodzi gdy kontrolka jest wybrana tabem.
Occurs when the control is entered.
GotFocus
Zachodzi gdy kontrolka jest wybrana.
Occurs when the control receives focus.
KeyDown
Zachodzi gdy przycisk klawiatury jest przyciskany i kontrolka jest wybrana.
Occurs when a key is pressed while the control has focus.
KeyPress
Zachodzi gdy przycisk klawiatury jest naciśnięty i kontrolka jest wybrana
Occurs when a key is pressed while the control has focus.
KeyUp
Zachodzi gdy przycisk klawiatury jest zwalniany i kontrolka jest wybrana.
Occurs when a key is released while the control has focus.
Leave
Zachodzi gdy tab opuszcza kontrolkę.
Occurs when the input focus leaves the control.
LostFocus
Zachodzi gdy kontrolkę jest opuszczana.
Occurs when the control loses focus.
MouseDown
Zachodzi gdy kursor myszy jest na kontrolce i naciśnięty jest przycisk myszy.
Occurs when the mouse pointer is over the control and a mouse button is pressed.
MouseEnter
Zachodzi gdy kursor myszy wchodzi na kontrolkę.
Occurs when the mouse pointer enters the control.
MouseHover
Zachodzi gdy kursor myszy zawieszony nad kontrolką.
Occurs when the mouse pointer hovers over the control.
MouseLeave
Zachodzi gdy kursor myszy wychodzi z kontrolki.
Occurs when the mouse pointer leaves the control.
MouseMove
Zachodzi gdy kursor myszy jest poruszany na kontrolce.
Occurs when the mouse pointer is moved over the control.
MouseUp
Zachodzi gdy kursor myszy jest na kontrolce i zwalniany jest przycisk myszy.
Occurs when the mouse pointer is over the control and a mouse button is released.
5
Programowanie 2. Język C++. Wykład 6.
Przykład. Zdarzenia Enter i Leave dla kontrolki TextBox.
Private:
void textBox1_Enter( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Jeżeli w TextBox wpisany jest tekst, zmień kolor tła
if ( textBox1->Text != String::Empty )
{
textBox1->ForeColor = Color::Red;
textBox1->BackColor = Color::Black;
// przesuń kursor na koniec tekstu
textBox1->Select(textBox1->Text->Length,0);
}
}
void textBox1_Leave( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Powróć do poprzednich kolorów i elementu z listy wyboru po przejściu
// tabem do innego obiektu
textBox1->ForeColor = Color::Black;
textBox1->BackColor = Color::White;
textBox1->Select(0,0);
}
W kodzie formatki należy dodać następujące
void InitializeComponent(void)
{
this-> textBox1_Enter +=
gcnew System::EventHandler(this, &Form1::textBox1_Enter);
this-> textBox1_Leave +=
gcnew System::EventHandler(this, &Form1::textBox1_Leave);
}
6
Programowanie 2. Język C++. Wykład 6.
6.5 Formatka z przyciskiem i polem tekstowym
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:
~Form1(){
if (components) { delete components;} }
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Label^ label1;
private:
System::ComponentModel::Container ^components;
7
Programowanie 2. Język C++. Wykład 6.
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
// button1
this->button1->Location = System::Drawing::Point(173, 119);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Pokaz tekst";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
// textBox1
this->textBox1->Location = System::Drawing::Point(38, 77);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(210, 20);
this->textBox1->TabIndex = 1;
// label1
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(38, 58);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(65, 13);
this->label1->TabIndex = 2;
this->label1->Text = L"Wpisz tekst ";
// 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->label1);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^
String^ tekst = textBox1->Text + "\n";
MessageBox::Show(tekst, "Wpisany tekst", MessageBoxButtons::OK,
MessageBoxIcon::Information);
}
};
}
e) {
8
Programowanie 2. Język C++. Wykład 6.
6.6 Wątki. Formatka z ‘ProgressBar’em’
Przykład pochodzi z książki
R. Wileczek, Visual C++ 2005 Express Edition. Tworzenie aplikacji dla Windows, Helion, 2006.
Etykiety dla ComboBox’ów, ProgressBar’ów należy wykonać na ćwiczeniach.
Plik Form1.h nowe elementy.
#pragma once
namespace AplikacjaWf {
public ref class Form1 : public System::Windows::Forms::Form
{
private:
int postep1, postep2;
public:
Form1(void)
{
InitializeComponent();
postep1 = postep2 = 0;
}
protected:
~Form1(){if (components){ delete components; }
}
private: System::ComponentModel::BackgroundWorker^ bgWorker1;
private: System::ComponentModel::BackgroundWorker^ bgWorker2;
private: System::Windows::Forms::ComboBox^ cbCzas1;
private: System::Windows::Forms::ComboBox^ cbCzas2;
private: System::Windows::Forms::CheckBox^ checkWatek1;
private: System::Windows::Forms::CheckBox^ checkWatek2;
private: System::Windows::Forms::ProgressBar^ progressWatek1;
private: System::Windows::Forms::ProgressBar^ progressWatek2;
private: System::Windows::Forms::ErrorProvider^ err;
private: System::ComponentModel::IContainer^ components;
private:
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->bgWorker1 = (gcnew System::ComponentModel::BackgroundWorker());
this->bgWorker2 = (gcnew System::ComponentModel::BackgroundWorker());
this->cbCzas1 = (gcnew System::Windows::Forms::ComboBox());
this->cbCzas2 = (gcnew System::Windows::Forms::ComboBox());
this->checkWatek1 = (gcnew System::Windows::Forms::CheckBox());
this->checkWatek2 = (gcnew System::Windows::Forms::CheckBox());
this->progressWatek1 = (gcnew System::Windows::Forms::ProgressBar());
this->progressWatek2 = (gcnew System::Windows::Forms::ProgressBar());
this->err = (gcnew System::Windows::Forms::ErrorProvider(this->components));
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->err))->BeginInit();
this->SuspendLayout();
9
Programowanie 2. Język C++. Wykład 6.
// bgWorker1
this->bgWorker1->WorkerReportsProgress = true;
this->bgWorker1->WorkerSupportsCancellation = true;
this->bgWorker1->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this,
&Form1::bgWorker1_DoWork);
// dodanie metody bgWorker1_ProgressChanged() do eventu ProgressChanged
this->bgWorker1->ProgressChanged += gcnew
System::ComponentModel::ProgressChangedEventHandler(this,Form1::bgWorker1_ProgressChanged);
// bgWorker2
// …
// cbCzas1
this->cbCzas1->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList;
this->cbCzas1->FormattingEnabled = true;
this->cbCzas1->Items->AddRange(gcnew cli::array< System::Object^ >(3)
{L"100", L"200", L"300"});
this->cbCzas1->Location = System::Drawing::Point(109, 6);
this->cbCzas1->Name = L"cbCzas1";
this->cbCzas1->Size = System::Drawing::Size(121, 21);
this->cbCzas1->TabIndex = 2;
// cbCzas2
// …
// checkWatek1
this->checkWatek1->AutoSize = true;
this->checkWatek1->Location = System::Drawing::Point(275, 8);
this->checkWatek1->Name = L"checkWatek1";
this->checkWatek1->Size = System::Drawing::Size(129, 17);
this->checkWatek1->TabIndex = 4;
this->checkWatek1->Text = L"Uruchomiony wątek 1";
this->checkWatek1->UseVisualStyleBackColor = true;
this->checkWatek1->CheckedChanged += gcnew System::EventHandler(this,
&Form1::checkWatek1_CheckedChanged);
// checkWatek2
// …
// progressWatek1
this->progressWatek1->Location = System::Drawing::Point(15, 84);
this->progressWatek1->Name = L"progressWatek1";
this->progressWatek1->Size = System::Drawing::Size(572, 23);
this->progressWatek1->Style = System::Windows::Forms::ProgressBarStyle::Continuous;
this->progressWatek1->TabIndex = 8;
// progressWatek2
// …
// err
this->err->ContainerControl = this;
// Form1
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(599, 162);
this->Controls->Add(this->progressWatek2);
this->Controls->Add(this->progressWatek1);
this->Controls->Add(this->checkWatek2);
this->Controls->Add(this->checkWatek1);
this->Controls->Add(this->cbCzas2);
this->Controls->Add(this->cbCzas1);
this->Name = L"Form1";
this->Text = L"Wątki";
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->err))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
}; // koniec ref class Form1
}
// koniec namespace AplikacjaWf
10
Programowanie 2. Język C++. Wykład 6.
Plik Form1.h - implementacje zdarzeń.
// uruchom, zatrzymaj watek 1
private: System::Void checkWatek1_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
{
err->Clear();
if (checkWatek1->Checked)
{
// CStringT::Trim, Trims all leading and trailing whitespace from the string.
if (cbCzas1->Text->Trim()->Equals(""))
{
checkWatek1->Checked = false;
err->SetError(cbCzas1, "Wybierz czas");
} else {
bgWorker1->RunWorkerAsync(System::Convert::ToInt32(cbCzas1->Text));
} else {
bgWorker1->CancelAsync(); }
}
}
// uruchom, zatrzymaj watek 2
private: System::Void checkWatek2_CheckedChanged(System::Object^
{
// …
}
sender, System::EventArgs^
// Worker 1
/ DoWorkEventArgs class, provides data for the DoWork event handler.
private: System::Void bgWorker1_DoWork(System::Object^ sender,
System::ComponentModel::DoWorkEventArgs^ e) {
BackgroundWorker^ worker = dynamic_cast<BackgroundWorker^>(sender);
while (!bgWorker1->CancellationPending)
{
worker->ReportProgress(postep1);
postep1++;
postep1 %= 100;
System::Threading::Thread::Sleep(safe_cast<System::Int32>(e->Argument));
}
}
private: System::Void bgWorker1_ProgressChanged(System::Object^
System::ComponentModel::ProgressChangedEventArgs^ e)
{
progressWatek1->Value = e->ProgressPercentage;
}
// Worker 2
private: System::Void bgWorker2_DoWork(System::Object^
System::ComponentModel::DoWorkEventArgs^ e)
{
// …
}
sender,
sender,
private: System::Void bgWorker2_ProgressChanged(System::Object^
System::ComponentModel::ProgressChangedEventArgs^ e)
{
// …
}
sender,
11
e)
Programowanie 2. Język C++. Wykład 6.
6.7 Dodatek. Formatka z przyciskiem i polem tekstowym w języku C#
Struktura i zawartość plików projektu
Plik: Form1.cs
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;
using
using
using
using
using
using
using
using
using
namespace AplikacjaWF
{
public partial class Formatka1 : Form
{
public Formatka1()
{
InitializeComponent();
}
}
}
12
Programowanie 2. Język C++. Wykład 6.
plik: Form1.Designer.cs
namespace AplikacjaWF
{
using System;
using System.Windows.Forms;
//using System.Drawing;
partial class Formatka1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#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>
private void InitializeComponent()
{
this.przycisk1 = new System.Windows.Forms.Button();
this.poleTekstowe1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// przycisk1
//
this.przycisk1.Location = new System.Drawing.Point(145, 165);
this.przycisk1.Name = "przycisk1";
this.przycisk1.Size = new System.Drawing.Size(104, 23);
this.przycisk1.TabIndex = 0;
this.przycisk1.Text = "Naciśnij mnie";
this.przycisk1.UseVisualStyleBackColor = true;
przycisk1.Click += new System.EventHandler(przycisk1_Click);
//
// poleTekstowe1
//
this.poleTekstowe1.Location = new System.Drawing.Point(40, 52);
this.poleTekstowe1.Name = "poleTekstowe1";
this.poleTekstowe1.Size = new System.Drawing.Size(209, 20);
this.poleTekstowe1.TabIndex = 1;
this.poleTekstowe1.Text = "Wpisz tekst";
//
// Formatka1
//
13
Programowanie 2. Język C++. Wykład 6.
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.poleTekstowe1);
this.Controls.Add(this.przycisk1);
this.Name = "Formatka1";
this.Text = "Formatka ";
this.ResumeLayout(false);
this.PerformLayout();
}
private void przycisk1_Click(object sender, EventArgs evArgs)
{
MessageBox.Show("Wpisałeś: '" + poleTekstowe1.Text + "'");
}
#endregion
private System.Windows.Forms.Button przycisk1;
private System.Windows.Forms.TextBox poleTekstowe1;
}
}
14