我有这段代码:
GdkColor color;
gdk_color_parse ("black", &color);
GdkColor color2;
gdk_color_parse ("red", &color2);
button = gtk_button_new_with_label ("Text");
g_signal_connect (button, "clicked", G_CALLBACK (a), NULL);
gtk_widget_modify_bg ( GTK_WIDGET(button), GTK_STATE_PRELIGHT, &color);
gtk_widget_modify_text ( GTK_WIDGET(button), GTK_STATE_PRELIGHT, &color2);
gtk_table_attach_defaults (GTK_TABLE (outside), button, 0, 1, 0, 1);
gtk_widget_show (button);
虽然当鼠标悬停时背景变为黑色,但文字不会改变颜色。我怎么能改变这个?
答案 0 :(得分:2)
虽然我不知道为什么这段代码不起作用,但我找到了一个解决方法:
g_signal_connect (button, "enter", G_CALLBACK (changeColor), NULL);
g_signal_connect (button, "leave", G_CALLBACK (revertColor), NULL);
gtk_widget_modify_bg ( GTK_WIDGET(button), GTK_STATE_NORMAL, &color);
gtk_widget_modify_bg ( GTK_WIDGET(button), GTK_STATE_PRELIGHT, &color2);
static void changeColor(GtkWidget *widget, gpointer data) {
GdkColor color;
color.red = 0x0;
color.green = 0x0;
color.blue = 0x0;
GList *list;
std::string s = (std::string)"<span color=\"black\">" + (std::string)(gtk_widget_get_name(widget))+"</span>";
list = gtk_container_get_children(GTK_CONTAINER (widget));
gtk_label_set_markup(GTK_LABEL(list->data), s.c_str());
}
static void revertColor(GtkWidget *widget, gpointer data) {
GdkColor color;
color.red = 0x0;
color.green = 0x0;
color.blue = 0x0;
GList *list;
std::string s = (std::string)"<span color=\"white\">" + (std::string)(gtk_widget_get_name(widget))+"</span>";
list = gtk_container_get_children(GTK_CONTAINER (widget));
gtk_label_set_markup(GTK_LABEL(list->data), s.c_str());
}
这些回调会根据需要更改颜色和字体。