简体   繁体   English

如何在Mono(GTK)中更改窗口背景颜色?

[英]How to change window background color in mono (GTK)?

I have a window in gtk which includes a Build() function as follow: 我在gtk中有一个窗口,其中包括一个Build()函数,如下所示:

protected virtual void Build()
{
    global::Stetic.Gui.Initialize(this);
    // Widget Client.Forms.Notification
    this.Name = "Client.Forms.Notification";
    this.Title = "Notification";
    this.TypeHint = Gdk.WindowTypeHint.Normal;
    //this.TypeHint = ((global::Gdk.WindowTypeHint)(4));
    this.WindowPosition = ((global::Gtk.WindowPosition)(4));
    // Container child Client.Forms.Notification.Gtk.Container+ContainerChild
    this.vbox1 = new global::Gtk.VBox();
    this.vbox1.Name = "vbox1";
    this.vbox1.Spacing = 6;
    // Container child vbox1.Gtk.Box+BoxChild
    this.label1 = new global::Gtk.Label();
    this.label1.HeightRequest = 20;
    this.label1.Name = "label1";
    this.label1.LabelProp = "Notification";
    this.vbox1.Add(this.label1);
    global::Gtk.Box.BoxChild w1 = ((global::Gtk.Box.BoxChild)(this.vbox1[this.label1]));
    w1.Position = 0;
    w1.Expand = false;
    w1.Fill = false;
    // Container child vbox1.Gtk.Box+BoxChild
    this.hbox1 = new global::Gtk.HBox();
    this.hbox1.Name = "hbox1";
    this.hbox1.Spacing = 6;
    // Container child hbox1.Gtk.Box+BoxChild
    this.image1 = new global::Gtk.Image();
    this.image1.Name = "image1";
    this.image1.Pixbuf = global::Gdk.Pixbuf.LoadFromResource("Client.Resources.icon.png");
    this.hbox1.Add(this.image1);
    global::Gtk.Box.BoxChild w2 = ((global::Gtk.Box.BoxChild)(this.hbox1[this.image1]));
    w2.Position = 0;
    w2.Expand = false;
    w2.Fill = false;
    // Container child hbox1.Gtk.Box+BoxChild
    this.label2 = new global::Gtk.Label();
    this.label2.Name = "label2";
    this.label2.WidthRequest = 260;
    this.label2.Wrap = true;
    this.label2.LabelProp = "Description";
    this.hbox1.Add(this.label2);
    global::Gtk.Box.BoxChild w3 = ((global::Gtk.Box.BoxChild)(this.hbox1[this.label2]));
    w3.Position = 1;
    w3.Expand = false;
    w3.Fill = false;
    this.vbox1.Add(this.hbox1);
    global::Gtk.Box.BoxChild w4 = ((global::Gtk.Box.BoxChild)(this.vbox1[this.hbox1]));
    w4.Position = 1;
    w4.Expand = false;
    w4.Fill = false;
    this.label1.ModifyBase(Gtk.StateType.Normal, Core.fromColor(System.Drawing.Color.Orange));
    this.ModifyBase(Gtk.StateType.Normal, Core.fromColor(System.Drawing.Color.Orange));
    this.vbox1.ModifyBase(Gtk.StateType.Normal, Core.fromColor(System.Drawing.Color.Orange));
    root = new Gtk.EventBox();
    root.Add(this.vbox1);
    this.Decorated = false;
    this.Add(root);
    if ((this.Child != null))
    {
        this.Child.ShowAll();
    }
    this.DefaultWidth = 460;
    this.DefaultHeight = 220;
}

