Gtk4设置GtkWidget的css样式
static void
set_widget_css_style(GtkWidget*w, const char * name, const char * css)
{
GtkCssProvider* provider = gtk_css_provider_new();
gtk_widget_set_name(w, name);
//gtk_css_provider_load_from_path(provider, "style.css");
gtk_css_provider_load_from_string(provider, css);
GtkStyleContext* context = gtk_widget_get_style_context(w);
gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
}
//全局css
static void
set_css_style(const char* css)
{
GtkCssProvider* provider = gtk_css_provider_new();
gtk_css_provider_load_from_string(provider, css);
gtk_style_context_add_provider_for_display(gdk_display_get_default(), GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
调用方式:
set_widget_css_style(buttonTest, "custom-button", "#custom-button {"
"background-image: none;"
"background-color: #ff0000;"
"color: #00ff00;"
"padding: 10px 10px 10px 10px;"
"border: solid green 2px;"
"border-radius: 8px;"
"}"
"#custom-button:hover {"
"background-image: none;"
"background-color: #008800;"
"}"
"#custom-button:active {"
"background-image: none;"
"background-color: #000888;"
"}");