如何使用 GTK+ 作为 GUI 将图像文件作为图标编译为 EXE?

Wes*_*ley 1 gtk

我知道如何将图像设置为 GtkWindow 的图标。

GtkWindow * window;
gtk_window_set_icon_from_file(window, "icon.png", NULL);
Run Code Online (Sandbox Code Playgroud)

并将 icon.png 与 exe 放在一起。

但是,如果我不想将该图像文件保留在我的文件夹中怎么办?我希望它以某种方式捆绑在 exe 文件中。

我知道这可以用 Qt 中的 qrc 来完成。我也可以在 GTK+ 中执行此操作吗?

Wes*_*ley 5

我现在知道该怎么做了。

首先,我必须将该图像转换为纯代码。

说我想制作icon.png为图标。

运行此命令将其转换为名为 的变量icon_1。如果没有该二进制文件,请获取它。

gdk-pixbuf-csource --raw --name=icon_1 icon.png
Run Code Online (Sandbox Code Playgroud)

它将显示在命令提示符上。但我想你想将其保存到文件中。所以

gdk-pixbuf-csource --raw --name=icon_1 icon.png>icon.c
Run Code Online (Sandbox Code Playgroud)

将在 Windows 中完成这项工作。

现在该变量icon_1可以使用了。

下一步。调用gdk_pixbuf_new_from_inline()以创建新的GdkPixbuf *from icon_1

GdkPixbuf * icon_title = gdk_pixbuf_new_from_inline(-1, icon_1, false, NULL);
Run Code Online (Sandbox Code Playgroud)

您现在可以调用gtk_window_set_icon()新创建的icon_title来设置窗口的图标。

gtk_window_set_icon(window, icon_title);
Run Code Online (Sandbox Code Playgroud)

另请记住,如果保留icon_1在独立的 C 文件中,则必须包含该glib.h文件,以便可以编译它,因为它使用 type guint8,并在使用之前 extern 该变量,以便可以链接它。

更多细节:

https://developer.gnome.org/gdk-pixbuf/unstable/gdk-pixbuf-Image-Data-in-Memory.html#gdk-pixbuf-new-from-inline