GTK + allows you to switch between different statuses of irregular buttons.

Source: Internet
Author: User

Allows you to switch between images in different States with irregular buttons. PNG transparent buttons

It's just some code for technical verification. Just encapsulate it and pull it.

# Include
<GTK/GTK. h>

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
(%. 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
(%. 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 );


/* 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,

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_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_container_add (gtk_container (eventbox), image );

Gtk_widget_show (image );
Gtk_widget_show (eventbox );


Gtk_widget_show (window );
Gtk_main ();
Return false;
}

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.