how to change the background color of Fl_Window by

2019-08-29 12:41发布

Can anyone tell me, how to change the background color of Fl_Window by pressing Fl_Button. I'm trying to do it this way but it's not working.

void new_color(Fl_Widget* w, void*){
Fl_Button* b = (Fl_Button*)w;
b->parent()->color(FL_RED);
}


int main()
}
Fl_Window* win = new Fl_Window(...);
win->color(FL_WHITE);
win->begin();

Fl_Button* but = new Fl_Button(...);
but->callback(new_color);

win->end();
...
}

thanks!!!

标签: c++ linux fltk
1条回答
啃猪蹄的小仙女
2楼-- · 2019-08-29 12:55

As pointed by cup, redraw does the trick:

#include <FL/Fl.H> 
#include <FL/Fl_Button.H>
#include <FL/Fl_Window.H>

int main()
{
    Fl_Window win(200, 200, "Color changer");
    win.color(FL_WHITE);

    Fl_Button but(50, 80, 100, 20, "Click Me!");
    but.callback([](Fl_Widget* w, void*) {
        Fl_Widget* p = w->parent();
        p->color(p->color() == FL_WHITE ? FL_RED : FL_WHITE);
        p->redraw();
    });

    win.show();
    return Fl::run();
}
查看更多
登录 后发表回答