接下来是cairo
用于绘制某些东西的小代码。问题是cairo绘制的canvas大于gtk的scrolled_window的面积。但是 scrolled_window 不滚动,所以我只能看到画布的一部分。
有什么建议吗?我的环境:MinGW + gtk2.10.11。
#include <gtk/gtk.h>
static gboolean draw_cb (GtkWidget *widget, GdkEventExpose *event)
{
cairo_t *cr;
cr = gdk_cairo_create (widget->window);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr);
cairo_set_source_rgb (cr, 0.42, 0.65, 0.80);
cairo_set_line_width (cr,6);
cairo_rectangle (cr, 30, 30, 100, 100);
cairo_stroke (cr);
cairo_destroy(cr);
return FALSE;
}
int main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
GtkWidget *window;
GtkWidget *sw;
GtkWidget *cr;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_policy((GtkWindow *)window, FALSE, FALSE, FALSE);
sw = gtk_scrolled_window_new (NULL,NULL);
gtk_widget_set_usize(sw, 100, 100);
cr = gtk_drawing_area_new();
gtk_scrolled_window_add_with_viewport(GTK_CONTAINER(sw), cr);
gtk_container_add (GTK_CONTAINER(window), sw);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (cr, "expose-event", G_CALLBACK(draw_cb), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}