Username: Password:

gtk透明背景和不规则按钮
来源:ChinaUnix博客  作者:ChinaUnix博客   发布时间:2008-01-01 18:46:00





/*
* Copyright (C) 1998-2006  
panhuachun@hotmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*/
#include
GtkWidget *window1;
GdkPixmap *pixmap;
static gboolean button_press_callback (GtkWidget      *event_box,
                         GdkEventButton *event,
                         gpointer        data)
{
    g_print ("Event box pressed at coordinates %f,%f\n",
            event->x, event->y);
     gtk_widget_show_all (window1);
    return TRUE;
}
static gboolean  button_release_callback (GtkWidget      *event_box,
                         GdkEventButton *event,
                         gpointer        data)
{
    g_print ("Event box released at coordinates %f,%f\n",
            event->x, event->y);
    gtk_widget_hide (window1);
    return TRUE;
}

GdkPixmap *load_pixmap (GtkWidget *widget,gchar *filename)
{
GdkPixbuf *pixbuf=NULL;
GdkPixmap *pixmap=NULL;
GdkBitmap *window_shape_bitmap;
pixbuf = gdk_pixbuf_new_from_file (filename,NULL);
pixmap = gdk_pixmap_new (widget->window,gdk_pixbuf_get_width(pixbuf),gdk_pixbuf_get_height(pixbuf),-1);

gdk_pixbuf_render_to_drawable (pixbuf,pixmap,
   widget->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);
return pixmap;
}

int main( int   argc,         char *argv[] )
{
  /* GtkWidget is the storage type for widgets */
  gchar *xpm_file = "caomei.xpm";
  GtkWidget *window;
   //GtkWidget *window1;
  GtkWidget *fixed,*fixed1;
  GtkWidget *event_box, *pixmap_widget ;
  GdkPixmap *bg;
GdkBitmap *gdk_pixmap_mask;
GdkPixmap *gdk_pixmap;
  GtkStyle *style;
  GdkGC *gc;
  GdkColormap *colormap;
  GdkBitmap *window_shape_bitmap;
  GdkColor black;
  GdkColor white;

  gtk_init (&argc, &argv);   
  style = gtk_widget_get_default_style ();
  
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW(window),"test");
gtk_widget_set_events (window,GDK_SCROLL_MASK);
gtk_widget_set_app_paintable(window,TRUE);
gtk_window_set_position (GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);
gtk_widget_set_size_request (window,500,400);
  gtk_widget_realize (window);
   g_signal_connect (G_OBJECT (window), "destroy",
      G_CALLBACK (gtk_main_quit), NULL);
//window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
window1 = gtk_window_new (GTK_WINDOW_POPUP);
gtk_window_set_title (GTK_WINDOW(window1),"test");
gtk_widget_set_events (window1,GDK_SCROLL_MASK);
gtk_widget_set_app_paintable(window1,TRUE);
gtk_window_set_position (GTK_WINDOW(window1),GTK_WIN_POS_CENTER_ALWAYS);
gtk_widget_set_size_request (window1,500,400);
  gtk_widget_realize (window1);
   g_signal_connect (G_OBJECT (window1), "destroy",
      G_CALLBACK (gtk_main_quit), NULL);

fixed = gtk_fixed_new();
gtk_container_add (GTK_CONTAINER(window),fixed);
gtk_widget_show (fixed);
fixed1 = gtk_fixed_new();
gtk_container_add (GTK_CONTAINER(window1),fixed1);
gtk_widget_show (fixed1);
//setup the background
bg = load_pixmap (fixed,"123.jpg");
gdk_window_set_back_pixmap (fixed->window,bg,FALSE);
event_box=gtk_event_box_new();
gtk_event_box_set_visible_window(event_box,FALSE);
      gtk_fixed_put (GTK_FIXED (fixed), event_box, 100,100);
     g_signal_connect (G_OBJECT (event_box),
            "button_press_event",
            G_CALLBACK (button_press_callback),
            "./1.png");
      g_signal_connect (G_OBJECT (event_box),
          "button_release_event",
            G_CALLBACK (button_release_callback),
          "./m.png");
    pixmap_widget = gtk_image_new_from_file("m.png");
    //gtk_container_add (GTK_CONTAINER (event_box), pixmap_widget);

//************为window创建window_shape_bitmap******************************
       colormap = gdk_colormap_get_system ();
      gdk_color_black (colormap, & black);
      gdk_color_white (colormap, & white);
     window_shape_bitmap = (GdkBitmap *) gdk_pixmap_new (NULL, 500, 400, 1);
      gc = gdk_gc_new (window_shape_bitmap);
    gdk_gc_set_foreground (gc, & black);
    gdk_gc_set_background (gc, & white);
    gdk_draw_rectangle (window_shape_bitmap, gc, TRUE, 0, 0,500, 400);
      gdk_gc_set_foreground (gc, & white);
      gdk_gc_set_background (gc, & black);
     // 在window_shape_bitmap中画一个填充的圆
     gdk_draw_arc (window_shape_bitmap, gc,TRUE,
                  70, 70, 200,200, 0, 360*64);
  
     // 在window_shape_bitmap中画一个填充的圆
       gdk_draw_arc (window_shape_bitmap, gc,TRUE,
                 250, 90, 150,150, 0, 360*64);
    gtk_widget_shape_combine_mask (window, window_shape_bitmap, 0, 0);
   gtk_container_add (GTK_CONTAINER (event_box), pixmap_widget);
     //gtk_widget_shape_combine_mask (event_box, window_shape_bitmap, 0, 0);
     //  eventbox在这儿为什么不行呢?   
//***************************************************************************

//****利用已有的图片形状为window  gdk_pixmap_mask *******************
     gdk_pixmap = gdk_pixmap_create_from_xpm (event_box->window, &gdk_pixmap_mask,
            &style->bg[GTK_STATE_NORMAL],
            xpm_file);// "./m.png");
     pixmap = gtk_image_new_from_pixmap (gdk_pixmap, gdk_pixmap_mask);
     gtk_widget_show (pixmap);
     gtk_fixed_put (GTK_FIXED (fixed1), pixmap, 100,100);
     //gtk_widget_shape_combine_mask (window, gdk_pixmap_mask, 100, 100);
    gtk_widget_shape_combine_mask (window1, gdk_pixmap_mask, 100,100);
//***************************************************************************
  
   gtk_widget_show_all (window);
  //gtk_widget_show_all (window1);
   gtk_main();
   return TRUE;
}



本文来自ChinaUnix博客,假如查看原文请点:http://blog.chinaunix.net/u1/33226/showart_495584.html

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