Chapter IV. Graphical User Interface in C++ CLI
Chapter IV. Graphical User Interface in C++ CLI
Hà Nội, 2018 1
Chapter IV. Graphical User Interface in C++/CLI
2
Specialties of CLI, standard C++ and C++/CLI
There are several ways to develop applications for a computer running the
Windows operating system:
• We implement the application with the help of a development kit and it will operate
within this run-time environment. The file cannot be run directly by the operating
system (e.g. MatLab, LabView) because it contains commands for the run-time
environment and not for the CPU of the computer. Sometimes there is a pure run-time
environment also available beside the development kit for the use of the application
developed, or an executable (exe) file is created from our program, which includes the
run-time needed for running the program.
• The development kit prepares a stand-alone executable application file (exe), which
contains the commands written in machine code runnable on the given operating
system and processor (native code). This file is run while developing and testing the
program. Such tools are e.g. Borland Delphi and Microsoft Visual Studio, frequently
used in industry.
3
Specialties of CLI, standard C++ and C++/CLI
5
Specialties of CLI, standard C++ and C++/CLI
8
Specialties of CLI, standard C++ and C++/CLI
9
Specialties of CLI, standard C++ and C++/CLI
10
Specialties of CLI, standard C++ and C++/CLI
11
Specialties of CLI, standard C++ and C++/CLI
12
Specialties of CLI, standard C++ and C++/CLI
❖ 1.8. C#
• The .NET framework and the pure managed code can be programmed with C# easily.
• It is recommended to amateurs and students in higher education (not for
programmers – their universal tools are the languages K&R C and C++).
• The .NET framework contains a command line C# compiler and we can also download
freely the Visual C# Express Edition from Microsoft.
• Their goal with this is to spread C# (and .NET).
13
Specialties of CLI, standard C++ and C++/CLI
14
Specialties of CLI, standard C++ and C++/CLI
17
Specialties of CLI, standard C++ and C++/CLI
19
Specialties of CLI, standard C++ and C++/CLI
20
Specialties of CLI, standard C++ and C++/CLI
21
Specialties of CLI, standard C++ and C++/CLI
22
Specialties of CLI, standard C++ and C++/CLI
23
Specialties of CLI, standard C++ and C++/CLI
24
Specialties of CLI, standard C++ and C++/CLI
• The Convert class, however, performs the real <-> string conversion according to the
region and language settings (CultureInfo). The CultureInfo can be set for the current
program, if for example we got a text file that contains real numbers in English format.
25
Specialties of CLI, standard C++ and C++/CLI
26
Specialties of CLI, standard C++ and C++/CLI
❖ 1.13. The reference class of the array implemented with the CLI
array template
• Declaration: cli::array<type, dimension=1>^ arrayname, the dimension is optional; in
this case its value is 1. The ^ is the sign of the ref class, the cli:: is also omissible, if we
use at the beginning of our file the using namespace cli; statement.
• We have to allocate space for the array with the gcnew operator before using – since it
is a reference class when declaring a variable only the handle is created, and it is not
pointing to anywhere. We can make the allocation in the declaration statement as
well: we can list the elements of the array between { } as used in C++.
• Array’s property: Length gives the number of elements of the onedimensional array.
For arrays passed to a function we do not have to pass the size, like in the basic C. The
size can be used in the loop statement, which does not address out from the array:
27
Specialties of CLI, standard C++ and C++/CLI
❖ 1.13. The reference class of the array implemented with the CLI
array template
For the basic array algorithms static methods were created, and those are stored in
the System::Array class:
• Clear(array, from where, how many) deletion. The value of the array elements will be
0, false, null, nullptr (depending on the base type of the array),
• Resize(array, new size) in case of resizing (expanding) after the old elements it fills the
array with the values used with Clear().
• Sort(array) sorting the elements of the array. It can be used by default to order
numerical data in ascendant order. We can set keys and a comparing function to sort
any type data.
• CopyTo(target array, starting index) copying elements. Note: the = operator duplicates
the reference only. If an element of the array is changed, this changed element is
reached using the other reference as well. Similarly, the == oparetor that the two
references are the same but it does not compare the elements themselves.
28
Specialties of CLI, standard C++ and C++/CLI
❖ 1.13. The reference class of the array implemented with the CLI
array template
29
Specialties of CLI, standard C++ and C++/CLI
30
Specialties of CLI, standard C++ and C++/CLI
31
Specialties of CLI, standard C++ and C++/CLI
32
Specialties of CLI, standard C++ and C++/CLI
33
Specialties of CLI, standard C++ and C++/CLI
The Toolbox 34
Specialties of CLI, standard C++ and C++/CLI
35
Specialties of CLI, standard C++ and C++/CLI
36
Specialties of CLI, standard C++ and C++/CLI
Project properties
39
Specialties of CLI, standard C++ and C++/CLI
40
Chapter IV. Graphical User Interface in C++/CLI
41
2. The window model and the basic controls
2.19. The status bar appearing at the bottom of the window, the
StatusStrip control
2.20. Dialog windows helping file usage: OpenFileDialog,
SaveFileDialog and FolderBrowserDialog
2.21. The predefined message window: MessageBox
2.22. Control used for timing: Timer
2.23. SerialPort
44
2. The window model and the basic controls
45
2. The window model and the basic controls
46
2. The window model and the basic controls
47
2. The window model and the basic controls
48
2. The window model and the basic controls
49
2. The window model and the basic controls
50
2. The window model and the basic controls
51
2. The window model and the basic controls
52
2. The window model and the basic controls
53
2. The window model and the basic controls
54
2. The window model and the basic controls
55
2. The window model and the basic controls
56
2. The window model and the basic controls
57
2. The window model and the basic controls
58
2. The window model and the basic controls
59
2. The window model and the basic controls
60
2. The window model and the basic controls
61
2. The window model and the basic controls
62
2. The window model and the basic controls
63
2. The window model and the basic controls
• With the help of NumericUpDown control we can enter an integer number. It will
appear in Value property, between the Minimum and Maximum values. The user can
increase and decrease the Value by 1, when clicking on the up and down arrows. All
the integer numbers between the Minimum and Maximum values appear among the
values to choose from. Event: ValueChanged is running after every change.
64
2. The window model and the basic controls
65
2. The window model and the basic controls
• With the help of ProgressBar we can give information about the status of the process,
how much is still left, the program has not frozen, it is working. We have to know in
advance when the process will be ready, when it should reach the maximum. The
control’s properties are similar to HScrollBar, however the values cannot be modified
with the help of the mouse. Certain Windows versions animate the control even if it
shows a constant value. Practically, this control used to be placed in the StatusStrip in
the bottom left corner of our window. When clicking on it, its Click event is running,
however, it is something we normally do not deal with.
66
2. The window model and the basic controls
67
2. The window model and the basic controls
Normal size picturebox on the form Stretched size picturebox on the form 68
2. The window model and the basic controls
• In case our program implements so many functions that we would require a large
number of command buttons in order to start them, it is practical to group functions
hierarchically and settle them into a menu.
The submenu
71
2. The window model and the basic controls
The contextmenu
72
2. The window model and the basic controls
73
2. The window model and the basic controls
Toolkit on toolstrip 74
2. The window model and the basic controls
75
2. The window model and the basic controls
• Almost all computer programs in which modifications carried out can be saved contain
the Open file and Save file functions. Selecting a file to be saved or to be opened is
done with the same window type in each software because the developers of .NET
created a uniform control for these windows.
76
2. The window model and the basic controls
77
2. The window model and the basic controls
78
2. The window model and the basic controls
79
2. The window model and the basic controls
❖ 2.23. SerialPort
• The SerialPort control makes possible the communication between a serial port of rs-
232 standard and the peripherial devices connected to it (modem, SOC,
microcontroller, Bluetooth device). The port has to be parameterized then opened.
Then, textual information (Write, WriteLine, Read, ReadLine) and binary data
(WriteByte, ReadByte) can be sent and received. It also has an event handler function:
if data arrive, the DataReceived event is run. The following code portion checks all
available serial ports, and searches the hardware named "iCorset" by sending a "?"
character. If it is not found, it exits with an error message. In the case of virtual serial
ports (USBs), the value of the BaudRate parameter can be anything.
80
2. The window model and the basic controls
❖ 2.23. SerialPort
81
Chapter IV. Graphical User Interface in C++/CLI
82
3. Text and binary files, data streams
83
3. Text and binary files, data streams
Another thing to do is to decide what the file to be handled contains and what we would
like to do with it:
• Only deleting, renaming, copying it or checking whether it exists.
• We would like to handle it by bytes (as a block containing bytes) if we are brave
enough: virus detection, character encoding etc.
• It is a binary file with fixed-length record structure.
• If it is a text file with variable-length lines (Strings), and the lines end with line feed.
84
3. Text and binary files, data streams
85
3. Text and binary files, data streams
86
3. Text and binary files, data streams
87
3. Text and binary files, data streams
88
3. Text and binary files, data streams
90
3. Text and binary files, data streams
91
3. Text and binary files, data streams
92
3. Text and binary files, data streams
93
3. Text and binary files, data streams
95
3. Text and binary files, data streams
96
3. Text and binary files, data streams
98
3. Text and binary files, data streams
99
3. Text and binary files, data streams
100