fbpixel

Il est possible de créer des interfaces graphique en langage C avec le paquet GTK. Nous allons voir dans ce tutoriel comment installer GTK et comment coder une interface graphique simple.

Installation de GTK

Téléchargez et installez MSYS2 et ouvrez un terminal

Puis installez le paquet GTK

pacman -S mingw-w64-ucrt-x86_64-gtk4

Installez ensuite le compilateur qui vous permettra de coder en C, C++.

pacman -S mingw-w64-ucrt-x86_64-toolchain base-devel

Pour compiler un projet GTK, un thème est nécessaire à l’application comme le thème Windows 10

git clone https://github.com/B00merang-Project/Windows-10.git

Code de test pour GTK

#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), "Hello");
  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;

  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;
}

Compiler

Avec un terminal bash (Git Bash Here ou MSYS2 bash.exe), entrez la commande suivante pour compiler le code

gcc $(pkg-config --cflags gtk4) -o testgtk.exe testgtk.c $(pkg-config --libs gtk4)

Pour lancer l’interface

./testgtk.exe

Alternatives

  • wxWidgets (C++, cross platform)
  • Win32api GUI (C, Windows)
  • GTK+ (C, cross platform)
  • Qt (C++, cross platform)

Sources