(source code of this whole window: https://github.com/pidgeonproject/pidgeon/blob/master/Forms/Notification.cs ) (整个窗口的源代码: https : //github.com/pidgeonproject/pidgeon/blob/master/Forms/Notification.cs

As you can see it is calling 如您所见,它正在呼唤

this.ModifyBase(Gtk.StateType.Normal, Core.fromColor(System.Drawing.Color.Orange));

which should change the background to orange, however it doesn't. 这应该将背景更改为橙色,但事实并非如此。

I tried to insert more similar functions on window elements, but it doesn't work either, does anyone know how to change the background color of the window? 我试图在窗口元素上插入更多类似的函数,但是它也不起作用,有人知道如何更改窗口的背景色吗?

What I want to do is create a small window with no decoration (so just a rectangle) of specific color and some text in it. 我要做的是创建一个没有特定颜色和其中一些文字的装饰的小窗口(所以只有一个矩形)。 This window is supposed to be transparent (that works now) and should have a picture and 2 labels with text and disappear on click - all this works now just I can't change the background from gray to some better color. 该窗口应该是透明的(现在可以使用),并且应该具有图片和2个带有文本的标签,并且在单击时消失-现在所有这些都可以使用,只是我无法将背景从灰色更改为更好的颜色。 I would be happy for answers that would let me do this using some other way (I can think of creating a small form just with drawing area and paint it with the color and then paint the text as well, but it sounds to me quite complicated for something so simple as I want to do). 我很高兴获得可以使用其他方法完成此操作的答案(我可以想到只用绘图区域创建一个小表格,然后用颜色对其进行绘制,然后再对文本进行绘制,但是这对我来说听起来很复杂对于我想做的那么简单的事情)。

NOTE: mono is using GTK 2, and the version of GTK# for .Net is using 2.12.20 注意: mono使用的是GTK 2,.Net的GTK#版本使用的是2.12.20

Try to use Event Box. 尝试使用事件框。 Seems to work 似乎可以工作

For GTK+2 you need to create a gtk resource like so: 对于GTK + 2,您需要创建一个gtk资源,如下所示:

char *my_custom_style = "style \"my-style-name\" { bg[NORMAL] = \"#339933\" }\nclass \"GtkWindow\" style \"my-style-name\"";

then load the resource when you start the program, after initialization: 然后在初始化后在启动程序时加载资源:

gtk_rc_parse_string (my_custom_style);

Now any GtkWindow will use your custom style for it's background. 现在,任何GtkWindow都将使用自定义样式作为背景。

The C documentation for Gtk Resource Files Gtk资源文件的C文档

Edit: 编辑:

If you only want to change one specific window, then you'd change the custom style string to something like 如果您只想更改一个特定的窗口,则可以将自定义样式字符串更改为类似

char *my_custom_style="style \"my-style-name\" { bg[NORMAL] = \"#339933\" }\nwidget \"my-custom-window\" style= \"my-style-name\"';

You then set the name of the window you want to change the background with 然后,您可以设置要用来更改背景的窗口的名称

gtk_widget_set_name (GTK_WIDGET (my_window), "my-custom-window");

Edit 2: 编辑2:

Here's the documentation to the Gtk# Rc parsing function: 这是Gtk#Rc解析功能的文档:

http://buttle.shangorilla.com/1.1/handlers/monodoc.ashx?link=M%3aGtk.Rc.Parse(System.String) http://buttle.shangorilla.com/1.1/handlers/monodoc.ashx?link=M%3aGtk.Rc.Parse(System.String)

The documentation (for the C API) says that gtk_widget_modify_color() is deprecated, and that newly written code should use gtk_widget_override_background_color() instead, so try that. 文档(针对C API)说gtk_widget_modify_color()已弃用,而新编写的代码应改为使用gtk_widget_override_background_color() ,因此请尝试这样做。

Overriding themes in GTK+ is notoriously hard though, but the 3.0 API really sounds as if it's supporting what you want to do. 尽管众所周知,在GTK +中覆盖主题非常困难,但是3.0 API听起来确实好像在支持您想做的事情。

Can't test this myself at the moment, unfortunately. 不幸的是,目前无法自己测试。

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

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