computers and programming the 10 th lecture jiří Šebesta

24
Computers and programming The 10 th lecture Jiří Šebesta

Upload: victor-jenkins

Post on 11-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Computers and programming The 10 th lecture Jiří Šebesta

Computers and programming

The 10th lecture

Jiří Šebesta

Page 2: Computers and programming The 10 th lecture Jiří Šebesta

TOPIC – programming in MSVS for Windows

1. Basic definitions 2. Form application 3. Examples

Page 3: Computers and programming The 10 th lecture Jiří Šebesta

Basic definitions (1/4)

• Project for Windows:

– header files • files xxx.h

– source code files • files xxx.c or xxx.cpp

– resources = above all graphic objects determined by set of properties and behavior

• files xxx.rc (xxx.ico)

Page 4: Computers and programming The 10 th lecture Jiří Šebesta

Basic definitions (2/4)

• Resources:– menus– shortcuts– bit rasters, icons, cursors– character strings– tool panels– dialog windows

• Dialog window:– fundamental objekt (each window is dialog window)– control objects in dialog window are again dialog windows with special properties– applied principle: parent’s vs. child’s dialogs

Page 5: Computers and programming The 10 th lecture Jiří Šebesta

Basic definitions (3/4)

• Dialog window (resp. object):– properties – variables define a visual characteristic and behavior of window (object) and events, i.e. functions called if an events in window (object) occurs, e.g. click by mouse

- window modality - modal window, cannot be leaved without closing (style attribute WS_VISIBLE is set) - unmodal window can be whenever leaved (defocused)

Page 6: Computers and programming The 10 th lecture Jiří Šebesta

Basic definitions (4/4)

• Fundamental sorts of Win applications:– using MFC (Microsoft Foundation Class Library)

• SDI (Single-document interface) – application using one document • MDI (Multiple-document interface) – application using more documents at the same time (e.g. MS Visual Studio is MDI application) • Dialog application – single dialog window for simple programs

– using standard resources from Windows

• Form application for Windows

Page 7: Computers and programming The 10 th lecture Jiří Šebesta

Form application (1/10)

• Form project establishment (MSVS 2008/10/12):

Page 8: Computers and programming The 10 th lecture Jiří Šebesta

Form application (2/10)

• Form creating (setting of properties + inserting of standard graphic objects to design Form1.h[design]):

Page 9: Computers and programming The 10 th lecture Jiří Šebesta

Form application (3/10)

this->ColorBox->BackColor = system::Drawing::Color::Transparent;this->ColorBox->Controls->Add(this->RB_blue);this->ColorBox->Controls->Add(this->RB_green);this->ColorBox->Controls->Add(this->RB_red);this->ColorBox->ForeColor =

system::Drawing::SystemColors::ControlText;this->ColorBox->Location = System::Drawing::Point(2, 86);this->ColorBox->Name = L"ColorBox";this->ColorBox->Size = System::Drawing::Size(88, 100);this->ColorBox->TabIndex = 1;this->ColorBox->TabStop = false;this->ColorBox->Text = L"Color";

• Automatically generated code for setting of graphic object properties in Form1.h :

• this is pointer to this form

Page 10: Computers and programming The 10 th lecture Jiří Šebesta

Form application (4/10)

• Function generation for event processing

…private: System::Void RB_blue_Click(System::Object^ sender, System::EventArgs^ e)

{this->My_text->ForeColor =

System::Drawing::Color::Blue;}

• in Form1.h a header of function for events is generated, required code can be written into the body of this function

Page 11: Computers and programming The 10 th lecture Jiří Šebesta

Form application (5/10)

• Function main() in Ex76.cpp

#include "stdafx.h"#include "Form1.h"using namespace Ex76;[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 itApplication::Run(gcnew Form1());return 0;

}

Code: Ex76

Page 12: Computers and programming The 10 th lecture Jiří Šebesta

Form application (6/10)

• Windows Form application in MSVS2013: new project

• A form application can not be established directly• It needs to insert an empty project of type CLR Empty Project with adequate name

Page 13: Computers and programming The 10 th lecture Jiří Šebesta

Form application (7/10)

• A form application required an adding UI – Windows Form with adequate name, e.g. MyForm.h or Form.h, by Project – Add (use right mouse button):

Page 14: Computers and programming The 10 th lecture Jiří Šebesta

