首页 > 代码库 > GTK3学习笔记3 Packing

GTK3学习笔记3 Packing

其实在不同平台上的应用程序,其常用控件往往都是类似的。

比如按钮控件、布局方式等等,在Android、ios、Windows、Linux上都是相近的,方法也类似。这次的笔记涉及到GTK3中对控件的一种布局方式:Packing。

其效果如下:

    

该窗口使用Packing,实现了三个按钮控件的布局。代码如下:

#include <gtk/gtk.h>

static void print_hello (GtkWidget *widget, gpointer   data)
{
    g_print ("Hello World\n");
}

int main (int  argc, char *argv[])
{
    GtkWidget *window, *grid, *button;
    gtk_init (&argc, &argv);

    /*创造一个新窗口window,并且设置标题等*/ 
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "Grid");
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    /*现在我们构建一个 pack 我们按钮的 container*/
    grid = gtk_grid_new ();
    /* 将这个container pack 在我们的窗口window中*/
    gtk_container_add (GTK_CONTAINER (window), grid);

    button = gtk_button_new_with_label ("Button 1");
    g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
    /*将button放置在距左端0,距上边0的位置,设置宽为1,高为1.*/
    gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
    /* 函数声明
     void  gtk_grid_attach  (GtkGrid  *grid, GtkWidget *child,
                            gint left,  gint top,  gint width,  gint height);
    */

    button = gtk_button_new_with_label ("Button 2");
    g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
    gtk_grid_attach (GTK_GRID (grid), button, 1, 0, 1, 1);

    button = gtk_button_new_with_label ("Quit");
    g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
    gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 2, 1);

    gtk_widget_show_all (window);
    gtk_main ();
    return 0;
}

先写到这儿。



GTK3学习笔记3 Packing