GTK+容器管理--GtkAlignment

不像vbox等,GtkAlignment中只能放入一个Widget,但是它可以按照百分比控制这个元素处于Align元素的哪个位置。
示例代码如下:
最终的效果是:OK和Cancel始终处于窗体右下角。

#include <gtk/gtk.h>

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

GtkWidget* btn1;
GtkWidget* btn2;

//设置主窗体
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "测试容器管理--Align");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);
gtk_container_set_border_width(GTK_CONTAINER(window), 20);//窗体边框4

g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

//GtkAlignment主要是用来按照百分比对齐其内元素位置的,一个align能放置一个元素。
align = gtk_alignment_new(0.95, 0.95, 0.04, 0.04);//子元素将添加到align的右下角
gtk_container_add(GTK_CONTAINER(window), align);

//添加hbox
hbox = gtk_hbox_new(TRUE, 5);
gtk_container_add(GTK_CONTAINER(align), hbox);

//添加按钮
btn1 = gtk_button_new_with_label("OK");
gtk_widget_set_size_request(btn1, 80, 40);
btn2 = gtk_button_new_with_label("Cancel");
gtk_widget_set_size_request(btn2, 80, 40);
gtk_container_add(GTK_CONTAINER(hbox), btn1);
gtk_container_add(GTK_CONTAINER(hbox), btn2);

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

Leave a Reply

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