I need to make a function which open new form. In this form need to use mouse event and change label caption. Need this form to open many times.

bool openHold() {
    using namespace nana::gui;

    form fm(nana::rectangle(20, 20, 300, 300));
    fm.caption(L"Hello World");

    static label lm(fm);
    lm.caption(L"Hello, world!");
    button change(fm), quit(fm);
    change.caption(L"Change");
    quit.caption(L"Quit");

    place plc(fm);
    plc.div("<><weight=80% vertical<><weight=70% vertical<vertical label><weight=25 buttons>><>><>");
    plc.field("label") << lm;
    plc.field("buttons") << change<<10<<quit;

    plc.collocate();

    struct X {static void del() {
        lm.caption(L"World change");
    }};
    change.make_event<events::click>(X::del);

    fm.show();
    exec();

    return true;
}
int main() {
    for (int i=0; i<2; i++) {
        openHold();
        Sleep(500);
    }
    return 0;
}

The first loading of openHold() it's ok, but in the second, label lm is invisible.

I need to make a function which open new form. In this form need to use mouse event and change label caption. Need this form to open many times. ```` bool openHold() { using namespace nana::gui; form fm(nana::rectangle(20, 20, 300, 300)); fm.caption(L"Hello World"); static label lm(fm); lm.caption(L"Hello, world!"); button change(fm), quit(fm); change.caption(L"Change"); quit.caption(L"Quit"); place plc(fm); plc.div("<><weight=80% vertical<><weight=70% vertical<vertical label><weight=25 buttons>><>><>"); plc.field("label") << lm; plc.field("buttons") << change<<10<<quit; plc.collocate(); struct X {static void del() { lm.caption(L"World change"); }}; change.make_event<events::click>(X::del); fm.show(); exec(); return true; } int main() { for (int i=0; i<2; i++) { openHold(); Sleep(500); } return 0; } ```` The first loading of openHold() it's ok, but in the second, label lm is invisible.

The label object is the problem. When you call the openHold() function a second time, a new form object is constructed, but the label still has the the first form object as the parent, because it's declared as static and is only constructed once, when you call the function for the first time. Since the first form object no longer exists at the second function call, the nana::window handle that the label uses as parent is invalid, and the place object should throw an exception when you try to add the label to one of its fields (not sure why you're not getting an exception).

To address this issue, you can do this:

    static label lm;
    lm.create(fm, true);

This way, the label always has the correct parent.

I'm not sure I understand why you want the label to be static, but that's a different issue.

The `label` object is the problem. When you call the `openHold()` function a second time, a new `form` object is constructed, but the `label` still has the the first `form` object as the parent, because it's declared as `static` and is only constructed once, when you call the function for the first time. Since the first `form` object no longer exists at the second function call, the `nana::window` handle that the label uses as parent is invalid, and the `place` object should throw an exception when you try to add the label to one of its fields (not sure why you're not getting an exception). To address this issue, you can do this: ```` static label lm; lm.create(fm, true); ```` This way, the label always has the correct parent. I'm not sure I understand why you want the label to be static, but that's a different issue.
edited Jun 25 at 6:23 pm

This worked perfect! Thanks! Need static to can get the label into struct and change the caption.

For C++03 can I change the buttons colors ? I see btn.bgcolor(colors::blue); do not works in the 0.7.2 last version of nana for C++03

This worked perfect! Thanks! Need static to can get the label into struct and change the caption. For C++03 can I change the buttons colors ? I see btn.bgcolor(colors::blue); do not works in the 0.7.2 last version of nana for C++03
edited Jun 25 at 7:21 pm

For C++03 can I change the buttons colors ? I see btn.bgcolor(colors::blue); do not works in the 0.7.2 last version of nana for C++03

In version 0.7.2, the button class doesn't have the bgcolor method. It's still possible to change the bg color with button::set_bground, but it's a bit more complicated. You have to draw the background yourself, by overriding the draw pure virtual function of abstract class nana::element::element_interface.

