测试环境是ubuntu15.04,使用的图形库是gtk+。
图片格式采用的是X11的xpm格式,首先从网上找一个背景透明的png图片,然后用图形转换工具(比如gimp)转换为xpm格式。
程序代码如下:
#include <gtk/gtk.h>
void close_app(GtkWidget *widget,GdkEvent *event,gpointer data){
gtk_main_quit();
}
int main(int argc,char **argv){
GtkWidget *window,*pixmap,*fixed;
GdkPixmap *gdk_pixmap;
GdkBitmap *mask;
GtkStyle *style;
GdkGC *gc;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_POPUP);
gtk_signal_connect(GTK_OBJECT(window),"delete_event",
GTK_SIGNAL_FUNC(close_app),NULL);
gtk_widget_set_events(window,gtk_widget_get_events(window)|GDK_BUTTON_PRESS_MASK);
gtk_signal_connect(GTK_OBJECT(window),"button_press_event",
GTK_SIGNAL_FUNC(close_app),NULL);
gtk_widget_show(window);
style = gtk_widget_get_default_style();
gc = style->black_gc;
gdk_pixmap = gdk_pixmap_create_from_xpm(window->window,&mask,
&style->bg[GTK_STATE_NORMAL],"./start.xpm");
pixmap = gtk_pixmap_new(gdk_pixmap,mask);
gtk_widget_show(pixmap);
fixed = gtk_fixed_new();
gtk_widget_set_usize(fixed,256,256);
gtk_fixed_put(GTK_FIXED(fixed),pixmap,0,0);
gtk_container_add(GTK_CONTAINER(window),fixed);
gtk_widget_shape_combine_mask(window,mask,0,0);
gtk_widget_set_uposition(window,20,400);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
编译命令:
clang `pkg-config --libs --cflags gtk+-2.0` -O3 -g0 -s -o pixmap pixmap.c
执行效果如下:
时间: 2024-11-17 22:16:49