1

我得到了一个使用 Cairo 上下文工作的绘制函数,最终结果应该是一个 GtkImage(没有中间图像创建)。我尝试使用 gdk_cairo_create 函数,但是这段代码:

...
GdkPixbuf *pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 22, 22);
GtkWidget *image = gtk_image_new_from_pixbuf (pixbuf);
GdkDrawable *drawable = image->window;
cairo_t *ctx = gdk_cairo_create (drawable);
my_cairo_paint_function (ctx);
...

失败:

Gdk-CRITICAL **:IA__gdk_cairo_create:断言“GDK_IS_DRAWABLE(可绘制)”失败

与一个简单的相同:

#include <gtk/gtk.h>
#include <cairo.h>

int
main (int argc, char *argv[])
{
  gtk_init(&argc, &argv);
  cairo_t *ctx = gdk_cairo_create (gtk_widget_get_window (gtk_image_new_from_file ("foobar.png")));
  gtk_main();
  return 0;
}

我不明白为什么这会失败。任何帮助表示赞赏!

4

1 回答 1

2

GtkImage doesn't have a GdkWindow, so the call to gtk_widget_get_window() or the access of widget->window returns NULL. You can put the GtkImage in a GtkEventBox and draw on the event box's GdkWindow.

Although, it looks like you're trying (with gdk_pixbuf_new) to create an empty space to draw on. In that case, GtkImage is not the widget you want -- use GtkDrawingArea. And don't forget to call your paint function in the handler for the event-expose signal!

于 2010-12-23T22:11:39.633 回答