使用gtk 2.0在窗口上绘图

时间:2015-10-01 07:34:22

标签: c interface gtk drawing gtk2

我有一个使用pthreads的程序。我想使用gtk2.0添加一个界面,但在窗口上绘图有问题。当我致电g_signal_connect(G_OBJECT(window), "configure_event",G_CALLBACK(draw_things), NULL);时 我绘制的线条出现并立即消失。如果我移动窗口调用功能 gtk_window_move (GTK_WINDOW(widget), 10,10); 或者通过鼠标,出现线条。如果我拨打g_signal_connect(G_OBJECT(window), "expose_event", G_CALLBACK(draw_things), NULL);我的线路显示正确,但不显示按钮和标签。我的代码如下所示:

    int main(int argc, char *argv[]){

        pthread_t tr1;
        pthread_t tr2;
        pthread_t *trs;
        int rc = 0;
        type j;
        int16_t *id;
        type Stop_Point;

        GtkWidget *window;
        GtkWidget *button;
        GtkWidget *table;
        GtkWidget *drawing_area;

        g_thread_init (NULL);
        gdk_threads_init ();
        gdk_threads_enter ();
        gtk_init (&argc, &argv);        

        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_widget_set_size_request(window, width/1.6, height/1.33);
        gtk_window_set_position(GTK_WINDOW(window), -1);

        gtk_window_set_title (GTK_WINDOW (window), "Window");
        g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (delete_event), NULL);
        gtk_container_set_border_width (GTK_CONTAINER (window), 2);
        table = gtk_table_new ( width/1.6, height/1.33, TRUE);
        gtk_container_add (GTK_CONTAINER (window), table);

        button = gtk_button_new_with_label ("next >>");
        gtk_table_attach_defaults (GTK_TABLE (table), button, 1110, 1190, 750, 790);
        gtk_widget_show (button);

        button = gtk_button_new_with_label ("<< prev");
        gtk_table_attach_defaults (GTK_TABLE (table), button, 1020, 1100 , 750, 790);
        gtk_widget_show (button);

        rc = pthread_create(&tr1, NULL, tr1_func, &id);
        rc = pthread_create(&Generator, NULL, tr2_func, &Stop_Point);
        trs = (pthread_t*)malloc(Num * sizeof(pthread_t));
        for(j = 0; j < Num; j++) {
             rc = pthread_create(trs + j, NULL, trs_func, id + j);
        }

//      g_signal_connect(G_OBJECT(window), "configure-event", GTK_SIGNAL_FUNC(draw_things), NULL);
        g_signal_connect(G_OBJECT(window), "expose_event", G_CALLBACK(draw_things), NULL);
        gtk_widget_show_all (window);
        gtk_main ();

        gdk_threads_leave ();

        return 0;
}

绘图的功能看起来像这样

gboolean draw_things(GtkWidget *widget, GdkEventExpose *event, gpointer data) {

    GdkGC *gc;

    gc=gdk_gc_new(GDK_DRAWABLE(widget->window));
    gdk_gc_set_line_attributes(gc, 5, GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
    gdk_draw_line(GDK_DRAWABLE(widget->window), gc, 5, 35, 1200, 35);
    gdk_draw_line(GDK_DRAWABLE(widget->window), gc, 1170, 45, 1200, 35);
    gdk_draw_line(GDK_DRAWABLE(widget->window), gc, 1170, 25 , 1200, 35);
//    gtk_window_move (GTK_WINDOW(widget), 1,1);

    return TRUE;
}

欢迎任何帮助,因为现在每次我需要绘制图片时,我都会使用“configure_event”并调用gtk_window_move (GTK_WINDOW(widget), 0,0);

0 个答案:

没有答案