1 事件信号expose-event
窗口状态(移动、初始化、点击按钮等等)改变,只要窗口有变化,都会自动触发事件信号expose-event
。
可以使用函数gtk_widget_queue_draw
触发信号,刷新构件区域或者指定区域。如果这样叙述,该函数会触发expose-event信号,但是实际测试中,我并没有看到命令行窗口输出expose-event被触发信息
。
2 总结
gtk_widget_queue_draw
函数不能放在expose-event
中,应该是该函数会触发expose-event
信号,造成两者来回调用,死循环。
#include<cairo.h>
#include<gtk/gtk.h>
gint startx = 1;
gint w = 400;
gint h = 300;
gboolean
on_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data){
/* 如果给窗口绘图、窗口本身不能绘图
* 窗口本质上是一个结构体(里面有window成员)
* window成员才是真正的绘图区域
*/
gtk_widget_get_window(widget);
cairo_t *cr = gdk_cairo_create(widget->window);
GdkPixbuf *src_pixbuf = gdk_pixbuf_new_from_file("/home/lieryang/Desktop/gtk-3.0-study/05 river/wallpaper.jpeg", NULL);
GdkPixbuf *dst_pixbuf = gdk_pixbuf_scale_simple(src_pixbuf, w, h,
GDK_INTERP_BILINEAR);
gdk_cairo_set_source_pixbuf(cr, dst_pixbuf, 0, 0);
/* 绘图 */
cairo_paint(cr);
g_object_unref(dst_pixbuf);
g_object_unref(src_pixbuf);
/* 画apple */
src_pixbuf = gdk_pixbuf_new_from_file("apple.svg", NULL);
dst_pixbuf = gdk_pixbuf_scale_simple(src_pixbuf, 80, 100,
GDK_INTERP_BILINEAR);
gdk_cairo_set_source_pixbuf(cr, dst_pixbuf, startx, 100);
/* 绘图 */
cairo_paint(cr);
g_object_unref(dst_pixbuf);
g_object_unref(src_pixbuf);
cairo_destroy(cr);
g_print("expose-event\n");
return FALSE;
}
void
deal_button_clicked(GtkWidget *widget,
gpointer data){
startx += 3;
if(startx >= w){
startx = 0;
}
/* 更新刷图区域、刷新整个窗口 */
gtk_widget_queue_draw(GTK_WIDGET(data));
g_print("timeout\n");
}
gboolean timeout(gpointer user_data){
deal_button_clicked(NULL, user_data);
/*如果返回FALSE会自动销毁该函数*/
return TRUE;
}
int
main(int argc, char *argv[]){
gtk_init (&argc, &argv);
GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_set_size_request(window, 400, 300);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
GtkWidget *table = gtk_table_new(5, 5, TRUE);
gtk_container_add(GTK_CONTAINER(window), table);
GtkWidget *button = gtk_button_new_with_label("click me");
gtk_table_attach_defaults(GTK_TABLE(table), button, 3, 4, 4, 5);
gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
g_signal_connect(button, "clicked",
G_CALLBACK(deal_button_clicked), window);
g_signal_connect(window, "expose-event",
G_CALLBACK(on_expose_event), window);
/* 允许窗口绘图 */
gtk_widget_set_app_paintable(window, TRUE);
gtk_widget_show_all(window);
g_timeout_add(1, timeout, window);
gtk_main();
return 0;
}