简体   繁体   English

GTK_WINDOW_TOPLEVEL Gtk Widget的背景颜色

[英]Background color for GTK_WINDOW_TOPLEVEL Gtk Widget

In the following code, I want the background colour of the main GTK_WINDOW_TOPLEVEL to be 0xc0deed. 在下面的代码中,我希望主GTK_WINDOW_TOPLEVEL的背景颜色为0xc0deed。 But when I run it is appearing black. 但是当我跑的时候看起来很黑。 I even tried gtk_drawing_area_new and adding it to the main window. 我甚至尝试了gtk_drawing_area_new并将其添加到主窗口。 But still it is appearing black although I could get other colours like red, blue, white etc 但它仍然显得黑色,虽然我可以得到其他颜色,如红色,蓝色,白色等

#include <gtk/gtk.h>

int main( int argc, char *argv[])
{
    GtkWidget *p_s_window = NULL;
    GdkColor color;
    color.red = 0x00C0;
    color.green = 0x00DE;
    color.blue = 0x00ED;
    gtk_init(&argc, &argv);
    p_s_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(p_s_window), GTK_WIN_POS_CENTER);
    gtk_window_set_title(GTK_WINDOW(p_s_window), "hello");
    gtk_widget_modify_bg(p_s_window, GTK_STATE_NORMAL, &color);
    g_signal_connect_swapped(G_OBJECT(p_s_window), "destroy",
            G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show_all(p_s_window);
    gtk_main();
    return 0;
}

The GdkColor components are 16-bit, thus having a range of 0 through 65535. Multiply your values with 65535/255 and you'll be alright. GdkColor组件是16位,因此范围为0到65535.将您的值乘以65535/255,您就可以了。

For example yellow would be: 例如黄色将是:

color.red = 0xffff;
color.green = 0xffff;
color.blue = 0;

Although the question is fairly old, I would like to provide another answer that doesn't require calculation. 虽然问题相当陈旧,但我想提供另一个不需要计算的答案。

You can use gdk_color_parse() to parse the string representation of your color. 您可以使用gdk_color_parse()来解析颜色的字符串表示形式。 As mentioned in the documentation , this works on various formats: 文档中所述,这适用于各种格式:

The string can either [sic!] one of a large set of standard names (taken from the X11 rgb.txt file), or it can be a hexadecimal value in the form “#rgb” “#rrggbb”, “#rrrgggbbb” or “#rrrrggggbbbb” where “r”, “g” and “b” are hex digits of the red, green, and blue components of the color, respectively. 该字符串可以是[sic!]大量标准名称之一(取自X11 rgb.txt文件),也可以是“#rgb”“#rrggbb”,“#rrrgggbbb”形式的十六进制值或“#rrrrggggbbbb”,其中“r”,“g”和“b”分别是该颜色的红色,绿色和蓝色分量的十六进制数字。

So in your case this would simply be: 所以在你的情况下,这只是:

GdkColor color;
if (gdk_color_parse("#c0deed", &color)) {
    gtk_widget_modify_bg(p_s_window, GTK_STATE_NORMAL, &color);
} else {
    // set default color
}

Please also note that as of Gtk 3.0, gtk_widget_modify_bg() is deprecated. 另请注意,自Gtk 3.0起,不推荐使用gtk_widget_modify_bg() Use gtk_widget_override_background_color() instead. 请改用gtk_widget_override_background_color()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM