如何更改GTK +中任意小部件的颜色? [英] How do I change the colors of an arbitrary widget in GTK+?

查看:246
本文介绍了如何更改GTK +中任意小部件的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正在编写想通过颜色使用某些信息的应用程序,如何更改给定小部件的背景和前景色?我想知道如何在glade中做到这一点,如果可能的话,以及以编程方式(计算颜色)。

If I'm writing an application that wants to communicate some information through the use of color, how can I change the background and foreground colors of a given widget? I would like to know how to do this in glade if it's possible, as well as programmatically (to a computed color).

我想知道如何做到这一点一个复杂的小部件,例如,包含 VBox HBox ,其中包含一些标签

I want to know how to do this to a complex widget as well, for example, an HBox that contains a VBox that contains some Labels.

理想情况下,这还包括一个解决方案解决方案,它允许我对该窗口小部件的现有颜色进行识别主题使用的任何图像的平均颜色,以便我可以通过编程方式补偿可能导致文本无法读取或以其他方式冲突的任何颜色选择 - 但如果我可以将按钮变成红色,我会很高兴。

Ideally this would also include a solution solution that allows me to tint the widget's existing colors, and identify the average colors of any images in use by the theme, so that I can programmatically compensate for any color choices which might make text unreadable or otherwise clashing - but I would be happy if I could just turn a button red.

推荐答案

示例程序:

Example program:

#include <gtk/gtk.h>

static void on_destroy(GtkWidget* widget, gpointer data)
{
        gtk_main_quit ();
}

int main (int argc, char* argv[])
{
        GtkWidget* window;
        GtkWidget* button;

        gtk_init(&argc, &argv);
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        g_signal_connect(G_OBJECT (window), "destroy",
                G_CALLBACK (on_destroy), NULL);
        button = gtk_button_new_with_label("Hello world!");
        GdkColor red = {0, 0xffff, 0x0000, 0x0000};
        GdkColor green = {0, 0x0000, 0xffff, 0x0000};
        GdkColor blue = {0, 0x0000, 0x0000, 0xffff};
        gtk_widget_modify_bg(button, GTK_STATE_NORMAL, &red);
        gtk_widget_modify_bg(button, GTK_STATE_PRELIGHT, &green);
        gtk_widget_modify_bg(button, GTK_STATE_ACTIVE, &blue);
        gtk_container_add(GTK_CONTAINER(window), button);
        gtk_widget_show_all(window);
        gtk_main();
        return 0;
}

这篇关于如何更改GTK +中任意小部件的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