fltk中如何在不同的静态函数间传递数据
时间: 2023-05-28 18:05:49 浏览: 50
在FLTK中,可以使用全局变量来在不同的静态函数间传递数据。 全局变量可以在任何函数内部和外部访问,因此可以在一个函数中设置它并在另一个函数中使用它。 例如:
```c++
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>
// 定义全局变量
int counter = 0;
// 回调函数1
void cb_button1(Fl_Widget* w, void* data) {
counter = 1;
// ...
}
// 回调函数2
void cb_button2(Fl_Widget* w, void* data) {
if (counter == 1) {
// ...
}
}
int main() {
Fl_Window* win = new Fl_Window(200, 200, "FLTK Example");
Fl_Button* button1 = new Fl_Button(50, 50, 100, 50, "Button 1");
button1->callback(cb_button1);
Fl_Button* button2 = new Fl_Button(50, 110, 100, 50, "Button 2");
button2->callback(cb_button2);
win->end();
win->show();
return Fl::run();
}
```
在上面的示例中,我们使用了一个名为`counter`的全局变量来在两个回调函数之间传递数据。 当用户单击`button1`时,我们将`counter`设置为1。 在`cb_button2`函数中,我们检查`counter`的值是否为1,如果是,则执行一些操作。