改变控件背景色可以使用gtk_widget_set_style来实现
GdkColor color;
gdk_color_parse ("white", &color);//白色
GtkStyle *rc_style = gtk_style_new();
rc_style->bg[GTK_STATE_NORMAL] = color;//设置背景色
gtk_widget_set_style(widget, rc_style);//设置样式
g_object_unref(rc_style);//释放空间经过测试对按钮等控件管用,对frame控件不管用
还可以通过gtk_widget_modify_bg实现
测试对按钮不管用,对frame不管用,但是对窗体和viewport管用
对于第二个参数GtkStateType,有下面的几种状态,解释参考gtk文档
enum GtkStateType
typedef enum
{
GTK_STATE_NORMAL,
GTK_STATE_ACTIVE,
GTK_STATE_PRELIGHT,
GTK_STATE_SELECTED,
GTK_STATE_INSENSITIVE
} GtkStateType;
对于box容器和frame不能直接设置背景色,gtk文档的解释:
Note that "no window" widgets (which have the GTK_NO_WINDOW flag set) draw on their parent container's window and thus may not draw any background themselves. This is the case for e.g. GtkLabel. To modify the background of such widgets, you have to set the background color on their parent; if you want to set the background of a rectangular area around a label, try placing the label in a GtkEventBox widget and setting the background color on that.
所以把他们放进一个eventbox控件里面就行了,然后对eventbox控件设置背景色。