这里的技巧是添加布局并将调整大小回调设置为不是窗口而是布局。它并不完美,有点脏,但可以。初始定位效果不佳,但仍有改进空间。必须检查 Gtk.Widget 和 Gtk.Containers 的请求、分配和自然大小,甚至使用 Gdk 方法。迟到了,希望这会引导您朝着正确的方向前进。
PS:我正在使用一个无尽的.png图像,但可以随意使用另一个,只需更改代码以反映它。
using Gtk;
public int main (string[] args) {
Gtk.Image image;
Gtk.Layout layout;
Gtk.Window window;
Gdk.Pixbuf pixbuf;
Gtk.init (ref args);
window = new Gtk.Window ();
layout = new Gtk.Layout ();
image = new Gtk.Image ();
try {
pixbuf = new Gdk.Pixbuf.from_file ("endless.png");
image = new Gtk.Image.from_pixbuf (pixbuf);
layout.put (image, 0,0);
window.add (layout);
layout.size_allocate.connect ((allocation) => {
print ("Width: %d Height: %d\n", allocation.width, allocation.height);
var pxb = pixbuf.scale_simple (allocation.width, allocation.height, Gdk.InterpType.BILINEAR);
image.set_from_pixbuf (pxb);
});
window.destroy.connect (Gtk.main_quit);
window.show_all ();
Gtk.main ();
return 0;
} catch (Error e) {
stderr.printf ("Could not load file...exit (%s)\n", e.message);
return 1;
}
}
编辑:
一个简单的开罗版本:
using Gtk;
using Cairo;
public int main (string[] args) {
Cairo.ImageSurface image;
image = new Cairo.ImageSurface.from_png ("endless.png");
Gtk.init (ref args);
var window = new Gtk.Window ();
var darea = new DrawingArea ();
window.add (darea);
window.show_all ();
darea.draw.connect ((cr) => {
float xscale;
float yscale;
cr.save ();
xscale = (float) darea.get_allocated_width () / image.get_width ();
yscale = (float) darea.get_allocated_height () / image.get_height ();
cr.scale (xscale, yscale);
cr.set_source_surface (image, 0, 0);
cr.paint ();
cr.restore ();
return true;
});
window.destroy.connect (Gtk.main_quit);
Gtk.main ();
return 0;
}
编辑 2:我创建了另一个版本来在 2 个图像之间切换,并检查是否在执行此操作时多次检查内存是否增加,但事实并非如此。添加了几个框,并添加了 2 个按钮。
using Gtk;
using Cairo;
public int main (string[] args) {
Cairo.ImageSurface image;
image = new Cairo.ImageSurface.from_png ("endless.png");
Gtk.init (ref args);
var window = new Gtk.Window ();
var box1 = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
var box2 = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
var b1 = new Gtk.Button.with_label ("Image1");
var b2 = new Gtk.Button.with_label ("Image2");
box2.pack_start (b1, true, true, 0);
box2.pack_end (b2, true, true, 0);
var darea = new DrawingArea ();
box1.pack_start (box2, false, false, 0);
box1.pack_end (darea, true, true, 0);
window.add (box1);
window.show_all ();
b1.clicked.connect (() => {
image = new Cairo.ImageSurface.from_png ("endless.png");
darea.queue_draw ();
});
b2.clicked.connect (() => {
image = new Cairo.ImageSurface.from_png ("Gnome-logo.png");
darea.queue_draw ();
});
darea.draw.connect ((cr) => {
float xscale;
float yscale;
cr.save ();
xscale = (float) darea.get_allocated_width () / image.get_width ();
yscale = (float) darea.get_allocated_height () / image.get_height ();
cr.scale (xscale, yscale);
cr.set_source_surface (image, 0, 0);
cr.paint ();
cr.restore ();
return true;
});
window.destroy.connect (Gtk.main_quit);
Gtk.main ();
return 0;
}