The following code works with Nana 1.7, let me know if you have trouble adapting it for your project:

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

class btn_bg : public nana::element::element_interface
{
public:
    bool draw(nana::paint::graphics &g, const nana::color&, const nana::color&, const nana::rectangle &r, nana::element_state state) override
    {
        // gradient colors for the bg, change them to whatever you like
        nana::colors from = nana::colors::light_goldenrod_yellow, to = nana::colors::light_coral;

        // draw the custom background
        if(state != nana::element_state::pressed)
            g.gradual_rectangle(r, from, to, true);
        else g.gradual_rectangle(r, to, from, true);

        // draw the frame like the library does
        g.rectangle(r, false, static_cast<nana::color_rgb>(0x7f7f7f));
        g.palette(false, nana::colors::button_face);
        nana::paint::draw draw(g);
        draw.corner(r, 1);
        g.palette(false, static_cast<nana::color_rgb>(0x919191));
        draw.corner(nana::rectangle(r).pare_off(1), 1);

        return true;
    }
};

int main()
{
    using namespace nana;

    form fm(API::make_center(300, 300));
    fm.div("vert margin=15 <><btn weight=30><>");

    button btn(fm, "Button");
    fm["btn"] << btn;
    btn.set_bground(btn_bg());

    fm.collocate();
    fm.show();
    nana::exec();
}

EDIT: Actually, I just realized that bgcolor is a method of class widget, not button. Looking at widget.hpp from version 0.7.2, I see that method void background(nana::color_t); exists, so you can probably use that. You can still use button::set_bground if you want to use two different colors for the gradient.

>For C++03 can I change the buttons colors ? I see btn.bgcolor(colors::blue); do not works in the 0.7.2 last version of nana for C++03 In version 0.7.2, the `button` class doesn't have the `bgcolor` method. It's still possible to change the bg color with `button::set_bground`, but it's a bit more complicated. You have to draw the background yourself, by overriding the `draw` pure virtual function of abstract class `nana::element::element_interface`. The following code works with Nana 1.7, let me know if you have trouble adapting it for your project: ```` #include <nana/gui.hpp> #include <nana/gui/widgets/button.hpp> class btn_bg : public nana::element::element_interface { public: bool draw(nana::paint::graphics &g, const nana::color&, const nana::color&, const nana::rectangle &r, nana::element_state state) override { // gradient colors for the bg, change them to whatever you like nana::colors from = nana::colors::light_goldenrod_yellow, to = nana::colors::light_coral; // draw the custom background if(state != nana::element_state::pressed) g.gradual_rectangle(r, from, to, true); else g.gradual_rectangle(r, to, from, true); // draw the frame like the library does g.rectangle(r, false, static_cast<nana::color_rgb>(0x7f7f7f)); g.palette(false, nana::colors::button_face); nana::paint::draw draw(g); draw.corner(r, 1); g.palette(false, static_cast<nana::color_rgb>(0x919191)); draw.corner(nana::rectangle(r).pare_off(1), 1); return true; } }; int main() { using namespace nana; form fm(API::make_center(300, 300)); fm.div("vert margin=15 <><btn weight=30><>"); button btn(fm, "Button"); fm["btn"] << btn; btn.set_bground(btn_bg()); fm.collocate(); fm.show(); nana::exec(); } ```` EDIT: Actually, I just realized that `bgcolor` is a method of class `widget`, not `button`. Looking at `widget.hpp` from version 0.7.2, I see that method `void background(nana::color_t);` exists, so you can probably use that. You can still use `button::set_bground` if you want to use two different colors for the gradient.
edited Jun 26 at 2:49 am

Thanks for this class.

I got errors when try to build:
'element': the symbol to the left of a '::' must be a type
'element_interface' : is not a member of 'nana'
etc...

Yes is more good to have gradient

