Nana C ++ Library: Preliminary Exploration

Source: Internet
Author: User

Although C ++ is a powerful and flexible syntax language, many programmers do not like to use C ++ to develop guis because it is too complicated to use C ++ to develop guis. Some existing C ++ GUI frameworks define some rules, and you need to write some rigid code to run them. This will always cause some problems, for example, it makes it difficult to maintain the hierarchy of inheritance in depth. Now, there is another option: Nana C ++ library, a pure C ++ library, which allows you to fully utilize your c ++ knowledge, skills, and techniques to compile the GUI, this is a major improvement in developing a GUI using C ++.

Easy to learn and easy to use

How easy is it to create a hello World Program with Nana?

#include <nana/gui/wvl.hpp>#include <nana/gui/widgets/label.hpp>int main(){using namespace nana::gui;form fm;label lb(fm, fm.size());lb.caption(STR("Hello, World"));fm.show();exec();}

Very simple and easy-to-understand code. Nana introduces simple and rational concepts to keep it concise. Second, unlike those frameworks that write rigid code due to naming and syntax constraints, Nana makes your code more intuitive and readable. For example, to respond to an event.

#include <nana/gui/wvl.hpp>#include <nana/gui/widgets/button.hpp>void clicked(const nana::gui::eventinfo&){//When the window corresponds to fm is clicked, this function will be "called".}int main(){using namespace nana::gui;form fm;fm.make_event<events::click>(clicked);fm.show();exec();}

The name of the function clicked () is not highly restrictive. You can give it any name. This is more intuitive than the method for implementing the response by inheriting an event interface. In some cases, we do not need to care about the parameters of the clicked () function, for example, the example above.

Void clicked () // No parameter. {// when the form is clicked, this function will be "called ".} FM. make_event <events: Click> (clicked); // Nana is also allowed!

Very flexible, keeping your code simple and clear. This feature also applies to function objects.

What makes Nana so flexible?

Nana C ++ library does not contain any "extra compiler" to parse "Special syntax". Nana uses 100% C ++ and template technology to make it powerful and flexible. Nana is not like other template-based libraries, leading to a large amount of code expansion and requires programmers to have template skills. Nana is also very friendly to C ++ beginners.

Nana is a complete C ++ library that can run in Visual C ++ 7.1/GCC 3.4 or later compilers. If you are a C ++ expert, Nana also allows you to use the latest feature Lambda in C ++ 11 to handle events. For example

FM. make_event <events: Click> ([] {// when the window is clicked, the object created by Lambda will be "called"}); orfm. make_event <events: Click> ([] (const eventinfo & EI) {// when the window is clicked, the object created by Lambda is called, // you can obtain the parameter information of this event through Ei });

In addition, if Nana is used with STD: bind in C ++ 11, more flexibility can be achieved.

Multithreading

To put it simply, Nana is thread-safe, and it is common to access widget objects in different threads. This is an important feature that allows programmers to conveniently submit event processing tasks to other threads. For example

# Include <Nana/GUI/wvl. HPP> # include <Nana/Threads/pool. HPP> void Foo () {// this function will be "called"} int main () {using namespace NANA: Gui; using namespace NANA:: threads; pool thrpool; form FM; FM. make_event <events: Click> (pool_push (thrpool, foo); FM. make_event <events: Click> (pool_push (thrpool, [] {// lambda expressions can also be used}); FM. show (); Exec ();}

RaiI

An important feature is shown in the examples above. When a form object is created, the corresponding window is also created, and the window is hidden until the show () method is called. When the form object is destroyed, the corresponding window is closed, which also conforms to the concept of object lifecycle in C ++.

Cross-platform programming

Nana C ++ library is designed for cross-platform programming. Although the release of the first version can only run on Windows, the Library is now basically transplanted to Linux (X11) platform.

The most important feature: free

This is an open-source project, which is free for non-commercial and commercial applications.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.