An Introduction to C & GUI Programming -----Simon Long 学习笔记 1

这本书主要是讲C语言和GTK的

 

1. 环境准备

Ubuntu 20.04安装gtk3.0

sudo apt-get install gtk+-3.0   (书中用的gtk2.0 有点老了)

2. 编写程序

第一个gtk程序

#include <gtk/gtk.h>
int main (int argc, char *argv[])
{
    gtk_init (&argc, &argv);
    GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_show (win);
    gtk_main ();
    return 0;
}

3. 编译

gcc `pkg-config --cflags gtk+-3.0` test.c -o hello `pkg-config --libs gtk+-3.0`        test.c 文件生成hello二进制文件

4.运行结果

 5. 引申

gtk_window_new感觉是新生成一个窗口,再加一个会如何呢
#include <gtk/gtk.h>
int main (int argc, char *argv[])
{
    gtk_init (&argc, &argv);
    GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    GtkWidget *winwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_show (win);
    gtk_widget_show (winwin);
    gtk_main ();
    return 0;
}

编译后运行结果

 生成了2个窗口

点击窗口上的X并不会终止程序,只是窗口没了(

If you press the X in the
top-right corner, the window closes, but if you look in the terminal window from which you
ran gtktest, you’ll see the program is still running. This is because closing a window doesn’t
terminate the gtk_main function, which will carry on running until you hit CTRL+C in the
terminal window)

posted @ 2020-07-04 13:55  goodluck14  阅读(114)  评论(0编辑  收藏  举报