如何在窗口内调整窗口小部件的大小

时间:2011-11-24 05:11:54

标签: c gtk widget size

首先,我希望你做得很好。现在,我正在用C语言进行GTK游戏,我正在尝试调整Board所在的Box,我不确定我应该调整大小的是它所在的盒子还是按钮表本身。

我希望调整大小以便用户友好,请记住我是GTK的菜鸟。

#include<gtk/gtk.h>
#include<stdio.h>
#include<stdlib.h>

void boton_click (GtkWidget * widget, gpointer info);
void salida (Gtkdget * widget, gpointer info);
gboolean cierra_ventana (GtkWidget * widget, GdkEvent * event, gpointer info);

int
main (int argc, char *argv[])
{
    GtkWidget *ventana, *cajavertjugador1, *namejugador1, *imageplayer1,
    *framejugador1, *labeljugador1, *cajavertjugador2, *namejugador2,
    *imageplayer2, *framejugador2, *labeljugador2, *cajaH, *cajaboton,
    *separador, *separadorvjug1, *separadorvjug2, *cajatablero, *boton,
    *botoncancel, *tabla, *tablero[20][20];
    int i, j;
    gchar *arch1 = "troll.jpg";
    gchar *arch2 = "challenge.jpg";

    //***********************************1.-Initialize Enviroment
    gtk_init (&argc, &argv);

    //***************************************2.-Create Widgets and attributes
    imageplayer1 = gtk_image_new_from_file (arch1);
    imageplayer2 = gtk_image_new_from_file (arch2);
    tabla = gtk_table_new (20, 20, TRUE);
    ventana = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (ventana), "Pente");
    cajaH = gtk_hbox_new (FALSE, 25);
    cajavertjugador1 = gtk_vbox_new (FALSE, 0);
    cajatablero = gtk_hbox_new (TRUE, 500);
    cajavertjugador2 = gtk_vbox_new (TRUE, 5);
    cajaboton = gtk_hbox_new (TRUE, 5);
    boton = gtk_button_new_with_label ("Ok");
    botoncancel = gtk_button_new_with_label ("Cancel");
    separador = gtk_hseparator_new ();
    separadorvjug1 = gtk_vseparator_new ();
    separadorvjug2 = gtk_vseparator_new ();
    framejugador1 = gtk_frame_new ("Fichas Comidas");
    namejugador1 = gtk_label_new ("Variable Nombre Jugador1");
    labeljugador1 = gtk_label_new ("Aqui Imagenes de Las Fichas Comidas\n");
    gtk_label_set_justify (GTK_LABEL (labeljugador1), GTK_JUSTIFY_LEFT);
    framejugador2 = gtk_frame_new ("Fichas Comidas");
    namejugador2 = gtk_label_new ("Variable Nombre Jugador2");
    labeljugador2 = gtk_label_new ("Aqui Imagenes de Las Fichas Comidas\n");
    gtk_label_set_justify (GTK_LABEL (labeljugador1), GTK_JUSTIFY_LEFT);

    for (i = 0; i < 20; i++)
    {
        for (j = 0; j < 20; j++)
        {
            tablero[i][j] = gtk_button_new ();
        }
    }

    //**********************************3.Register Function Calls
    g_signal_connect (G_OBJECT (ventana), "delete_event",
            G_CALLBACK (cierra_ventana), NULL);
    g_signal_connect (G_OBJECT (ventana), "destroy", G_CALLBACK (salida), NULL);

    //***********************************4.-Hierarchy of Elements

    gtk_container_add (GTK_CONTAINER (ventana), cajaH);
    gtk_container_add (GTK_CONTAINER (cajaH), cajavertjugador1);
    gtk_container_add (GTK_CONTAINER (cajaH), separadorvjug1);
    gtk_container_add (GTK_CONTAINER (cajavertjugador1), namejugador1);
    gtk_container_add (GTK_CONTAINER (cajavertjugador1), imageplayer1);
    gtk_container_add (GTK_CONTAINER (framejugador1), labeljugador1);
    gtk_container_add (GTK_CONTAINER (cajavertjugador1), framejugador1);
    gtk_container_add (GTK_CONTAINER (cajatablero), tabla);
    gtk_container_set_focus_vadjustment (GTK_CONTAINER (cajatablero),
                       GTK_ADJUSTMENT (50));
    gtk_container_add (GTK_CONTAINER (cajaH), cajatablero);
    gtk_container_add (GTK_CONTAINER (cajaH), separadorvjug2);
    gtk_container_add (GTK_CONTAINER (cajavertjugador2), namejugador2);
    gtk_container_add (GTK_CONTAINER (cajavertjugador2), imageplayer2);
    gtk_container_add (GTK_CONTAINER (framejugador2), labeljugador2);
    gtk_container_add (GTK_CONTAINER (cajavertjugador2), framejugador2);
    gtk_container_add (GTK_CONTAINER (cajaH), cajavertjugador2);


    for (i = 0; i < 20; i++)
    {
        for (j = 0; j < 20; j++)
        {
            gtk_table_attach_defaults (GTK_TABLE (tabla), tablero[i][j], i,
                         i + 1, j, j + 1);
        }
    }
    //****************************5.Show the Widddgggeetss
    gtk_widget_show_all (ventana);

    //****************************6.Process Signals and Events
    gtk_main ();
    return 1;

}//main

gboolean
cierra_ventana (GtkWidget * widget, GdkEvent * event, gpointer info)
{
  return FALSE;
}

void
salida (GtkWidget * widget, gpointer info)
{
  gtk_main_quit ();
}

1 个答案:

答案 0 :(得分:1)

你要说的是不是很清楚,如果你的意思是你添加按钮的表似乎没有展开以适应分隔符之间那么它是由于事实上,在创建cajaH时,您指定的间距为25像素。尝试将其更改为较小的不同值或0,看看你是否得到你想要的。因为你正在使用默认包装的桌子和&amp;使用gtk_container_add打包将扩大&amp;配合。
当你使用GtkHBox&amp; GtkVBox gtk_box_pack_start&amp; gtk_box_pack_end会使用gtk_container_add更好地控制布局。有关信息,请参阅this question 使用gtk_container_set_focus_vadjustment时,您需要使用gtk_adjustment_new创建新的GtkAdjustment。将{50}作为GTK_ADJUSTMENT进行类型转换不是这样做的方法! 希望这有帮助!