GTK+容器管理----vbox

#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
GtkWidget* window;
GtkWidget* vbox;

GtkWidget* button1;
GtkWidget* button2;
GtkWidget* button3;
GtkWidget* button4;

//设置主窗体
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "测试容器管理");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

//添加按钮
button1 = gtk_button_new_with_label("按钮1");
button2 = gtk_button_new_with_label("按钮2");
button3 = gtk_button_new_with_label("按钮3");
button4 = gtk_button_new_with_label("按钮4");

//设置vbox容器布局管理器
vbox = gtk_vbox_new(TRUE, 5);//参数:按钮是否等大小,元素间距
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_box_pack_start(GTK_BOX(vbox), button1, TRUE, TRUE, 5);//参数:管理器、子元素、是否expand、是否fill,padding
gtk_box_pack_start(GTK_BOX(vbox), button2, TRUE, TRUE, 5);
gtk_box_pack_start(GTK_BOX(vbox), button3, TRUE, TRUE, 5);
gtk_box_pack_start(GTK_BOX(vbox), button4, TRUE, TRUE, 5);

//最终显示
gtk_widget_show_all(window);
gtk_main();
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *