GtkToolButton具有自定义图标但库存图标大小

leg*_*s2k 4 gtk icons toolbar

我有一个GtkToolBar,它说3个GtkToolButtons,每个都有一个股票图标值,因此它们都出现在相同的大小; 现在我添加了第四个带有自定义图像(.png)的GtkToolButton,它具有任意尺寸,只有这个按钮看起来很大(因为图像具有更高的重新设置).如何缩放此GtkToolButton以匹配其他3个按钮?

这是我所做的简要介绍的代码:

GtkWidget *custom_icon = gtk_image_new_from_file(path);
GtkToolItem *toolbar_item = gtk_toggle_tool_button_new();
gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(toolbar_item), custom_icon);
gtk_tool_button_set_label(GTK_TOOL_BUTTON(toolbar_item), "Custom Item");
gtk_toolbar_insert(toolbar, toolbar_item, -1);
Run Code Online (Sandbox Code Playgroud)

pep*_*pan 6

这是另一种解决方案.

GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(icon_file_path, NULL);
int width, height;
gdk_pixbuf_get_file_info (icon_file_path, &width, &height);
gtk_icon_theme_add_builtin_icon ("custom_icon", width, pixbuf);
g_object_unref (G_OBJECT (pixbuf)); 

GtkToolItem *toolbar_item = gtk_toggle_tool_button_new();
gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON(toolbar_item), "custom_icon");
Run Code Online (Sandbox Code Playgroud)

如果您有不同大小的图像,可以将它们全部添加,然后让Gtk选择正确尺寸的图像(如果没有找到则调整大小):只需为每个图像文件重复前五行.

您可以在其他地方使用您的图标,其大小也将自动调整.例如,要将它用于主窗口:

gtk_window_set_icon_name(GTK_WINDOW(main_window), "custom_icon");
Run Code Online (Sandbox Code Playgroud)