Thanks for this class. I got errors when try to build: 'element': the symbol to the left of a '::' must be a type 'element_interface' : is not a member of 'nana' etc... Yes is more good to have gradient

Here's the code adapted for Nana 0.7.2:

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

class btn_bg : public nana::gui::element::element_interface
{
public:
    bool draw(nana::paint::graphics &g, 
        const nana::color_t, 
        const nana::color_t, 
        const nana::rectangle &r, 
        nana::gui::element_state::t state) override
    {
        // gradient colors for the bg, change them to whatever you like
        nana::color_t from = 0xfafad2, to = 0xf08080;

        // draw the custom background
        if(state != nana::gui::element_state::pressed)
            g.shadow_rectangle(r, from, to, true);
        else g.shadow_rectangle(r, to, from, true);

        // draw the frame like the library does
        int right = r.width - 1;
        int bottom = r.height - 1;
        g.line(1, 0, right - 1, 0, 0x7F7F7F);
        g.line(1, bottom, right - 1, bottom, 0x707070);
        g.line(0, 1, 0, bottom - 1, 0x7F7F7F);
        g.line(right, 1, right, bottom - 1, 0x707070);

        g.set_pixel(1, 1, 0x919191);
        g.set_pixel(right - 1, 1, 0x919191);
        g.set_pixel(right - 1, bottom - 1, 0x919191);
        g.set_pixel(1, bottom - 1, 0x919191);

        g.set_pixel(0, 0, nana::gui::color::button_face);
        g.set_pixel(right, 0, nana::gui::color::button_face);
        g.set_pixel(0, bottom, nana::gui::color::button_face);
        g.set_pixel(right, bottom, nana::gui::color::button_face);

        return true;
    }
};

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

    form fm(API::make_center(300, 300));

    button btn(fm);
    btn.move(nana::rectangle(15, 15, 100, 30));
    btn.caption(L"Button");
    btn.set_bground(btn_bg());

    fm.show();
    exec();
}
Here's the code adapted for Nana 0.7.2: ```` #include <nana/gui/wvl.hpp> #include <nana/gui/widgets/button.hpp> class btn_bg : public nana::gui::element::element_interface { public: bool draw(nana::paint::graphics &g, const nana::color_t, const nana::color_t, const nana::rectangle &r, nana::gui::element_state::t state) override { // gradient colors for the bg, change them to whatever you like nana::color_t from = 0xfafad2, to = 0xf08080; // draw the custom background if(state != nana::gui::element_state::pressed) g.shadow_rectangle(r, from, to, true); else g.shadow_rectangle(r, to, from, true); // draw the frame like the library does int right = r.width - 1; int bottom = r.height - 1; g.line(1, 0, right - 1, 0, 0x7F7F7F); g.line(1, bottom, right - 1, bottom, 0x707070); g.line(0, 1, 0, bottom - 1, 0x7F7F7F); g.line(right, 1, right, bottom - 1, 0x707070); g.set_pixel(1, 1, 0x919191); g.set_pixel(right - 1, 1, 0x919191); g.set_pixel(right - 1, bottom - 1, 0x919191); g.set_pixel(1, bottom - 1, 0x919191); g.set_pixel(0, 0, nana::gui::color::button_face); g.set_pixel(right, 0, nana::gui::color::button_face); g.set_pixel(0, bottom, nana::gui::color::button_face); g.set_pixel(right, bottom, nana::gui::color::button_face); return true; } }; int main() { using namespace nana::gui; form fm(API::make_center(300, 300)); button btn(fm); btn.move(nana::rectangle(15, 15, 100, 30)); btn.caption(L"Button"); btn.set_bground(btn_bg()); fm.show(); exec(); } ````
edited Jun 26 at 9:18 pm
30
views
5
replies
2
followers
live preview
enter atleast 10 characters
WARNING: You mentioned %MENTIONS%, but they cannot see this message and will not be notified
Saving...
Saved
All posts under this topic will be deleted ?
Pending draft ... Click to resume editing
Discard draft