GTK transparent background and irregular buttons

Source: Internet
Author: User
Tags gtk

GTK transparent background and irregular buttons
Http://blog.chinaunix.net/u1/33226/showart_495584.html

GTK display irregular button

Step 1: Create a gtk_event_box and add the button image to the container;
2. Obtain the mask of the button image and associate it with event_box.
In this way, parts other than the button will not be displayed.

# Include <GTK/GTK. h>

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,
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 <GTK/GTK. h>

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_policy:
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_policy | 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_policy_event", gtk_signal_func (mouse_event_handle), null );
G_signal_connect (g_object (eventbox), "leave_policy_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;
}

In the open-source project openmoko source generation ***, there is such a method to implement irregular forms, but it can only be a form without modification. The implementation method is probably:

1. Create a PNG image of the target form size. The image contains the alpha channel. Fill in the data in the places you want to retain, and set the pixels in the areas you don't want;

2. Create a window with a PNG image as the background;

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 );

The above generation is for reference only.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.