如何在Monodevelop GTK#中更改背景颜色Entry小部件?

时间:2019-01-21 10:38:20

标签: monodevelop gtk#

我正在使用 Monodevelop GTK#并尝试更改条目小部件中的背景颜色,但是

entry.ModifyBase(StateType, Gdk.Color)

entry.ModifyBg(StateType, Gdk.Color)

不要为我更改背景颜色。

也许有人遇到了这个问题并解决了。

2 个答案:

答案 0 :(得分:0)

很多时候您实际上无法更改任何内容:Gtk(因此也就是Gtk#)认为它比您更了解最适合您的小部件的样式,并且该方法不会执行任何操作。

答案 1 :(得分:0)

您可以使用GTK RC文件进行指定,前提是您使用的是GTK#v2,而不是v3。 RC文件声明样式,这些样式可以在您启动应用时在运行时加载到窗口引擎中。

您可以在网络上找到RC文件的示例,但是以下是这类示例:

gtk-color-scheme = "bg_colour:#101010
fg_colour:#FFFFFF
fg_insensitive_colour:#EEEEEE
bg_prelight_colour:#38a4da
bg_selected_colour:#38a4da

gtk-auto-mnemonics              = 1
gtk-primary-button-warps-slider = 1

engine "murrine" 
{
    contrast = 1.0
    glazestyle = 1                  # 0 = flat hilight, 1 = curved hilight, 2 = concave style, 3 = top curved hilight, 4 = beryl hilight
    menubarstyle = 0                # 0 = flat, 1 = glassy, 2 = gradient, 3 = striped
    menubaritemstyle = 1            # 0 = menuitem look, 1 = button look
    menuitemstyle = 0               # 0 = flat, 1 = glassy, 2 = striped
    listviewheaderstyle = 0         # 0 = flat, 1 = glassy, 2 = raised
    listviewstyle = 0               # 0 = nothing, 1 = dotted
    scrollbarstyle = 0              # 0 = nothing, 1 = circles, 2 = handles, 3 = diagonal stripes, 4 = diagonal stripes and handles, 5 = horizontal stripes, 6 = horizontal stripes and handles
    highlight_shade = 0.9555        # set the amount of buttons or widgets hilight
    roundness = 2                   # 0 = squared, 1 = old default, more will increase roundness
    reliefstyle = 2                 # 0 = flat, 1 = inset, 2 = shadow, 3 = gradient shadow, 4 = strong shadow
    lightborderstyle = 1            # 0 = on top, 1 = on all sides
    animation = FALSE               # FALSE = disabled, TRUE = enabled
    gradients = TRUE                
    glow_shade = 1.0
    comboboxstyle = 0               # 1 to colourize below button
    expanderstyle = 0               # 0 = arrows, 1 = circles, 2 = buttons
}

style "entry"
{
    xthickness = 3
    ythickness = 4

    base[ACTIVE] = @bg_colour
    base[INSENSITIVE] = @bg_colour
    base[NORMAL] = @bg_colour
    base[PRELIGHT] = @bg_prelight_colour
    base[SELECTED] = @bg_selected_colour

    bg[ACTIVE] = @bg_colour
    bg[INSENSITIVE] = @bg_colour
    bg[NORMAL] = @bg_colour
    bg[PRELIGHT] = @bg_colour
    bg[SELECTED] = @bg_colour

    fg[ACTIVE] = @fg_colour
    fg[INSENSITIVE] = @fg_insensitive_colour
    fg[NORMAL] = @fg_colour
    fg[PRELIGHT] = @fg_colour
    fg[SELECTED] = @fg_colour

    text[ACTIVE] = @fg_colour
    text[INSENSITIVE] = @fg_insensitive_colour
    text[NORMAL] = @fg_colour
    text[PRELIGHT] = @fg_colour
    text[SELECTED] = @fg_colour
}

class "GtkEntry"                style "entry"
"

您需要加载文件的C#如下,将需要在Program静态main()中的某个位置调用它。假设您的GTKRC是项目中程序集中的嵌入式资源:

var _a = Assembly.GetAssembly (typeof (<your class>));
using (var _s = _a.GetManifestResourceStream ("<path in class's assembly to GTKRC file>")) {
    using (StreamReader _r = new StreamReader (_s)) {
        string _rc = _r.ReadToEnd ();
        Gtk.Rc.ParseString (_rc);
        Gtk.Settings.Default.ThemeName = "<your theme name>";
    }
}