#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()里面的第二参数,虽然也是打双引号,但其实不是字符串起提示作用,原来是调用的事件名,不能乱写,一个字母都不能改。狂汗!! 看看 看一下! 好少啊
回 jackprince 的帖子
jackprince:初来练手,很多不足,请各位大虾多指教#include <gtk/gtk.h>
void callback(GtkWidget *widget, gpointer data)
....... (2011-12-06 16:37) images/back.gif
看了下,好多都是水贴,只有LZ的好!最起码有实例,谢谢!! 学习 学习学习 我才是真正的新手!!!学C语言才两个月左右!!!
#include<gtk/gtk.h>
int main(int argc,char *argv[])
{
GtkWidget *window;
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"hello ubuntu 11.10!");
gtk_widget_show(window);
gtk_main();
} 有必要吗