| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 
 |  #include <gtk/gtk.h>
 
GdkPixbuf *load_released_button_bitmap()
{
    GdkPixbuf *src_pixbuf=NULL;
    GdkPixbuf *dest_pixbuf=NULL;
 
    src_pixbuf=gdk_pixbuf_new_from_file("./buttons.bmp", NULL);
    if (src_pixbuf)
    {
        dest_pixbuf=gdk_pixbuf_new_subpixbuf(src_pixbuf, 171, 0, 30, 30);
        g_object_unref(src_pixbuf);
    }
    else
        printf("Chargement de l'image echoué.\n");
 
    return dest_pixbuf;
}
 
GdkPixbuf *load_pressed_button_bitmap()
{
    GdkPixbuf *src_pixbuf=NULL;
    GdkPixbuf *dest_pixbuf=NULL;
 
    src_pixbuf=gdk_pixbuf_new_from_file("./buttons.bmp", NULL);
    if (src_pixbuf)
    {
        dest_pixbuf=gdk_pixbuf_new_subpixbuf(src_pixbuf, 201, 0, 30, 30);
        g_object_unref(src_pixbuf);
    }
    else
        printf("Chargement de l'image echoué.\n");
 
    return dest_pixbuf;
}
 
void CMouse_on_button(GtkButton *bouton, GdkPixbuf *pressed_image_button)
{
    gtk_button_set_image(bouton, gtk_image_new_from_pixbuf(pressed_image_button));
}
 
void CMouse_out_button(GtkButton *bouton, GdkPixbuf *released_image_button)
{
    gtk_button_set_image(bouton,gtk_image_new_from_pixbuf(released_image_button));
}
 
void init_window()
{
    GtkWidget *fenetre_principale=NULL;
    GtkWidget *bouton=NULL;
    GdkPixbuf *pressed_image_button=NULL;
    GdkPixbuf *released_image_button=NULL;
 
    // Chargement des bitmaps du bouton
    pressed_image_button=load_pressed_button_bitmap();
    released_image_button=load_released_button_bitmap();
 
    /* création de la fenêtre principale */
    fenetre_principale=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(fenetre_principale), "test bitmap");
 
    // Création d'un bouton graphique
    bouton=gtk_button_new();
    gtk_button_set_image(GTK_BUTTON(bouton),
                                                            gtk_image_new_from_pixbuf(released_image_button));
    gtk_container_add(GTK_CONTAINER(fenetre_principale), bouton);
 
    /* Affectation des signaux lors du clic et relaché sur le bouton pour changer
    l'image du bouton.*/
    g_signal_connect(G_OBJECT(bouton), "pressed",
                                                        (GCallback)CMouse_on_button, pressed_image_button);
    g_signal_connect(G_OBJECT(bouton), "released",
                                                    (GCallback)CMouse_out_button, released_image_button);
 
    // Affection des signaux pour fermeture de l'application
    g_signal_connect(G_OBJECT(fenetre_principale), "destroy",
                                                                                                (GCallback)gtk_main_quit, NULL);
 
    // Affichage de la fenêtre
    gtk_widget_show_all(fenetre_principale);
}
 
gint main(gint argc,gchar **argv)
{
    gtk_init(&argc, &argv);
 
    init_window();
 
    gtk_main();
 
    return 0;
} | 
Partager