Hello Nana
Project URL
Introduction
Let's start a simple program and explain it in one line.
1 # include <Nana/GUI/wvl. HPP>
2 # include <Nana/GUI/widgets/label. HPP>
3IntMain ()
4 {
5Using namespaceNANA: Gui;
6 form FM;
7 label Lab (FM, Nana: size (100,
20 ));
8 lab. Caption (STR ("Hello Nana "));
9 FM. Show ();
10 exec ();
}
The first and second rows includeNamespaceNANA: Form class and label class in GUI. All interface programs developed using Nana C ++ library must contain <Nana/GUI/wvl. HPP>.
The fifth line introduces the namespace NANA: GUI to the main function domain, so that the compiler can directly find the name defined in Nana: Gui, such as form, label, and exec.
Row 6 defines a form object named FM. In this example, this is a window for placing the label microware.
Row 7 defines a label object named lab. It is a label microware used to display text strings and is created in a window. Microware is a visual element in the user interface.
Row 8 sets the title of the label object. Each microfile has a caption () method to set the title. In this line of code, there is a string referenced by a macro named STR (). This macro is used to overwrite a string to facilitate switching between Unicode and multi-byte versions.
The ninth line to make the window visible.
Row 10: give control of the program to the nana database. At this time, the program enters the event loop and waits for and receives user input, for example, moving the mouse, clicking and hitting the keyboard. Exec () the function blocks the thread until the window FM is closed. In this example, when the window is closed, the program ends.
Fig 1.1Hello Nana
Now, before running the program in your system, you should install Nana C ++ library. In the help directory, installation library documentation explains the installation method.
Event Processing
To receive user operations, You need to register the event processing function for the specified microware. The Nana library will wait for user input and then trigger the event processing function corresponding to the event. The second example shows how to respond to user operations. The program consists of a button. When you click this button, the program exits.
#include <nana/gui/wvl.hpp>#include <nana/gui/widgets/button.hpp>int main(){using namespace nana::gui;form fm;button btn(fm, nana::size(100, 20));btn.caption(STR("Quit"));btn.make_event<events::click>(API::exit);fm.show();exec();}
This code is similar to the first example. It only replaces the label with a button and registers a click event for the button. All widget (microware) classes provide a set of methods for registering events. All these methods are named make_event. This is a function template. The first template parameter specifies the type of an event, and must be explicitly specified. The parameter of this function template is the event processing function. API: exit () is a function provided by the nana library. It is used to close all windows in the current UI thread and exit the event loop function. In this example, when a button is clicked, API: exit () is called, and the window is closed, and the program ends.
In the next article, we will explain the function objects and Lambdas, and how these two language features are used in event processing to make the program structure clearer.