在我们使用gtk这个工具时,有时想在同一个窗口中,根据选择来显示不同的操作菜单,这篇博文主要是解决此类问题
//创建窗口 GtkWidget *CreateMenuMain() { GtkWidget *window; GtkWidget *button; GtkWidget *fixed; window = gtk_window_new(GTK_WINDOW_TOPLEVEL); char title[] = "test"; gtk_window_set_title(window,title); gtk_widget_set_usize(GTK_WINDOW(window),500,400); change_background(window,500,400,"play1.jpg"); gtk_signal_connect(GTK_OBJECT(window),"delete_event",G_CALLBACK(gtk_ main_quit),NULL); button = gtk_button_new_with_label("切换菜单"); gtk_widget_set_usize(GTK_BUTTON(button),100,100) fixed = gtk_fixed_new(); gtk_fixed_put(GTK_FIXED(fixed),button); gtk_container_add(GTK_CONTAINER(window),fixed); gtk_fixed_put(GTK_FIXED(fixed),button,250,200); gtk_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(button_deal),fixed); gtk_widget_show_all(window); } //按钮的回调函数 void button_deal(GtkWidget *button,gpointer) { GtkFixed *fixed_del = (GtkFixed *)data//进行类型强转 gtk_container_remove(GTK_CONTAINER(window),fixed_del); GtkWidget *fixed; fixed = gtk_fixed_new(); gtk_container_add(GTK_CONTAINER(window),fixed); gtk_widget_show_all(window); }
其实也很简单,就是用gtk_container_remove()函数将原本的构件移除,再向窗口中添加新的构件。希望能帮你解决问题。