I'm writig a simple file browser in GTK#. I want to draw a stock image of a directory inside DrawingArea. I don't want to load external image. I came up to use Gdk.PixBuf but can't figure out how to load stock images.

有帮助吗?

解决方案

There are a few ways of doing that, but I believe gtk_icon_theme_load_icon() is the one that is not deprecated and is compatible with most GTK+ versions. You will need a icon GtkIconTheme as a parameter, but in most cases gtk_icon_theme_get_default() is what you want. The icon name parameter is from Icon naming spec.

其他提示

Ok, so rendering stock items inside DrawingArea is a bit ricky.

private Gdk.GC gc;
private Gdk.Pixbuf pixbuf;

public MainWindow (): base (Gtk.WindowType.Toplevel)
{
    Build ();
    this.gc = new Gdk.GC((Gdk.Drawable)this.browserArea.GdkWindow);
//the method below does the trick. It is the only method I found that will return
//a valid pixbuf, so you can easily draw with the "usual" DrawPixbuf method in the 
//ExposeEvent
    this.pixbuf = RenderIcon(Gtk.Stock.Cdrom, IconSize.Dialog, "");
    this.browserArea.ExposeEvent += new ExposeEventHandler(OnExposeEvent);
}

protected void OnExposeEvent(object sender, ExposeEventArgs args){
    this.browserArea.GdkWindow.DrawPixbuf(this.gc, this.pixbuf,0,0,20,20,-1,-1,Gdk.RgbDither.None,0,0);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top