A Tutorial of Nana C++ Library

This tutorial is intended to get you started programming with Nana. Let's start a simple Hello World program.

#include <nana/gui/wvl.hpp>
#include <nana/gui/widgets/button.hpp>

int main()
{
    using namespace nana::gui;

    form fm;
    fm.caption(L"Hello, World!");

    button btn(fm, nana::rectangle(20, 20, 150, 30));
    btn.caption(L"Quit");
    btn.make_event<events::click>(API::exit);

    fm.show();
    exec();
}

Screenshot



Walkthrough Line-by-line

    #include <nana/gui/wvl.hpp>

This line  includes the Nana class definition.

    #include <nana/gui/widgets/button.hpp>

This line includes the Nana own widget, button class definition.

    int main()
    {


The main() function which is defined by C++ is the entry point to the program. Almost always when using Nana, main() only needs to perform some kind of initialization before passing the control to the Nana library, which then tells the program about the user actions via events.

    using namespace nana::gui;

Specify the nominated namespace nana::gui can be used in main() function scope. In this example, these names form, button, events, API, and exec() are defined in the namespace nana::gui. With this using-directive, we could use these names directly in the main() function scope.

    form fm;

This is the first piece of window-system code. A form is creating while the variable fm is defined. The form is a window with title bar and a sizable border frame, it's a fundamental that you can put some widgets above it.

    fm.caption(L"Hello, World!");

Set the form to display the text "Hello, World!" in its title bar.

    button btn(fm, nana::rectangle(20, 20, 150, 30));

After the form, comes a button we created. In its constructor arg-list, the first argument tells the btn who the parent window is, and the following arguments describe position and size of the button.

    btn.caption(L"Quit");

Set the btn to display the text "Quit".

    btn.make_event<events::click>(API::exit);

make_event() is a method that every Nana widgets provide, you can register an event callback by using it. We want to exit the program while a mouse clicks on the btn. Now, register a callback function on click event.

    fm.show();

A form widget is never visible when you create it. You must call show() to make it visible.

    exec()

This is where the main() passes the control to Nana, and exec() will return when the form is closed. Inside of exec(), Nana processes the message loop and passes every event on to the appropriate widgets.

    }

You should now save and compile this program.

Further

btn.make_event<events::click>(API::exit);

What is API::exit?

This is an API provided by Nana, its prototype is void exit(), if exit() is called, Nana destroies all the windows in its calling thread, and the exec() will return. Member function make_event() has a parameter, that can be a function or a function object with type of void(const nana::gui::eventinfo&) or not.

When the button is clicked, the exit() function destroies all GUI in the event callback, is it right that call exit() in an event callback?

Yes, it is right that Nana guarantees a correct if it access an invalid GUI objects handle.