Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1146072
  • 博文数量: 309
  • 博客积分: 6093
  • 博客等级: 准将
  • 技术积分: 3038
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-03 17:14
个人简介

linux学习记录

文章分类

全部博文(309)

文章存档

2014年(2)

2012年(37)

2011年(41)

2010年(87)

2009年(54)

2008年(88)

分类:

2009-06-04 11:26:47

Gtk显示不规则按钮

分两步:1,建立gtk_event_box, 将按钮图片加入到这个容器中;
        2,获取按钮图片的掩码mask,和event_box相关联。
      这样,按钮以外的部分就不会再显示出来。

#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),"User Define Button  Demo");
    gtk_widget_set_app_paintable(window,TRUE);
    gtk_widget_realize (window);

    gtk_widget_set_size_request(window, 400, 300);
    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);

    eventbox = gtk_event_box_new();

    //load background
    pixbuf = gdk_pixbuf_new_from_file ("/bg.jpg",NULL);
    image = gtk_image_new_from_pixbuf (pixbuf);

    pixmap = gdk_pixmap_new (window->window, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), -1);
    gdk_pixbuf_render_to_drawable(pixbuf, pixmap,
                window->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 (window->window, pixmap, FALSE);

      // Load button
      pixbuf = gdk_pixbuf_new_from_file ("/button_auto.png",NULL);
    image = gtk_image_new_from_pixbuf (pixbuf);

      gtk_container_add(GTK_CONTAINER(eventbox), image);
      gtk_fixed_put (GTK_FIXED (fixed), eventbox, 50,50);
   
    //  Set button mask
      gdk_pixbuf_render_pixmap_and_mask(pixbuf, NULL, &bitmap, 255);
      gtk_widget_shape_combine_mask(eventbox, bitmap, 0, 0);

      gtk_widget_show_all(window);
      gtk_main();
        return 0;
}

 

//////////////http://blog.sina.com.cn/s/blog_4fa676f60100cgwm.html

 

 

 

#include

gint mouse_event_handle(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
 GdkPixbuf *pixbuf;
 GdkPixmap *pixmap;
 GdkBitmap *bitmap;
 GtkWidget *oldImage;
 GtkWidget *newImage;
 switch(event->button) {
  case 1:
   printf("Left ");
   break;
  case 2:
   printf("Middle ");
   break;
  case 3:
   printf("Right ");
   break;
  default:
   printf("Unknown ");
 }
 
 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);
 
 
 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);
 pixmap = gdk_pixmap_new (fixed->window, 800, 600, -1);

// 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_widget_shape_combine_mask(window, bitmap, 10, 10);
 gtk_container_add(GTK_CONTAINER(eventbox), image);
 gtk_widget_show(image);
 gtk_widget_show(eventbox);
 
 gtk_widget_show(window);
 gtk_main();
 return FALSE;
}

在开源项目openmoko的源代 *** 中有这么一种方法可以实现不规则窗体,但只能是没有修饰的窗体,实现方法大概是:

1。创建一个目标窗体大小的png图片,这个图片包含alpha通道,你想保留的地方填充上数据,不想要的地方的象素设置为透明;

2。创建窗口,以png图片作为窗口背景;

GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file (filename);
GtkWidget *self = gtk_window_new (GTK_WINDOW_POPUP);

GdkPixmap *pixmap;
GdkBitmap *bitmap;

gdk_pixbuf_render_pixmap_and_mask (pixbuf, &pixmap, &bitmap, 128 );
gtk_widget_shape_combine_mask (self, bitmap, 0, 0);

GtkStyle *style = gtk_style_copy (self->style);
if (style->bg_pixmap[GTK_STATE_NORMAL])
  g_object_unref (style->bg_pixmap[GTK_STATE_NORMAL]);

style->bg_pixmap[GTK_STATE_NORMAL] = g_object_ref (pixmap);

gtk_widget_set_style (self, style);

gtk_widget_show (self);


以上代 *** 仅供参考。

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