|
发表于 2011-12-6 16:37:35
|
显示全部楼层
初来练手,很多不足,请各位大虾多指教
#include <gtk/gtk.h>
void callback(GtkWidget *widget, gpointer data)
{
static gint count_temp=1;
g_print("Hello,You clicked %s, %dtimes\\n",(gchar *)data,count_temp);
count_temp++;
}
gint delete_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
gtk_main_quit();
return FALSE;
}
int main (int argc, int *argv[])
{
//定义两控件
GtkWidget *win;
GtkWidget *btn;
//初始化
gtk_init(&argc,&argv);
//建立窗体并初始各项参数
win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(G_OBJECT(win),"My first GTK Programme!");
g_signal_connect(G_OBJECT(win),"delete_event",G_CALLBACK(delete_event),NULL);
gtk_window_set_position(GTK_WINDOW(win),GTK_WIN_POS_CENTER);
gtk_container_border_width(G_OBJECT(win),5);
//建立button,并装入窗体中
btn=gtk_button_new_with_label("Click me!");
gtk_container_add(GTK_CONTAINER(win),btn);
//四参数分别是:信号发送源对象,连接信号名称,
//捕获信号时所需调用的函数,传给此函数的参数。
g_signal_connect(G_OBJECT(btn),"clicked",G_CALLBACK(callback),"Button");
gtk_widget_show(btn);
gtk_widget_show(win);
gtk_main();
}
注:原先编译时总是报警告,后来才发现,原来在g_signal_connect()里面的第二参数,虽然也是打双引号,但其实不是字符串起提示作用,原来是调用的事件名,不能乱写,一个字母都不能改。狂汗!! |
|