我正在使用Linux在C和gtk3中进行编程,我一直在尝试使用绘图信号和cairo上下文更改GtkBox元素的背景色,但到目前为止,它会将颜色更改为白色,而不是我所用的颜色。指定。我正在使用CSS修改其他元素的颜色,但我想对特定元素使用cairo上下文。
这是我的主。
#include <gtk/gtk.h>
#include "MainWindow.c"
int main(int argc, char** argv){
GtkApplication *app;
int status;
app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(MainWindow), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
这是我的MainWindow.c,从这里我叫绘图信号
#include <gtk/gtk.h>
#include "styles.h"
static void MainWindow(GtkApplication *app, gpointer data){
GtkWidget *window;
GtkWidget *fBox;
GtkWidget *boxLeft, *boxTotal, *boxDisplay;
GtkWidget *lblInput, *lblTotal, *lblIva, *lblSubTotal;
GtkWidget *boxItemList;
GtkWidget *boxDisplayScreen;
GtkWidget *boxDisplayTotal;
/* Create the window */
window = gtk_application_window_new(app);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_window_set_default_size(GTK_WINDOW(window), 1024, 768);
/* Main flow box */
fBox = gtk_flow_box_new();
gtk_container_add(GTK_CONTAINER(window), fBox);
/* Left Box */
boxLeft = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_flow_box_insert(GTK_FLOW_BOX(fBox), boxLeft ,1);
/* Total Window */
boxDisplay = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_set_size_request(boxDisplay, 418, 80);
gtk_box_pack_start(GTK_BOX(boxLeft), boxDisplay, FALSE, FALSE, 0);
boxDisplayScreen = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_size_request(boxDisplayScreen, 209, 80);
gtk_box_pack_start(GTK_BOX(boxDisplay), boxDisplayScreen, FALSE, FALSE, 0);
boxDisplayTotal = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_size_request(boxDisplayTotal, 209, 80);
gtk_box_pack_start(GTK_BOX(boxDisplay), boxDisplayTotal, FALSE, FALSE, 0);
//Painting the box
g_signal_connect(boxDisplayTotal, "draw", G_CALLBACK(redBackground), NULL);
gtk_widget_show_all(window);
}
这是我的style.c文件,我在其中指定绘图功能
#include <gtk/gtk.h>
#include "styles.h"
//Doesn't work
void whiteBackground(GtkWidget *widget, cairo_t *cr,
gpointer user_data){
cairo_set_source_rgb(cr, 50,205,50);
cairo_paint(cr);
}
//Doesn't work
gboolean redBackground(GtkWidget *widget, cairo_t *cr,
gpointer user_data){
guint width, height;
GdkRGBA color;
GtkStyleContext *context;
context = gtk_widget_get_style_context(widget);
width = gtk_widget_get_allocated_width(widget);
height = gtk_widget_get_allocated_height(widget);
gtk_render_background(context, cr, 0, 0, width, height);
gtk_style_context_get_color(context,
gtk_style_context_get_state(context),
&color);
cairo_set_source_rgb(cr, 50,205,50);
//gdk_cairo_set_source_rgba(cr, &color);
//cairo_fill(cr);
cairo_paint(cr);
return FALSE;
}
谢谢。