Form application (8/10)

• Insert to MyForm.cpp following code:

#include "MyForm.h"using namespace System;using namespace System::Windows::Forms;

[STAThread]

void Main(array<System::String ^> ^args){

Application::EnableVisualStyles();Application::SetCompatibleTextRenderingDefault

(false);Ex76::MyForm form; Application::Run(%form);return 0;

}

• Modify code according to project name and form name

Page 15: Computers and programming The 10 th lecture Jiří Šebesta

Form application (9/10)

• Set in Project – Properties: Linker - System

Page 16: Computers and programming The 10 th lecture Jiří Šebesta

Form application (10/10)

• Set Linker – Advanced – Entry Point to name of starting function, e.g. Main

Page 17: Computers and programming The 10 th lecture Jiří Šebesta

Examples (1/7)

1) Create a form application for a simple calculator – adding, subtracting, multiplying and division of two rational numbers.

• Visual design of form

Page 18: Computers and programming The 10 th lecture Jiří Šebesta

double get_A(void){ return System::Convert::ToDouble(this->text_A->Text);}

double get_B(void){ return System::Convert::ToDouble(this->text_B->Text);}

Examples (2/7)

Function for text reading from the TextBox and conversion to double

conversion method calling

pointer to this form

object of form TextBox named

as text_B

variable of

TextBox

Page 19: Computers and programming The 10 th lecture Jiří Šebesta

private: System::Void bt_plus_Click(System::Object^ sender, System::EventArgs^ e) {

this->Res->Text = System::Convert::ToString(get_A()+get_B());

}…private: System::Void bt_div_Click(System::Object^ sender, System::EventArgs^ e) {

this->Res->Text = System::Convert::ToString(get_A()/get_B());

}

Examples (3/7)

Event processing – pressing of particular buttons

function of class System

pointer to this form

conversion method calling

function for inputs reading

callingCode: Ex77

Page 20: Computers and programming The 10 th lecture Jiří Šebesta

Examples (4/7)

2) Create an form application for simple database of computers (items: producer, price and memory capacity) with record up to 20 computers using dynamic access.

• Visual design of form

Page 21: Computers and programming The 10 th lecture Jiří Šebesta

#include <stdlib.h>#include <string.h>

typedef struct t_pc{

char prod[ 20]; // name of the producerint price; // price of the computerfloat mem; // RAM capacity in GB

} a_pc;void add(char* _prod, int _price, float _mem); // adding new computervoid sort(void); // sorting according to the pricet_pc* get_fwd(void); // point out to the next computert_pc* get_bwd(void); // point out to the prev. computerint show_price(void);// get price of an added pcint show_cheap(void);// get price of the cheapest pc

Examples (5/7)

Building-up of own function library computer.h

Page 22: Computers and programming The 10 th lecture Jiří Šebesta

#include "computer.h" // definition of the struct t_pc

t_pc *register[20]; // array of pointers to computersint index=0; // first free position in the katalogint ptr=index-1; // pointer to a pc displayed in edits

void add(char* _prod, int _price, float _mem){

t_pc *my_pc;my_pc = (t_pc*) malloc(sizeof(t_pc));strcpy(my_pc->prod, _prod);my_pc->price = _price;my_pc->mem = _mem;register[ptr=index++] = my_pc;

}

Examples (6/7)

Array of pointers to records declaration + solution example of the function add() in computer.cpp

Page 23: Computers and programming The 10 th lecture Jiří Šebesta

#pragma once#include "computer.h"using namespace System::Runtime::InteropServices;namespace Ex78 { ….

private: System::Void AddBtn_Click(System::Object^ sender, System::EventArgs^ e) {add((char*)Marshal::StringToHGlobalAnsi(ProdEdit->Text).ToPointer(), System::Convert::ToInt32(PriceEdit->Text), System::Convert::ToDouble(MemEdit->Text));ShowLbl->Text = System::Convert::ToString(show_price());}

Examples (7/7)

Adding the library pocitac.h and processing of event for pressing button Add in Form1.h

conversion method

VisualString => *char calling Code: Ex78

Page 24: Computers and programming The 10 th lecture Jiří Šebesta

TOPIC OF THE NEXT LECTURE

1. FINAL TEST

THANK YOU FOR YOUR ATTENTION