qiankun11516 发表于 2014-11-14 21:20:53

【求助】gtk不能更新绘图区域,不知道错在哪里

我想在窗口化两个正方形,然后点一下BUTTON这两个正方形变化一下。程序大体都写好了。但是点了BUTTON之后不会更新,必须最小化一下窗口在还原才会更新,不知道问什么~~~
源代码:
#include <gtk/gtk.h>
#include <math.h>
#define DRAWING_AREA_WIDTH 400
#define DRAWING_AREA_HEIGHT 400
#define WINDOW_BORDER_WIDTH 5//有谁能告诉我gtk_container_set_border_width()这个函数有什么意义么?
GtkWidget *drawing_area;
    GtkWidget *window;
    GtkWidget *box1;
    GtkWidget *button;
int w = 10;
int h = 10;
gboolean
expose_event_callback (
    GtkWidget    *widget,
    GdkEventExpose    *event,
    gpointer    data)
{
    int    x1, y1, x2, y2;
    GdkColor    color;
    GdkDrawable    *canvas;
    GdkGC    *gc;
    canvas = widget->window;//不知道在主函数调用这个函数的时候,widget是怎么传递的,看来有时间又要好好看看g_signal_connect的实现机制了
    gc = widget->style->fg_gc;
    //w = 100;
    //h = 100;
    gdk_draw_rectangle (canvas, gc, FALSE, 50, 50, w, h); //画方
    gdk_draw_rectangle (canvas, gc, FALSE, 100, 100, w, h); //画方
    w=w+10;
    h=h+10;
    return TRUE;
}
gboolean
release ( GtkWidget *widget,
                   GdkEvent*event,
         gpointer   data )
{
          //printf("thks");
    int    x1, y1, x2, y2;
    GdkColor    color;
    GdkDrawable    *canvas;
    GdkGC    *gc;
    canvas = widget->window;//不知道在主函数调用这个函数的时候,widget是怎么传递的,看来有时间又要好好看看g_signal_connect的实现机制了
    gc = widget->style->fg_gc;
    //w = 100;
    //h = 100;
    gdk_draw_rectangle (canvas, gc, FALSE, 50, 50, w, h); //画方
    gdk_draw_rectangle (canvas, gc, FALSE, 100, 100, w, h); //画方
    w=w+10;
    h=h+10;
    return TRUE;
}
gint delete_event( GtkWidget *widget,
                   GdkEvent*event,
         gpointer   data )
{
    /* If you return FALSE in the "delete_event" signal handler,
*   * GTK will emit the "destroy" signal. Returning TRUE means
*   * you don't want the window to be destroyed.
*   * This is useful for popping up 'are you sure you want to quit?'
*   * type dialogs. */

    g_print ("delete event occurred/n");

    /* Change TRUE to FALSE and the main window will be destroyed with
*   * a "delete_event". */

    return FALSE;
}


void destroy( GtkWidget *widget,
            gpointer   data )
{
    gtk_main_quit ();
}

int main( int   argc,
          char *argv[] )
{
    /* GtkWidget is the storage type for widgets */

    /* This is called in all GTK applications. Arguments are parsed
   * from the command line and are returned to the application. */
    gtk_init (&argc, &argv);
    box1 = gtk_hbox_new(FALSE, 0);
    /* create a new window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new_with_label ("Button 1");
    gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (release), (gpointer) "button 1");
    /* When the window is given the "delete_event" signal (this is given
*   * by the window manager, usually by the "close" option, or on the
*   * titlebar), we ask it to call the delete_event () function
*   * as defined above. The data passed to the callback
*   * function is NULL and is ignored in the callback function. */
    g_signal_connect (G_OBJECT (window), "delete_event",
            G_CALLBACK (delete_event), NULL);

    /* Here we connect the "destroy" event to a signal handler.
*   * This event occurs when we call gtk_widget_destroy() on the window,
*   * or if we return FALSE in the "delete_event" callback. */
    g_signal_connect (G_OBJECT (window), "destroy",
            G_CALLBACK (destroy), NULL);

    /* Sets the border width of the window. */
    gtk_container_set_border_width (GTK_CONTAINER (window), WINDOW_BORDER_WIDTH);
    /* Set window title*/   
    gtk_window_set_title (GTK_WINDOW(window), "Drawing Area");

    drawing_area = gtk_drawing_area_new ();
    gtk_widget_set_size_request (drawing_area, DRAWING_AREA_WIDTH, DRAWING_AREA_HEIGHT);
    g_signal_connect (G_OBJECT(drawing_area), "expose_event", G_CALLBACK(expose_event_callback), NULL);
                                        //expose_event是什么东东,传递给expose_event的参数又是如何进行的?
    gtk_box_pack_start(GTK_BOX(box1), drawing_area, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
    gtk_container_add (GTK_CONTAINER (window), box1);
    //gtk_container_add (GTK_CONTAINER(window), drawing_area);
    gtk_widget_show_all(window);

    gtk_main ();


    return 0;
}



谢谢~~

ttttt 发表于 2014-11-17 21:16:58

页: [1]
查看完整版本: 【求助】gtk不能更新绘图区域,不知道错在哪里