Chinaunix首页 | 论坛 | 博客
  • 博客访问: 293300
  • 博文数量: 34
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 433
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-23 13:16
文章分类

全部博文(34)

文章存档

2011年(3)

2010年(4)

2009年(6)

2008年(21)

我的朋友

分类: LINUX

2008-09-17 15:44:11

实现非规则按钮的不同状态图片切换。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;
}


文件:zqbutton_button_release_enter_leave_press.tar.gz
大小:276KB
下载:下载


阅读(5030) | 评论(7) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-03-19 17:29:03

请博主帮我检查一下。我想实现移到按键时,按键上的图片有变化。但是一直实现不了,请高手帮忙看下哦,很急的,谢谢帮忙。 void on_com_button_enter (GtkWidget *button, GtkWidget *main_textview) { GtkWidget *smc_main_win; smc_main_win = create_smc_main_win(); GtkWidget *image_com; image_com = lookup_widget(GTK_WIDGET(smc_main_win), "image9"); gtk_image_set_from_file(GTK_IMAGE(image_com),"com_button2.png"); gtk_widget_show(image_com); } int main (int argc, char *argv[]) { 。。。。。 GtkWidget *image_com; image_com = look

chinaunix网友2009-08-06 14:27:33

用rc文件实现更容易啊

binghuiliang2008-12-19 22:01:46

说明一下: 非规则按扭的意思是把png画再window上,而没有button的规矩边框. 因此如果你没有看见任何东西的话, 也许load image失效.

binghuiliang2008-12-19 21:59:42

本代码不考虑优化了, 本就是一个学习中间代码, 忘见量...

mycodenum2008-11-17 14:34:54

文章的标题是“...非规则的按钮...”,可是实际上并不是一个按钮的东东,现在我将上面的代码修改了,希望与你探讨或者向你学习。 上代码: #include void widget_modify_change(GtkWidget *widget, gchar *str) { GtkWidget *oldImage = GTK_WIDGET(gtk_container_children(GTK_CONTAINER(widget))->data); #if 1 gtk_image_set_from_file(GTK_IMAGE(oldImage), str); #else GdkPixbuf *pixbuf; GdkPixmap *pixmap; GdkBitmap *bitmap; GtkWidget *newImage; gtk_container_remove(GTK_CONTAINER(widget), oldImage); pixbuf = gdk_pixbuf_new_from_file (str,