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
Un code de test proposé par l’outil est une interface qui affiche dans le terminal « hello, world! » lorsqu’on clique sur un bouton
/**
* @file gtk_helloworld.c
* @brief Simple Hello World program for GTK in C
*
* @compilation:
* gcc $(pkg-config --cflags gtk4) -o gtk_helloworld.exe gtk_helloworld.c $(pkg-config --libs gtk4)
* ./gtk_helloworld.exe
*/
#include <gtk/gtk.h>
/**
* @brief button clicked handler
*/
static void on_button_clicked(GtkWidget *widget, gpointer data)
{
g_print("Hello World\n");
}
/**
* @brief Build UI and show window
*/
static void activate(GtkApplication *app, gpointer user_data)
{
GtkWidget *window;
GtkWidget *button;
// create main window
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Hello");
gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
// create button and connect signal
button = gtk_button_new_with_label("Hello World");
g_signal_connect(button, "clicked", G_CALLBACK(on_button_clicked), NULL);
gtk_window_set_child(GTK_WINDOW(window), button);
gtk_window_present(GTK_WINDOW(window));
}
// ==================== MAIN ====================
int main(int argc, char **argv)
{
// create application and connect signal
GtkApplication *app = gtk_application_new("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
// run application
int status = g_application_run(G_APPLICATION(app), argc, argv);
// cleanup
g_object_unref(app);
return status;
}
Compiler le code GTK
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 gtk_helloworld.exe gtk_helloworld.c $(pkg-config --libs gtk4)
Pour lancer l’interface
./gtk_helloworld.exe
Compiler et exécuter le code de test pour vérifier l’installation et le process de compilation.
Alternatives
- wxWidgets (C++, cross platform)
- Win32api GUI (C, Windows)
- GTK+ (C, cross platform)
- Qt (C++, cross platform)