/*File:Normal_Button.c
*Date:2013-12-03
*Author:sjin
*Mail:[email protected]
*/
#include
#include
#include
/*創建一個橫向盒,它包含一個圖像和一個標簽,並非你會這個盒子*/
GtkWidget *xpm_label_box(gchar *xpm_filename,gchar *label_text)
{
GtkWidget *box;
GtkWidget *label;
GtkWidget *image;
/*為圖像和標簽創建盒*/
box = gtk_hbox_new(FALSE,0);
gtk_container_set_border_width(GTK_CONTAINER(box),2);
/*創建一個圖像*/
image = gtk_image_new_from_file(xpm_filename);
/*為按鈕創建一個標簽*/
label = gtk_label_new(label_text);
/*圖像和按鈕組裝到盒子裡*/
gtk_box_pack_start(GTK_BOX(box),image,FALSE,FALSE,3);
gtk_box_pack_start(GTK_BOX(box),label,FALSE,FALSE,3);
gtk_widget_show(image);
gtk_widget_show(label);
return box;
}
/*回調函數
*傳到這個函數的數據將呗打印到標准輸出
* */
void callback(GtkWidget *widget, gpointer data)
{
g_print("Hello again -%s was pressed\n",(char *) data);
}
/*退出回調函數*/
gint delete_event(GtkWidget *widget,GdkEvent *event,gpointer data)
{
gtk_main_quit();
return FALSE;
}
int main(int argc,char *argv[])
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *box;
/*初始化*/
gtk_init(&argc,&argv);
/*創建窗口*/
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/*這個一般必要有delete_event信號到主窗口*/
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(delete_event),NULL);
/*設置窗口寬度*/
gtk_container_set_border_width(GTK_CONTAINER(window),80);
/*設置窗口標題*/
gtk_window_set_title(GTK_WINDOW(window),"普通按鈕練習");
button = gtk_button_new();
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(callback),(gpointer)"cool button");
box = xpm_label_box("info.xpm","cool button");
gtk_widget_show(box);
gtk_container_add(GTK_CONTAINER(button),box);
gtk_widget_show(button);
gtk_container_add(GTK_CONTAINER(window),button);
gtk_widget_show(window);
gtk_main();
return 0;
}
運行程序顯示下面的圖像: