Developing User Interface (GUI) .
Developing User Interface (GUI) .
Developing User Interface (GUI) .
C++/Common Language
Infrastructure (C++/CLI)
So far the C++ programs we have learnt essentially
follow ANSI standard (Native C++)
– It means that those programs should be able to be compiled
and run in any computer platform, including Windows, UNIX,
Linux, Mac OS, etc.
Recently, Microsoft has developed in its Visual C++
2005 a new set of extension keywords (known as the
C++/CLI) to generate code that targets the .NET
Framework, which is also called managed code
Hence the codes that do not use these extension
keywords are known as unmanaged code.
Native C++: Unmanaged code
C++/CLI: Managed code
3
Computer Programming and Basic Software Engineering
9 Building Graphical User Interface
AAtypical
typicaldesktop
desktopof ofaapersonal
personal
computer
computer––full
fullof
oficons,
icons,menus,
menus,
windows,
windows,etc.
etc.to
tocommunicate
communicatewith
with
user
user
5
Computer Programming and Basic Software Engineering
9 Building Graphical User Interface
Step 2
Select
SelectWindows
Windows
Forms
Forms
Application
Application
8
Computer Programming and Basic Software Engineering
9 Building Graphical User Interface
AAplain
plainform
formwill
will
be
becreated
createdforforyou
you
to
tofill
fillin
inthe
the
details
details
9
Computer Programming and Basic Software Engineering
9 Building Graphical User Interface
You
Youcan canfind
findin
in
the
theProperties
Properties
window
windowthe thename
name
of
ofeach
eachcontrol.
control.
e.g.
e.g.thethename
nameforfor
this
thistexttextbox
boxisis
textBox1
textBox1
ItItisisimportant
importantfor for
developing
developing
program
programcodes codesfor
for
them
them
12
Computer Programming and Basic Software Engineering
9 Building Graphical User Interface
Step 3
From
Fromthe
the
toolbox,drag
toolbox, dragaa
label,aatextbox
label, textbox
and
andaabutton
buttonto to
the
theform
form
13
Computer Programming and Basic Software Engineering
9 Building Graphical User Interface
Step 5
•• Resize
Resizethe
theform
formto
tothe
the
original
original
•• Click
Clickthe
thetextbox
textbox
•• On
OnthetheProperties
Properties
window,
window,change
changethethe
Anchorproperty
Anchor propertyto to
“Top
“Top LeftLeft Right”
Right”
•• Try
Tryto toenlarge
enlargeyour
your
form.
form.YouYouwill
willsee
seethe
the
size
sizeofofthe
thetextbox
textbox
changes
changeswithwiththe
theform
form
size
size
14
Computer Programming and Basic Software Engineering
9 Building Graphical User Interface
•• Try
Trytotoenlarge
enlargethetheform.
form.
You
Youwill
willfind
findthat
thatthe
thesize
size
of
ofthe
thetextbox
textboxwill
willnot
not
change
changeaccordingly.
accordingly.
•• By
Bydefault,
default,all
allcomponents
components
are
areanchored
anchoredwithwithrespect
respect
to
tothe
thetop
topand
andleft
left
boundaries
boundariesof ofthe
theform.
form.
•• Hence,
Hence,notnotonly
onlythe
thesize,
size,but
but
the
theposition
positionofofall
all
components
componentswillwillnot
notchange
change
when
whenresizing
resizingthe
theform
form
15
Computer Programming and Basic Software Engineering
9 Building Graphical User Interface
Is there
Yes an event handler Yes
Any event?
for that
event?
No No
Run event
handler
MessageBox::Show(message,title,
MessageBoxButtons::OK);
• Show() is a static member function of the
MessageBox Class. We can call any member
function of a class without instantiating an object of
that class
• message is the message to be displayed in the message
box. It is of the managed class String^
• title is the title of the message box. It is of the
managed type String^
• OK is a static member enumeration of the class
MessageBoxButtons. If OK is used, only the OK
button will be shown. There can be other buttons, such
as, YesNo, OKCancel, AbortRetryIgnore, etc.
Use Help of Visual C++ 2005 to learn MessageBox and MessageBoxButtons
19
Computer Programming and Basic Software Engineering
9 Building Graphical User Interface
using namespace
System::Runtime::InteropServices;
• The class name such as Marshal is defined under the
System::Runtime::InteropServices namespace
i.e. the full name of the class is
System::Runtime::InteropServices::Marshal
• The above statement needs to be added at the beginning
of Form1.h with the other namespace definitions.
String ^ tbstr = textBox1->Text;
• For C++/CLI, any string should be defined as a handle to
an object of the System::String class
• Here, textBox1->Text is a managed string. That’s
why it should be assigned to the handle tbstr as above.
24
Computer Programming and Basic Software Engineering
will return
will returnan
9 Building Graphical User Interface
anobject
objectof
oftype
typeIntPtr
IntPtr
char *name = (char*)Marshal::
StringToHGlobalAnsi(tbstr).ToPointer();
:
Marshal::FreeHGlobal((IntPtr)name);
• To convert a managed string to an unmanaged string, we need
to make use of the function StringToHGlobalAnsi(),
HGlobal
= Global which is a static member function of the class Marshal
heap • The function will return an object of class IntPtr
Exercise 9.1
Modify the form as shown below. When a user enters 2
numbers and click Add, a message box will be shown to
display the sum of the 2 numbers.
Don't forget to
change the title
of the form
30
Computer Programming and Basic Software Engineering
9 Building Graphical User Interface