是否可以设置 ScrollWindow 的颜色?

Is it possible to set the color of a ScrollWindow?

我有以下代码:

GdkColor color;
color.red = 0x0;
color.green = 0x0;
color.blue = 0x0;

gtk_init (&argc, &argv);
window = gtk_dialog_new ();
gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color);
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
gtk_window_set_title (GTK_WINDOW (window), "GtkScrolledWindow example");
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
gtk_widget_set_size_request (window, 300, 300);
gtk_window_fullscreen (GTK_WINDOW(window));
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 10);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), scrolled_window, TRUE, TRUE, 0);
gtk_widget_show (scrolled_window);
outside = gtk_table_new (10, 10, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (outside), 10);
gtk_table_set_col_spacings (GTK_TABLE (outside), 10);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), outside);
gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
gtk_widget_modify_bg(outside, GTK_STATE_NORMAL, &color);
gtk_widget_show (outside);

出于某种原因,window 而不是卷轴 window 的颜色设置为黑色。我该如何更改?

如果这是 Gtk3,您应该使用 CSS:https://developer.gnome.org/gtk3/stable/GtkCssProvider.html

像这样设置滚动的所有 children 的背景颜色 windows:

scrolledwindow * {
  background-color: red;
}

显然,在实际使用中,您可能会通过为小部件自定义 class 本身来更加具体。

也请查看检查器,让试用 CSS 更容易:https://wiki.gnome.org/Projects/GTK+/Inspector

你只需要 GtkEventBox.

GdkColor color;
color.red = 0x0;
color.green = 0x0;
color.blue = 0x0;

gtk_init (&argc, &argv);
window = gtk_dialog_new ();
gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color);
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
gtk_window_set_title (GTK_WINDOW (window), "GtkScrolledWindow example");
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
gtk_widget_set_size_request (window, 300, 300);
gtk_window_fullscreen (GTK_WINDOW(window));
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
//gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 10);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), scrolled_window, TRUE, TRUE, 0);
gtk_widget_show (scrolled_window);
outside = gtk_table_new (10, 10, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (outside), 10);
gtk_table_set_col_spacings (GTK_TABLE (outside), 10);

GtkWidget *box = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER(box), outside);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), box);
gtk_widget_modify_bg(box, GTK_STATE_NORMAL, &color);
gtk_widget_show (box);

//gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), outside);
//gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
//gtk_widget_modify_bg(outside, GTK_STATE_NORMAL, &color);
gtk_widget_show (outside);