【问题标题】:How to change window background color in mono (GTK)?如何在单声道(GTK)中更改窗口背景颜色?
【发布时间】:2013-04-02 07:50:02
【问题描述】:

我在 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;
}

(整个窗口的源代码:https://github.com/pidgeonproject/pidgeon/blob/master/Forms/Notification.cs

如你所见,它正在调用

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

这应该将背景更改为橙色,但事实并非如此。

我尝试在窗口元素上插入更多类似的功能,但它也不起作用,有谁知道如何更改窗口的背景颜色?

我想要做的是创建一个小窗口,其中没有特定颜色的装饰(所以只是一个矩形)和一些文本。该窗口应该是透明的(现在可以使用),并且应该有一张图片和 2 个带有文本的标签,并且在单击时消失 - 所有这些现在都可以正常工作,只是我无法将背景从灰色更改为更好的颜色。我会很高兴得到可以让我使用其他方式做到这一点的答案(我可以考虑创建一个仅带有绘图区域的小表单并用颜色绘制它,然后也绘制文本,但这对我来说听起来很复杂对于我想做的这么简单的事情)。

注意: mono 使用 GTK 2,而用于 .Net 的 GTK# 版本使用 2.12.20

【问题讨论】:

    标签: c# mono gtk gtk#


    【解决方案1】:

    尝试使用事件框。似乎有效

    【讨论】:

    • eventbox1.ModifyBg(StateType.Normal, new Gdk.Color(0x60,0,0));
    • 谁两次投了我的票?根据我的声誉,我看到 2 票反对,但这里是 0 票。那奇怪。我不能再给出好的答案了:-)
    • 很好,但它被记录为-2,现在我被禁止了,不能写答案:-(
    【解决方案2】:

    对于 GTK+2,您需要像这样创建一个 gtk 资源:

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

    然后在启动程序时加载资源,初始化后:

    gtk_rc_parse_string (my_custom_style);
    

    现在任何GtkWindow 都将使用您的自定义样式作为其背景。

    Gtk Resource Files 的 C 文档

    编辑:

    如果您只想更改一个特定的窗口,那么您可以将自定义样式字符串更改为类似

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

    然后设置要更改背景的窗口的名称

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

    编辑 2:

    这是 Gtk# Rc 解析函数的文档:

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

    【讨论】:

    • 这听起来不错...我会试试的,只是听起来有点像它会改变/all/窗口的颜色,我不想要,让我们看看...
    • 我无法在 gtk# 中找到该 c 函数的替代方法:(
    • 是的,它会改变你程序中的所有GtkWindows。我已经编辑了我的答案,其中包含有关如何仅更改一个特定窗口的说明。
    【解决方案3】:

    文档(针对 C API)说 gtk_widget_modify_color() 已弃用,新编写的代码应该改用 gtk_widget_override_background_color(),所以试试吧。

    虽然在 GTK+ 中覆盖主题是出了名的困难,但 3.0 API 听起来确实支持您想要做的事情。

    很遗憾,目前无法自己测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 2013-08-19
      • 2020-06-16
      • 2014-03-19
      相关资源
      最近更新 更多