实现非规则按钮的不同状态图片切换。PNG透明效果的按钮
只是一些技术验证的代码,自己去好好封装一下就ok拉。
#include
gint mouse_event_handle(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
switch(event->button) {
case 1:
printf("Left ");
break;
case 2:
printf("Middle ");
break;
case 3:
printf("Right ");
break;
default:
printf("Unknown ");
}
GdkPixbuf *pixbuf;
GdkPixmap *pixmap;
GdkBitmap *bitmap;
GtkWidget *oldImage;
GtkWidget *newImage;
switch(event->type){
case GDK_BUTTON_PRESS:
printf("Mouse button press at (%.2f, %.2f)\n", event->x, event->y);
oldImage = GTK_WIDGET(gtk_container_children(GTK_CONTAINER(widget))->data);
gtk_object_ref(GTK_OBJECT(oldImage));
gtk_container_remove(GTK_CONTAINER(widget), oldImage);
pixbuf = gdk_pixbuf_new_from_file ("./press.png", NULL);
newImage = gtk_image_new_from_pixbuf (pixbuf);
gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
gtk_widget_shape_combine_mask(widget, bitmap, 0, 0);
gtk_container_add(GTK_CONTAINER(widget), newImage);
gtk_widget_show(newImage);
break;
case GDK_BUTTON_RELEASE:
printf("Mouse button release at (%.2f, %.2f)\n", event->x, event->y);
oldImage = GTK_WIDGET(gtk_container_children(GTK_CONTAINER(widget))->data);
gtk_object_ref(GTK_OBJECT(oldImage));
gtk_container_remove(GTK_CONTAINER(widget), oldImage);
pixbuf = gdk_pixbuf_new_from_file ("./enter.png", NULL);
newImage = gtk_image_new_from_pixbuf (pixbuf);
gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
gtk_widget_shape_combine_mask(widget, bitmap, 0, 0);
gtk_container_add(GTK_CONTAINER(widget), newImage);
gtk_widget_show(newImage);
break;
case GDK_ENTER_NOTIFY:
printf("Mouse enter.\n");
oldImage = GTK_WIDGET(gtk_container_children(GTK_CONTAINER(widget))->data);
gtk_object_ref(GTK_OBJECT(oldImage));
gtk_container_remove(GTK_CONTAINER(widget), oldImage);
pixbuf = gdk_pixbuf_new_from_file ("./enter.png", NULL);
newImage = gtk_image_new_from_pixbuf (pixbuf);
gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
gtk_widget_shape_combine_mask(widget, bitmap, 0, 0);
gtk_container_add(GTK_CONTAINER(widget), newImage);
gtk_widget_show(newImage);
break;
case GDK_LEAVE_NOTIFY:
printf("Mouse leave.\n");
oldImage = GTK_WIDGET(gtk_container_children(GTK_CONTAINER(widget))->data);
gtk_object_ref(GTK_OBJECT(oldImage));
gtk_container_remove(GTK_CONTAINER(widget), oldImage);
pixbuf = gdk_pixbuf_new_from_file ("./leave.png", NULL);
newImage = gtk_image_new_from_pixbuf (pixbuf);
gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, &bitmap, 128);
gtk_widget_shape_combine_mask(widget, bitmap, 0, 0);
gtk_container_add(GTK_CONTAINER(widget), newImage);
gtk_widget_show(newImage);
break;
default:
printf("\n");
break;
}
return FALSE;
}
int main(int argc, char *argv[])
{
GtkWidget *window = NULL;
GdkPixbuf *pixbuf = NULL;
GdkPixmap *pixmap = NULL;
GdkBitmap *bitmap = NULL;
GtkWidget *image = NULL;
GtkWidget *eventbox = NULL;
GtkWidget *fixed = NULL;
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_events(window, GDK_SCROLL_MASK);
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);
fixed = gtk_fixed_new();
gtk_container_add (GTK_CONTAINER(window), fixed);
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);
gdk_window_set_back_pixmap (fixed->window, pixmap, FALSE);
gtk_widget_show(fixed);
eventbox=gtk_event_box_new();
gtk_widget_set_events(eventbox, GDK_MOTION_NOTIFY | GDK_BUTTON_PRESS | GDK_BUTTON_RELEASE
| GDK_ENTER_NOTIFY | GDK_LEAVE_NOTIFY);
g_signal_connect(G_OBJECT(eventbox), "button_press_event", GTK_SIGNAL_FUNC(mouse_event_handle), NULL);
g_signal_connect(G_OBJECT(eventbox), "button_release_event", GTK_SIGNAL_FUNC(mouse_event_handle), NULL);
g_signal_connect(G_OBJECT(eventbox), "enter_notify_event", GTK_SIGNAL_FUNC(mouse_event_handle), NULL);
g_signal_connect(G_OBJECT(eventbox), "leave_notify_event", GTK_SIGNAL_FUNC(mouse_event_handle), NULL);
gtk_fixed_put (GTK_FIXED (fixed), eventbox, 10, 10);
pixbuf = gdk_pixbuf_new_from_file ("./leave.png", NULL);
image = gtk_image_new_from_pixbuf (pixbuf);
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;
}
阅读(1544) | 评论(0) | 转发(0) |