GTK3.x/4.x简单教程(一)

1.install MSYS2

Download the installer: msys2-x86_64-20221216.exe
modify the pacman config with tsinghua

sed -i "s#mirror.msys2.org/#mirrors.tuna.tsinghua.edu.cn/msys2/#g" /etc/pacman.d/mirrorlist*

2.install GCC and GTK+ - 3.x/4.x

$ pacman -S mingw-w64-x86_64-gcc
$ pacman -S mingw-w64-x86_64-gtk4         #install  Gtk+-4.x  or
$ pacman -S mingw-w64-x86_64-gtk3         #install Gtk+-3.x 

3.configure .bashrc

export PATH=/../../mingw64/bin:/../../mingw64/share:$PATH
export PKG_CONFIG_PATH=/../../mingw64/lib/pkgconfig:$PKG_CONFIG_PATH

4.Hello World App in C

在这里插入图片描述

To begin our introduction to GTK, we’ll start with a simple Hello World GTK application.

Create a new file with the following content named hello-world-gtk.c.

#include <gtk/gtk.h>

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *button;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  button = gtk_button_new_with_label ("Hello World");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  gtk_window_set_child (GTK_WINDOW (window), button);

  gtk_window_present (GTK_WINDOW (window));
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  //Gtk3.0
  //app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  //Gtk4.0
  app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

You can compile the program above with GCC using:

#compiling gtk+-4.x source code 
$ gcc $(pkg-config --cflags gtk4) -o hello-world-gtk hello-world-gtk.c $(pkg-config --libs gtk4) 

# OR
# compiling gtk+-3.x source code like this
$ gcc $(pkg-config --cflags gtk+-3.0) -o hello-world-gtk hello-world-gtk.c $(pkg-config --libs gtk+-3.0) 

5.install Glade

a RAD tool to enable quick & easy development of user interfaces for the GTK toolkit and the GNOME desktop environment.

$ pacman -S mingw-w64-x86_64-glade

在这里插入图片描述

6. run gtk-demo

$ gtk4-demo

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值