刚开始接触Gtk+,需要使用它来画出绚丽的效果,现在是第一步,实现贴图及其PNG的透明效果。以后还需要实现非规则窗体,Easy拉,这都搞掂了 ~~~ 哈哈,开心 ^_~ !!!
废话少说,上代码:
#include
int main(int argc, char *argv[])
{
GtkWidget *window = NULL;
GtkWidget *fixed = NULL;
GdkPixbuf *pixbuf = NULL;
GdkPixmap *pixmap = NULL;
GtkWidget *eventbox;
GtkWidget *image;
GdkBitmap *bitmap;
gtk_init(&argc,&argv);
/* Create the window */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"ZQButton Demo");
gtk_widget_set_app_paintable(window,TRUE);
gtk_widget_realize (window);
//gtk_window_fullscreen(GTK_WINDOW(window));
gtk_widget_set_size_request(window, 800, 600);
g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
/* Set window background */
fixed = gtk_fixed_new();
gtk_container_add (GTK_CONTAINER(window), fixed);
//load background
pixbuf = gdk_pixbuf_new_from_file ("./bg.jpg",NULL);
pixmap = gdk_pixmap_new (fixed->window, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), -1);
gdk_pixbuf_render_to_drawable(pixbuf, pixmap,
fixed->style->fg_gc[GTK_STATE_NORMAL],
0,0,0,0,
gdk_pixbuf_get_width(pixbuf),
gdk_pixbuf_get_height(pixbuf),
GDK_RGB_DITHER_NORMAL,0,0);
g_object_unref(pixbuf);
//set background
gdk_window_set_back_pixmap (fixed->window, pixmap, FALSE);
gtk_widget_show(fixed);
/* Create the eventbox */
eventbox = gtk_event_box_new();
gtk_fixed_put (GTK_FIXED (fixed), eventbox, 20, 20);
pixbuf = gdk_pixbuf_new_from_file ("./test.png", NULL);
image = gtk_image_new_from_pixbuf (pixbuf);
//NOTE: Here is very important
gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
gtk_widget_shape_combine_mask(eventbox, bitmap, 0, 0);
gtk_container_add(GTK_CONTAINER(eventbox), image);
gtk_widget_show(image);
gtk_widget_show(eventbox);
gtk_widget_show(window);
gtk_main();
return FALSE;
}
不过png透明效果不怎么好,边缘有毛刺~~~再找找其他更好的办法
阅读(6069) | 评论(2) | 转发(0) |