Visual Studio 2022 GTK4 Project Key setting
一般情况下需要MSYS2,然后参考Linux下开发。
这里使用第二种,直接 VS + 编译好的bin/lib/include的开发。
预编译好的文件(只有x64版本):
https://github.com/wingtk/gvsbuild
下载后,解压到C盘根目录,C:/gtk/
VS Project 配置方法:
Lib库依赖:
gio-2.0.lib;cairo.lib;gdk_pixbuf-2.0.lib;glib-2.0.lib;gtk-4.lib;harfbuzz.lib;pango-1.0.lib;graphene-1.0.lib;gobject-2.0.lib;%(AdditionalDependencies)
Lib库路径:
C:\gtk\lib;$(ReferencePath)
Bin依赖,需要添加到Path
C:\gtk\bin;$(ExecutablePath)
Include依赖:
C:\gtk\lib\graphene-1.0\include;C:\gtk\include\graphene-1.0;C:\gtk\include\gdk-pixbuf-2.0;C:\gtk\include\harfbuzz;C:\gtk\include\pango-1.0;C:\gtk\include\cairo;C:\gtk\include\gtk-4.0\;C:\gtk\lib\glib-2.0\include;C:\gtk\include\glib-2.0;C:\gtk\include;$(IncludePath)
===========================================================
VS Project设定:
Windows subsytem setting
===========================================================
Code
// gtk4test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//#pragma comment( linker, "/subsystem:\"windows\" /entry:\"main\"" ) // 设置入口地址#include <Windows.h>
#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;GtkWidget* box;// 创建 windowwindow = gtk_application_window_new(app);gtk_window_set_title(GTK_WINDOW(window), "Window");gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);// box layoutbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);gtk_widget_set_halign(box, GTK_ALIGN_CENTER);gtk_widget_set_valign(box, GTK_ALIGN_CENTER);gtk_window_set_child(GTK_WINDOW(window), box);// button to box layoutbutton = gtk_button_new_with_label("Hello World");g_signal_connect(button, "clicked", G_CALLBACK(print_hello), NULL);g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_window_destroy), window); // 关联 click 到 window destroygtk_box_append(GTK_BOX(box), button);// 显示 windowgtk_window_present(GTK_WINDOW(window));
}int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd
)
{GtkApplication* app;int status;app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);status = g_application_run(G_APPLICATION(app), 0,nullptr);g_object_unref(app);return status;
}