1:开始
从hello开始吧。
- #include <gtk/gtk.h>
-
-
static void
-
hello(GtkWidget *widget, gpointer data)
-
{
-
g_print("Hello world\n");
-
}
-
-
static gboolean
-
delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
-
{
-
g_print("delete event occurred\n");
-
return TRUE;
-
}
-
-
static void
-
destroy(GtkWidget *widget, gpointer data)
-
{
-
gtk_main_quit();
-
}
-
-
int
-
main(int argc, char **argv)
-
{
-
GtkWidget *window;
-
GtkWidget *button;
-
-
gtk_init(&argc, &argv);
-
-
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-
-
g_signal_connect(window, "delete-event", G_CALLBACK(delete_event), NULL);
-
g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL);
-
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
-
-
button = gtk_button_new_with_label("Hello world");
-
g_signal_connect(button, "clicked", G_CALLBACK(hello), NULL);
-
-
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
-
-
gtk_container_add(GTK_CONTAINER(window), button);
-
-
gtk_widget_show(button);
-
gtk_widget_show(window);
-
-
gtk_main();
-
-
return 0;
-
}
- ozo@ozo:~/work/gtk/hello$ gcc `pkg-config --cflags --libs gtk+-2.0` -Wall -O2 -o hello hello.c
-
ozo@ozo:~/work/gtk/hello$ ./hello
-
Hello world
我们可爱的"Hello world"。
- 初始化gtk环境
- 新建窗口
- 注册窗口事件
- 设置窗口属性
- 新建按钮
- 设置按钮属性
- 注册按钮事件
- 添加按钮到窗口
- 显示按钮
- 显示窗口
- 进入主循环
和前面差不多,多了,事件注册,我们可以想想和VC界面编程有点类似,不过过程简单多了。
2:注
- gulong g_signal_connect( gpointer *object,
-
const gchar *name,
-
GCallback func,
-
gpointer func_data );
- object:发出信号的对象(例如窗口、按钮)
- name:发出什么样的信号(例如按下按钮)
- func:触发信号后做什么(例如按下按钮输出hello world)
- func_data:额外传输参数的地方
- void callback_func( GtkWidget *widget,
-
... /* other signal arguments */
-
gpointer callback_data );
- widget:谁发出的信号
- callback_data:传过来的参数
- gulong g_signal_connect_swapped( gpointer *object,
-
const gchar *name,
-
GCallback func,
-
gpointer *callback_data );
- 和g_signal_connect类似,不同的我们可以看出来就是发信号的对象和用户数据位置交换了
- void callback_func( gpointer callback_data,
-
... /* other signal arguments */
-
GtkWidget *widget);
看出来了吧,位置换了,
3:事件列表
- event
-
button_press_event
-
button_release_event
-
scroll_event
-
motion_notify_event
-
delete_event
-
destroy_event
-
expose_event
-
key_press_event
-
key_release_event
-
enter_notify_event
-
leave_notify_event
-
configure_event
-
focus_in_event
-
focus_out_event
-
map_event
-
unmap_event
-
property_notify_event
-
selection_clear_event
-
selection_request_event
-
selection_notify_event
-
proximity_in_event
-
proximity_out_event
-
visibility_notify_event
-
client_event
-
no_expose_event
-
window_state_event
4:事件类型列表
- GDK_NOTHING
-
GDK_DELETE
-
GDK_DESTROY
-
GDK_EXPOSE
-
GDK_MOTION_NOTIFY
-
GDK_BUTTON_PRESS
-
GDK_2BUTTON_PRESS
-
GDK_3BUTTON_PRESS
-
GDK_BUTTON_RELEASE
-
GDK_KEY_PRESS
-
GDK_KEY_RELEASE
-
GDK_ENTER_NOTIFY
-
GDK_LEAVE_NOTIFY
-
GDK_FOCUS_CHANGE
-
GDK_CONFIGURE
-
GDK_MAP
-
GDK_UNMAP
-
GDK_PROPERTY_NOTIFY
-
GDK_SELECTION_CLEAR
-
GDK_SELECTION_REQUEST
-
GDK_SELECTION_NOTIFY
-
GDK_PROXIMITY_IN
-
GDK_PROXIMITY_OUT
-
GDK_DRAG_ENTER
-
GDK_DRAG_LEAVE
-
GDK_DRAG_MOTION
-
GDK_DRAG_STATUS
-
GDK_DROP_START
-
GDK_DROP_FINISHED
-
GDK_CLIENT_EVENT
-
GDK_VISIBILITY_NOTIFY
-
GDK_NO_EXPOSE
-
GDK_SCROLL
-
GDK_WINDOW_STATE
-
GDK_SETTING
阅读(2040) | 评论(0) | 转发(0) |