COS220 Concepts of PLs AUBG, COS dept Lecture 30aa Alternate Ways of Developing Windows Based...
-
Upload
frank-barber -
Category
Documents
-
view
219 -
download
3
Transcript of COS220 Concepts of PLs AUBG, COS dept Lecture 30aa Alternate Ways of Developing Windows Based...
COS220 Concepts of PLs AUBG, COS dept
Lecture 30aa
Alternate Ways of Developing Windows Based Applications
Reference: MS Visual C++,Deitel, Chapter 2.7
Lecture Contents
• Applications classified:– Console applications– Windows based applications– Internet/Web based applications
• Alternate ways to Build Windows based applications:– Using API programming model– Using MFC programming model– Using FCL /Framework Class Library/ and Win Forms – Using WPF /Windows Presentation Foundation/
3
Computer programs classified
• When you begin creating a new program, you must choose the type of application you will be developing
• Based on your choice, the IDE provides– the classes,– the tools,– the features
• required for the selected type of application
4
Types of Visual Basic 2010 Applications,
GShally,pp25
• VB operates 5 major types of apps:–Windows applications–Mobile applications–Web site applications–Office applications–Database applications
5
Types of Visual Basic 2010 Apps
• Windows application– Program will run on a computer or other
device that supports the Windows GUI
• Mobile application– Designed to run on mobile devices as
smartphones, Pocket PCs and computers running the Windows CE OS or Windows Phone OS
6
Types of Visual Basic 2010 Apps• Web site application
– Uses ASP.NET. Runs on a Web server. Produces HTML code that is downloaded to the client computer where the browser interprets HTML and displays the contents of a Web page
• Office application– Includes writing VBasic code to automate and
manipulate documents created using Microsoft Office 2003/2007/2010
• Database application– Written using ADO.NET to reference, access,
display, and update data stored in a database
7
Types of Visual Basic 2010 Apps
• Other types of applications include: – Console applications– Classes for class libraries– Certain controls to use in Windows
applications– Web services– Device-specific applications
04/19/23 Assoc. Prof. Stoyan Bonev 8
Types of C++ Apps• Application types you can create with C++
– Console applications– Windows applications– Web applications – run on a Web server when a
particular URL is loaded, and send HTML to a waiting browser.
– Services – applications that run in the background and respond to requests, but do not have any visual interface
– Class libraries – collections of code that can be called by other code.
– Other
9
Types of C# Apps• Applications Developed with C#,
– Web applications
– Windows (GUI) applications
– Console applications
– Class libraries and stand-alone components (.dlls)
– Smart device applications
– Services
10C# Programming: From Problem Analysis to Program Design 10
Types of Java Apps• Java programs may be applications and
applets(servlets).
• Applications are standalone programs, similar to .NET Console applications and/or Windows based GUI applications.
• Applets are similar to applications, but they do not run as standalone programs. Instead, applets adhere to a set of conventions that lets them run within a Java-compatible browser (client-side). You can only run an applet from an HTML page.
• Servlets – applets that run on a server.
11
Computer programs classifiedSummary
•Apps with Visual Interface
•Apps with No Visual Interface
12
Computer programs classifiedSummary
• Apps with Visual Interface– Console apps
• Input/Output data displayed in MSDOS window
– Windowed Dlg Box apps (modified console apps)• Using dialog boxes for I/O
– Windows based apps• Using visual components: labels, text fields, buttons
etc
– Web site apps
13
Computer programs classifiedSummary
• Apps with No Visual Interface that run in the background and respond to requests– Class libraries
• Collections of code that can be called by other code
– Windows services• No UI, Runs independently of any other user
app• Starts automatically when the computer starts
– Web services• No UI, It is code for other apps to call
14
.
FromConsole apps
ToWindows GUI apps
1515Java Programming: From Problem Analysis to Program Design, 4e
INTRO to Graphical User Interface (GUI) Components
• Drawbacks/disadvantages of– Console applications– Dialog boxes based windowed
applications• Suppose, the user/developer wants the
program to display all input data and output data in one pane or window or dialog box
• Such a dialog box is known as GUI or only UI
1616Java Programming: From Problem Analysis to Program Design, 4e
Graphical User Interface (GUI) Components
• Advantages of using GUI:– View inputs and outputs
simultaneously – One graphical window– Input values in any order– Change input values in window – Click on buttons to get output
1717Java Programming: From Problem Analysis to Program Design, 4e
Graphical User Interface (GUI) Components
• Terminology:• Frame/Form
– Title bar• System supported icons• Title
– Content pane• Components – labels, text fields, buttons
etc
18
Win API
Assoc. Prof. Stoyan Bonev 1919
IntroductionIntroduction
• The Windows Operating System introduced changes in the structure, implementation and execution of the computer programs.
• In line with the console applications, programs oriented towards Windows and Internet were developed.
Assoc. Prof. Stoyan Bonev 2020
Windows based Application – structure Windows based Application – structure and source code:and source code:
• A C++ program is composed to include:– definitions of global variables and– definitions of functions, one of which is the main
function titled main().
• The minimal console application does not include executable statements and is presented by sample source codes as on the next slide:
Assoc. Prof. Stoyan Bonev 2121
Windows based Application – structure Windows based Application – structure and source code:and source code:
• Minimal console app:•int main(){ return 0; }
• More minimal console application:•void main() {return; }
• Most minimal console application:•void main() { }
Assoc. Prof. Stoyan Bonev 2222
Windows based Application – structure Windows based Application – structure and source code:and source code:
• The execution of a Windows program doesn't start in main--it starts in WinMain
• Following the console apps analogy one may expect that
• The minimal Windows Application is expected to be presented by one of the presented source codes:–int WinMain() { return 0; }–void WinMain(){ return; }–void WinMain(){ }
Assoc. Prof. Stoyan Bonev 23
Minimal Windows API application
The analogy with console applications dictates the same style
int WinMain( )
{
return 0;
}
But real story is quite different . .> next slide
Assoc. Prof. Stoyan Bonev 24
Minimal Windows API applicationIncludes two non-empty functions. look at next two slides:
int WinMain( . . . )
{
. . .
}
int WndProc( . . . )
{
. . .
}
Assoc. Prof. Stoyan Bonev 25
Minimal Windows API application
int WinMain( . . . ) { WndClass wc;RegisterClass(&wc);CreateWindow(wc);ShowWindow(wc);UpdateWindow(wc);while ( GetMessage( … ) != NULL )
{ TranslateMessage( ); DispatchMessage( );}
}
Assoc. Prof. Stoyan Bonev 26
Minimal Windows API application int WndProc(HWND hWnd, UINT message,...) { switch (message) {
case WM_PAINT: /* stmts */case WM_LBUTTONUP: . . .
case WM_RBUTTONDOWN:. . .
case WM_CLOSE: . . .case WM_QUIT: . . .
};}
Assoc. Prof. Stoyan Bonev 2727
Windows based Application – structure Windows based Application – structure and source code:and source code:
• No, there is no analogy btw console app & Windows based app.
• Windows Application totally differs. • It has different philosophy and is based on
different principles like• Visual programming,• Component programming and• Event-driven programming
Assoc. Prof. Stoyan Bonev 2828
Windows based Application – structure Windows based Application – structure and source code:and source code:
• Win app need an entity to locate its components (labels, text boxes, buttons etc)
• Such an entity has to be visualized.• Win app needs a mechanism to respond to
user actions, known as events.
• To answer these requirements, Win app may not be empty, as the console app is.
Assoc. Prof. Stoyan Bonev 2929
Windows based Application – structure Windows based Application – structure and source code:and source code:
• Win app need at least two program units:• One that creates a structure for the
form/window and visualizes it• One that contains event handlers, i.e. User
specified code to run as response to events/messages/ sent to the Win app by the user or by the OS.
Assoc. Prof. Stoyan Bonev 3030
Windows based Application – structure Windows based Application – structure and source code:and source code:
Assoc. Prof. Stoyan Bonev 3131
Windows based Application – structure Windows based Application – structure and source code:and source code:
• As it was alredy discussed, Windows Application has a totally different composition.
• It is structurally organized to include two components:
• Source code component (.cpp file)• Resource component (.rc file)
Assoc. Prof. Stoyan Bonev 3232
Source code componentSource code component• The .cpp file contains at least 2 functions:
– WinMain()• To register, create, update, show app window
• To use a loop to distribute events for processing at run time
– WndProc()• To run code as response to fixed events at run time
Assoc. Prof. Stoyan Bonev 3333
Resource componentResource component• An additional feature of a Windows
Application is the resource component structured as contents of a resource file.
• Resource is any element of the user interface like icons, mouse pointers, menus etc.
• Comprised in a separate resource file, compiled by a resource compiler and is bound to the executable file.
Assoc. Prof. Stoyan Bonev 34
• C++ source C++ compiler Object code Linker Executable file
• Applic.cpp Applic.obj Applic.exe
Assoc. Prof. Stoyan Bonev 35
• C++ source C++ compiler Object code Linker Executable file
• Applic.cpp Applic.obj
Applic.exe
• Applic.rc Applic.res
• Resource RCcompiler Resource RCcompiler
description in binary
Assoc. Prof. Stoyan Bonev 36
How to build standard (unmanaged) .exe for console application?
• Processing stages:– Compiling– Linking
Assoc. Prof. Stoyan Bonev 37
• C++ source C++ compiler Object code Linker Executable file
• Applic.cpp Applic.obj Applic.exe
Assoc. Prof. Stoyan Bonev 38
How to build .exe for Windows based application?
• Processing stages:– Compiling– Compiling resources– Linking – Binding resources
Assoc. Prof. Stoyan Bonev 39
• C++ source C++ compiler Object code Linker Executable file
• Applic.cpp Applic.obj
Applic.exe
• Applic.rc Applic.res
• Resource RCcompiler Resource RCcompiler
description in binary
Assoc. Prof. Stoyan Bonev 40
!
• The presentation demonstrates the API programming model through two versions of an application:
– Standard– Minimal
Assoc. Prof. Stoyan Bonev 41
• My understanding of a Standard application:
– Standard – represents a window form with attributes and resources generated automatically by the IDE without any developer interference.
Assoc. Prof. Stoyan Bonev 42
• My understanding of a Minimal application:
– Minimal – represents a window form free of resources, displayed as an empty window with a greeting message “Hello, AUBG!”.
Assoc. Prof. Stoyan Bonev 43
The difference btw
debug and release version
Assoc. Prof. Stoyan Bonev 44
Comparative Efforts Analysis
• API Model:– Advantages
• Minimal program size;
• No need of extra libraries, except Windows dll’s.
– Disadvantages• Huge number of API base functions.
Assoc. Prof. Stoyan Bonev 45
Comparative Efforts Analysis
• MFC and .NET Model:– Advantages
• No need to know the set of API base functions;
• User friendly techniques for developing applications.
– Disadvantages• Greater size of the executable file;
• Need for additional class libraries (dll or static).
Assoc. Prof. Stoyan Bonev 46
Applications based on
A P Iprogramming model
Assoc. Prof. Stoyan Bonev 47
Building Windows Application using Windows API program model
• Select:–Project type Visual C++ >> Win32–Project template Win32 Project:
Assoc. Prof. Stoyan Bonev 48
Building Windows Application using Windows API program model
• Select:
– Standard application settings
• Build:– Debug application version
– Release application version
• How?
Build > Configur Manager > select Dbg/Rlse
• Compare:
– The .exe file sizes
Assoc. Prof. Stoyan Bonev 49
Building Windows Application using Windows API program model
• The standard API application window view:
Assoc. Prof. Stoyan Bonev 50
Building Windows Application using Windows API program model
• The minimal API application window view:
51
MFC
Assoc. Prof. Stoyan Bonev 5252
Examples using different techniques:Examples using different techniques:
• Example of a Windows Application using Windows MFC programming model.
standard (Dynamic) minimal (Dynamic)
standard (Static) minimal (Static)
Assoc. Prof. Stoyan Bonev 53
The difference btw statically linked and dynamically linked
application
Assoc. Prof. Stoyan Bonev 54
Applications based on
M F Cprogramming model
Assoc. Prof. Stoyan Bonev 55
Building Windows Application using Windows MFC program model
• Select:–Project type Visual C++ >> MFC–Project template MFC Application:
Assoc. Prof. Stoyan Bonev 56
Building Windows Application using Windows MFC program model
• Select:–Standard application settings:
• SDI as alternate to MDI or DIALOG based• MFC with Shared DLL• MFC with Static Lib
• Build:– Debug application version– Release application version
• Compare:–The .exe file sizes
Assoc. Prof. Stoyan Bonev 57
Building Windows Application using Windows MFC program model
◦The standard MFC application window view:
Assoc. Prof. Stoyan Bonev 58
Building Windows Application using Windows MFC program model
◦The minimal MFC application window view:
04/19/23 assoc. prof. Stoyan Bonev 59
OOP DOTNET FCL.NET & FCL
C++,Visual C++
andManaged C++
04/19/23 Assoc. Prof. Stoyan Bonev 61
C++, Visual C++ and Managed C++
• The Managed Extensions to C++
• Developers are offered a series of extension keywords, also known as Managed Extensions to C++, to generate code that targets the .NET Framework, also called managed code.
__gc
__value
__property
04/19/23 Assoc. Prof. Stoyan Bonev 62
Applications that run directly under Windows
• When you develop an application using Visual C++ .NET it will be either– managed application or– unmanaged application.
• In either case, it will be built to run on Windows machine.
• An unmanaged application runs directly under Windows.
• A managed application runs in the .NET runtime.
04/19/23 Assoc. Prof. Stoyan Bonev 63
Managed and Unmanaged Code
• A compiler that emits unmanaged code or native code compiles source code to machine code which then just runs under Windows.
• A compiler that emits managed code does not compile source code to machine code but rather to Intermediate Language (IL). Managed code runs in the Common Language Runtime (CLR), not directly under Windows.
• Compilers Visual C++ 2002 and later emit both unmanaged code and managed code
04/19/23 Assoc. Prof. Stoyan Bonev 64
Managed and Unmanaged Code
Using :– Visual C++ .NET 2010/2012– You have a choice of whether you build a
managed or unmanaged application.– Visual C++ .NET is the only language
provided with Visual Studio .NET that can emit either managed code or unmanaged code.
04/19/23 Assoc. Prof. Stoyan Bonev 65
Managed and Unmanaged Code
• When you build a managed application, your code is compiled to IL in a file called an assembly, along with metadata that describes the classes, methods and attributes of the code you’ve created. This assembly is the unit of deployment in the .NET world. You copy it to another server to deploy the assembly there – and often that copying is the only step required in the deployment.
04/19/23 Assoc. Prof. Stoyan Bonev 66
Managed and Unmanaged Code• Managed code runs in the CLR. The runtime offers a
variety of services to your running code:– It first loads and verifies the assembly to make sure the IL
is OK.– Then, just in time, as methods are called, the runtime
arranges for them to be compiled to machine code for the machine the assembly is running on, and caches this machine code to be used the next time the method is called. (This is known as Just In Time or JIT compiling, or just jitting).
– As the assembly runs, the runtime continues to provide services such as security, memory management, threading, etc
• The application is managed by the runtime.
04/19/23 Assoc. Prof. Stoyan Bonev 67
Application in unmanaged C++// UnmanagedCPPApplication.cpp#include <iostream>using namespace std;
class ArithmeticClass // Remark: class with no data members {
public: double Add( double n1, double n2) { return n1 + n2; }
};
void main(){
ArithmeticClass arith; cout << endl << arith.Add(1.1, 2.2);
double answer;answer = arith.Add(4.4, 5.5); cout << endl << answer;
}
04/19/23 Assoc. Prof. Stoyan Bonev 68
Application in managed C++#include "stdafx.h“ // ManagedCPPApplication.cpp : main project fileusing namespace System;
class ArithmeticClass {
public: double Add( double n1, double n2) { return n1 + n2; }
};
void main( ){
ArithmeticClass arith; Console::WriteLine( arith.Add(1.1, 2.2) );double answer;
answer = arith.Add(4.4, 5.5); Console::WriteLine(answer);}
04/19/23 Assoc. Prof. Stoyan Bonev 69
Managed and Unmanaged Data
• When you write unmanaged application, you have only unmanaged data.
• When you write managed application in VB or C#, you have only managed data.
• When you write a managed C++ application, you control whether your data is managed or unmanaged.
04/19/23 Assoc. Prof. Stoyan Bonev 70
Unmanaged Data
• This term is a way to describe how data has always been handled in C++.– Given a class– You create an instance of the class on the stack– You create an instance of the class on the heap
and use it through a pointer– See next slide
04/19/23 Assoc. Prof. Stoyan Bonev 71
Unmanaged Data
class ArithmeticClass {public: double Add( double num1, double
num2){ return num1 + num2; }
};
void main(){
ArithmeticClass arith; cout << endl << arith.Add(1.1, 2.2);
ArithmeticClass *pArith = new ArithmeticClass();cout << endl << pArith->Add(2., 2.);
}
04/19/23 Assoc. Prof. Stoyan Bonev 72
Unmanaged Data
No problems with the stack variable arith.
The heap variable must be explicitly cleared away:delete pArith;
So, memory management is your job in classic C++.What you create with new, you must clean up with delete.
If you forget, you can suffer a memory leak.
04/19/23 Assoc. Prof. Stoyan Bonev 73
Garbage-Collected Classes
• If you create a GCC ( __gc … ), the runtime will manage the memory for you.
• In exchange:– you give up capability to create an instance on the
stack– You have to follow some rules when designing your
class• Inheritance Restrictions• Single Inheritance• You cannot override operator & or operator new• You cannot implement a copy constructor.
04/19/23 Assoc. Prof. Stoyan Bonev 74
Garbage-Collected Classes// you set garbage collection class-by-class, not object-by-object
__gc class Sample{private: double number;public:
Sample (double num): number(num) { }};
void main( ){
Sample a(5.0); // Error
Sample* pa = new Sample(6.0); // OK,no need of delete operator}
04/19/23 Assoc. Prof. Stoyan Bonev 75
Value Classes
• Programmers feel uncomfortable when they are told they cannot create instances of GCC /managed data/ on the stack.
• The fundamental types, such as int, float, are referred as value types, because they are allocated on the stack.
• You can define a class of yours to be a value class ( __value …)
04/19/23 Assoc. Prof. Stoyan Bonev 76
Value Classes
__value class Sample{private: double number;public:
Sample (double num): number(num) { }};
void main( ){
Sample a(5.0); // OKSample* pa = __nogc new Sample(6); // OK
}
04/19/23 Assoc. Prof. Stoyan Bonev 77
Value Classes
• These value classes are still managed, but are not garbage-collected.
04/19/23 Assoc. Prof. Stoyan Bonev 78
Boxing
• When you use/mix managed and unmanaged data, you need to use the following .NET concept :– Boxing
04/19/23 Assoc. Prof. Stoyan Bonev 79
Boxing a Fundamental Type
If you try to pass a piece of unmanaged data to a method that is expecting managed data, the compiler will reject your attempt.
The way to get a pointer to garbage-collected data (i.e managed data) is to use the __box extension.
This is referred as boxing. See next slide.
04/19/23 Assoc. Prof. Stoyan Bonev 80
Boxing a Fundamental Type
int i=3;
System::Console::Write(“ i is =“);
System::Console::WriteLine(i);
System::Console::WriteLine( __box(i) );
04/19/23 Assoc. Prof. Stoyan Bonev 81
Properties • Managed classes have properties. class Employee { private: int salary;
. . . // remaining member variables
public: int getSalary() { return salary; } void setSalary(int s) {salary=s;}
. . . // remaining public methods
};
04/19/23 Assoc. Prof. Stoyan Bonev 82
Properties
• Basic concept: class Employee has data member salary– It’s a private variable– With public get and set functions
• Many classes follow this “get and set” paradigm.• Benefit is encapsulation.• Downside is the way your code looks when
you’re using get and set functions.
04/19/23 Assoc. Prof. Stoyan Bonev 83
Properties
• Managed classes have properties.
If you have a pointer to an instance of class Employee, say pe and you want to give that Employee’s $5000.00 raise, here is the code
Employee *pe = new Employee; pe->setSalary(40000);
pe->setSalary(pe->getSalary() + 5000);
04/19/23 Assoc. Prof. Stoyan Bonev 84
Properties
• In order to simplify the last line code
__gc class Employee { private: int salary; public:
__property int get_Salary() { return salary; }__property void set_Salary(int s) {salary=s;}
};
04/19/23 Assoc. Prof. Stoyan Bonev 85
Properties
• Now the Employee class has a Salary property. It is directly tied to salary member variable.
• The property name is what appears to follow get_ and set_ of the function names.
• To use the property, you write code that treats the Salary property as a public member variable: pe->Salary = 40000; pe->Salary = pe->Salary + 5000;
or pe->Salary += 5000;
Digression on Properties in VBasic
(read or skip 8 coming slides)
04/19/23 Assoc. Prof. Stoyan Bonev 87
Class Counter
Private m_count As Integer
. . .
Public Sub New()
m_count = 0
End Sub
Public Sub New(ByVal val As Integer)
m_count = val
End Sub
. . .
End Class
04/19/23 Assoc. Prof. Stoyan Bonev 88
Dim b, c As Counter
b = New Counter
b.ShowData()
c = New Counter(20)
c.ShowData()
04/19/23 Assoc. Prof. Stoyan Bonev 89
Class Distance
Private m_feet As Integer, m_inches As Single . . . Public Sub New() m_feet = 0 : m_inches = 0.0 End Sub
Public Sub New(ByVal valft As Integer, ByVal valin As Single) m_feet = valft m_inches = valin End Sub . . .End Class
04/19/23 Assoc. Prof. Stoyan Bonev 90
Dim aa, bb, cc As Distance
aa = New Distance(3, 3.0) bb = New Distance(6, 6.0) cc = New Distance
aa.ShowData() : bb.ShowData() : cc.ShowData()
04/19/23 Assoc. Prof. Stoyan Bonev 91
Example class Counter with property block
Class Counter
Private m_count As Integer . . .
Public Property Count() As Integer
Get Return m_count ' read value of property End Get
Set(ByVal Value As Integer) m_count = Value ' write value of property End Set
End Property . . .
End Class
04/19/23 Assoc. Prof. Stoyan Bonev 92
Example class Counter with property block
Dim d As New Counterd.Count = 880Console.WriteLine _
("Counter data displayed through property = {0}", d.Count)
Dim var As Integervar = d.CountConsole.WriteLine _
("Counter data displayed through assignment = {0}", var)
04/19/23 Assoc. Prof. Stoyan Bonev 93
Example class Distance with property block
Class Distance Private m_feet As Integer, m_inches As Single
. . . Public Property Feet() As Integer Get Return m_feet ' read value of property End Get Set(ByVal Value As Integer) m_feet = Value ' write value of property End Set End Property
Public Property Inches() As Single Get Return m_inches ' read value of property End Get Set(ByVal Value As Single) m_inches = Value ' write value of property End Set End Property
. . . End Class
04/19/23 Assoc. Prof. Stoyan Bonev 94
Example class Distance with property block
Dim aa As Distance
aa = New Distance aa.Feet = 5 aa.Inches = 5.0 Console.WriteLine _ ("Distance displayed through property Feet={0} Inches={1}", aa.Feet, aa.Inches)
Dim varft As Integer, varin As Single
varft = aa.Feet varin = aa.Inches Console.WriteLine _ ("Distance displayed through assignment: {0} feet, {1} inches", varft, varin)
04/19/23 Assoc. Prof. Stoyan Bonev 95
Application types you can create with Visual C++
• Console applications
• Windows applications
• Web applications – run on a Web server when a particular URL is loaded, and send HTML to a waiting browser.
• Services – are applications that run in the background and respond to requests, but do not have any visual interface
• Class libraries – are collections of code that can be called by other code.
• Other
04/19/23 Assoc. Prof. Stoyan Bonev 96
Creating unmanaged C++ Application
• Open Visual Studio .NET IDE
• On the main menu choose File , New , Project
• Select Visual C++ projects, Win32
04/19/23 Assoc. Prof. Stoyan Bonev 97
Creating managed C++ Application
• Open Visual Studio .NET IDE
• On the main menu choose File , New , Project
• Select Visual C++ projects, CLR
The .NET Base Class Libraries
04/19/23 Assoc. Prof. Stoyan Bonev 99
The .NET Base Class Libraries
• Libraries Shared Across Languages
• When you write managed C++, you have access to all of the managed code libraries that come with the .NET Framework:– The Base Class Libraries
– ASP.NET
– ADO.NET
• These libraries are modern and powerful. They provide services, as XML processing, that weren’t even thought of when libraries such as MFC and ATL were first written.
Digression on Namespaces
(read or skip 8 coming slides)
04/19/23 Assoc. Prof. Stoyan Bonev 101
Namespaces in C++
• Namespaces are a C++ feature designed to eliminate name conflicts, such as having two classes, each in different libraries, called the same name, e.g. String.
• Before namespaces, library developers made their names unique by adding letters to them: GCString, CString, TKString. This approach reduces, but doesn’t prevent name conflicts.
• With namespaces, classes can have simple names.
• Name conflicts are much less likely, because in addition to a short or local name, classes have a fully qualified name that includes their namespace.
04/19/23 Assoc. Prof. Stoyan Bonev 102
Namespaces in C++
namespace One { class Common { . . . }; void Do() { … } }
namespace Two { class Common { . . . }; void Do() { … } }
04/19/23 Assoc. Prof. Stoyan Bonev 103
Namespaces in C++
void main()
{
Common c(3); // ambiguous
One::Common c1(3); // OK
Two::Common c2(3); // OK
Do(); // ambiguous
One::Do(); // OK
Two::Do(); // OK
}
04/19/23 Assoc. Prof. Stoyan Bonev 104
Namespaces in C++
using namespace One;
void main()
{
Common c1(3); // OK
Two::Common c2(3); // OK
Do(); // OK
Two::Do(); // OK
}
04/19/23 Assoc. Prof. Stoyan Bonev 105
Namespaces in C++
using namespace Two;
void main()
{
One::Common c1(3); // OK
Common c2(3); // OK
One::Do(); // OK
Do(); // OK
}
04/19/23 Assoc. Prof. Stoyan Bonev 106
Namespaces in C++
Modern class libraries are each in their own namespace.
For example, the templates in the Standard Template Library STL are in the namespace std.
using namespace std;
04/19/23 Assoc. Prof. Stoyan Bonev 107
Namespaces in C++
To use a class in a namespace, you have two choices:• Call the class by its full name
x = System::Math::PI / 4;
• Add a using statement at the top of the file and then call the class by its name within the namespace: using namespace System;
. . .
x = Math::PI / 4;
04/19/23 Assoc. Prof. Stoyan Bonev 108
The .NET Base Class Libraries
• The System Namespace
• All the data types are represented.
• Two classes deserve special mention– System::Console Class
– System::String Class
• Other components– System::DateTime structure
• Other useful namespaces– System::IO namespace
– System::Text namespace
– System::Collections namespace
– System::Threading namespace
Building Simple User Interfaces with Windows Forms
04/19/23 Assoc. Prof. Stoyan Bonev 110
Building Simple User Interfaces with Windows Forms
• A Managed C++ Windows Forms App
04/19/23 Assoc. Prof. Stoyan Bonev 111
Building Simple User Interfaces with Windows Forms
• The project wizard creates a blank form for you, and later you’ll drag controls onto it.
• It also generates a file full of code, which will be edited as you add controls and set properties.
• To see the code, right-click the form in the designer and choose View Code.
04/19/23 Assoc. Prof. Stoyan Bonev 112
Building Simple User Interfaces with Windows Forms
04/19/23 Assoc. Prof. Stoyan Bonev 113
Building Simple User Interfaces with Windows Forms
04/19/23 Assoc. Prof. Stoyan Bonev 114
namespace SimpleForm2 {
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:~Form1(){
if (components){
delete components;}
}
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->components = gcnew System::ComponentModel::Container();this->Size = System::Drawing::Size(300,300);this->Text = L"Form1";this->Padding = System::Windows::Forms::Padding(0);this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
}#pragma endregion
};}
04/19/23 Assoc. Prof. Stoyan Bonev 115
Using the Toolbox to build the user interface
• The Toolbox is a tabbed window that appears on the editing screen.
• When you are working with the designer on Windows Forms, you can drag typical Windows controls onto your form
04/19/23 Assoc. Prof. Stoyan Bonev 116
Using the Toolbox to build the user interface
04/19/23 Assoc. Prof. Stoyan Bonev 117
Using the Toolbox to build the user interface
Popular controls:• Label• Text box• Button• Check box• Radio buttons• Group box• Picture box• DateTime picker
04/19/23 Assoc. Prof. Stoyan Bonev 118
Using the Toolbox to build the user interface
• The form and the controls are designed and implemented as classes with properties.
• The properties pane may be displayed in a way similar to toolbox pane.
• Right click on the control and select Properties.
04/19/23 Assoc. Prof. Stoyan Bonev 119
Using the Toolbox to build the user interface
04/19/23 Assoc. Prof. Stoyan Bonev 120
Using the Toolbox to build the user interface
04/19/23 Assoc. Prof. Stoyan Bonev 121
Demo Application
• Form and two controls:– Picture Box– DateTime picker
04/19/23 Assoc. Prof. Stoyan Bonev 122
Demo Application
04/19/23 Assoc. Prof. Stoyan Bonev 123
Demo Application
04/19/23 Assoc. Prof. Stoyan Bonev 124
Demo Application
04/19/23 Assoc. Prof. Stoyan Bonev 125
Writing Event Handlers
• There are two ways to add a handler to an event• The simplest is to double-click the control in the
designer. This adds a handler to the default event, which varies from control to control.
• To add a handler for another event, use the Properties window: at the top a toolbar with 5 tabs: the 4th is Events. When this tab is pressed, it displays all the events that can occur for this particular control.
126
WPF
What is WPF?Windows Presentation Foundation (WPF) is a next-generation presentation system for building Windows client applications with visually stunning user experiences. –Official Microsoft Definition
Based on: •.NET platform• Extensible Application Markup Language (XAML)
Advantages of WPF
• Easy to create stunning design• Technology for developing future
windows applications• Reuse of existing code• Advanced Data-Binding• Supported by Microsoft
Disadvantages of WPF• Requires .NET Framework 3.0 or
higher• Comparatively slower than
Windows Forms• Requires DirectX9 compatible
video card for graphics
Microsoft Visual Studio &.NET Framework 3.0 or
higher
and
WPF technology for building applications
Building WPF applications• WPF approach may apply for C# and
VBasic.
• UI is described using XAML /Extensible Application Markup Language/.
• Resource definition language and .rc file(s) as well as resource compiler replaced with XAML.
Creating WPF graphical applications
• You design the form-based UI of a Windows-based app interactively
• IDE generates code to implement UI that you designed
• IDE provides two views of a graphical app:– The design view– The code view
• Code and Text Editor window used to modify code and logic for a graphical app
• Design View window used to layout UI• Easy to switch between the two views
whenever user wants
Note
• As with WinForms, drag and drop controls from the Toolbox onto the window
Building C# WPF applications
.
Creating WPF graphical applications
• VS displays an empty WPF form in the Design View window, together with another window containing the XAML description of the form, as shown next slide
• XAML is used by WPF applications to define the layout of a form and its contents.
Assoc. Prof. Stoyan Bonev
Building C# WPF applications
.
Assoc. Prof. Stoyan Bonev
Building C# WPF applications
.
Assoc. Prof. Stoyan Bonev
Building C# WPF applications
.
Assoc. Prof. Stoyan Bonev
Building C# WPF applications
.
140
Demo
• One more comprehensive example
Windows Presentation Foundation
Figure 10-19 WPF design
142
My first WPF application
• Write a program to display an elementary simple form containing 3 controls:
• a label • a text box where you can enter your
name and • a button that displays a personalized
greeting in a message box• Try to Solve task using your skills
from Win Forms technology
143
My first WPF application
• Create your WPF project, named sbWPFHello.• Create the UI
– Click the Toolbox tab– Select/click Label, click visible part of the
form– Reposition and resize the label, – On the View menu, click Properties window,
change Text property. Another way to modify Text property is through XAML pane <Label> tag, Content attribute
144
My first WPF application
– Click the Toolbox tab– Select/click TextBox, click visible part of
the form– Reposition and resize the textbox, – On the View menu, click Properties
window, change Text property. Another way to modify Text property is through XAML pane <TextBox> tag, Content attribute
145
My first WPF application
– Click the Toolbox tab– Select/click Button, click visible part of
the form– Reposition and resize the button, – On the View menu, click Properties
window, change Text property. Another way to modify Text property is through XAML pane <Button> tag, Content attribute
146
My first WPF application
• Build Solution to verify the project builds successfully
• Click Start without Debugging– Form gets displayed– You type your name– Nothing happens
• You managed a C# application without writing any line of C# source code
147
My first WPF application
• You managed a C# application without writing any line of C# source code
• IDE generates a lot of code. Examine the .cs files attached to the project
• You need to add the EvH code associated with the button control
148
My first WPF application
• Before we include EvH code, examine what IDE generated as C# source code:
• The generated code handles routine tasks that all graphical apps must perform such as– Starting up an application– Displaying the form etc.
149
My first WPF application
• In Solution explorer click ‘+’ beside the file MainWindow.xaml
• The MainWindow.xaml.cs file appears.• Open the ….cs file. ..
– You see a lot of using namespace stmts– You see definition of a class MainWindow :
Window– You see a constructor that calls method InitializeComponent();– The purpose of code you see is that you can add
your own methods to handle the logic for your application, such as what happens when the user clicks a button
150
My first WPF application
• Now you may be wondering– Where is the Main() method– How the form gets displayed when the
app runs
• In Solution explorer you can see one more XAML file, App.xaml
151
My first WPF application• In Solution explorer click ‘+’ beside the file
App.xaml• The App.xaml.cs file appears.• Open the App.xaml.cs file
– You see a lot of using namespace stmts.– You see not a lot else (there is a lot of
hidden code)– The Main() method is hidden as well. But its
code is generated based on settings of the App.xaml file
– Main() method will create and display the form by the StartupURI property.
152
My first WPF application
• Now, it’s time to start writing C# source code, i.e. the EvH associated with the Button.
153
My first WPF application
• Double click on the button control• Source code window opens• You have to fill the body of the EVH andler
method
MessageBox.Show(“Hello, “+textBox1.Text);
• Rebuild the solution• Click the button• The program works.
Thank You
For
Your attention!
Any questions?