2013-04-02 95 views
0

我在GTK一個窗口,其中包括建立()函數如下:如何更改單聲道(GTK)中的窗口背景顏色?

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)); 

這應該改變背景爲橙色,但它沒有。

我試圖在窗口元素上插入更多類似的函數,但它也不起作用,有誰知道如何更改窗口的背景顏色?

我想要做的是創建一個沒有裝飾(只是一個矩形)的特定顏色和一些文本的小窗口。這個窗口應該是透明的(現在可以工作),並且應該有一個圖片和兩個帶有文本的標籤,並且在點擊時消失 - 現在所有這些都有效,只是我無法將背景從灰色更改爲更好的顏色。我會很高興的答案,可以讓我用其他方式來做這件事(我可以考慮用繪圖區創建一個小表格,然後用顏色繪製它,然後繪製文本,但是這聽起來很複雜爲我想做的事情這麼簡單)。

注:單是使用GTK 2,和GTK#for .NET的版本使用20年2月12日

回答

3

嘗試使用事件框。似乎工作

+0

我該怎麼辦呢? – Petr

+0

eventbox1.ModifyBg(StateType.Normal,new Gdk.Color(0x60,0,0)); –

+0

是否適合你? –

0

的文件(對於C API)表示,gtk_widget_modify_color()已被棄用,而新寫入代碼應該使用gtk_widget_override_background_color()來代替,所以試試。

雖然GTK +中的重寫主題非常困難,但3.0 API真的聽起來好像支持你想要做的一樣。

不幸的是,現在無法自己測試這個。

+0

單是使用GTK 2 – Petr

1

對於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將使用您的自定義樣式作爲背景。

爲C文檔Gtk Resource Files

編輯:

如果只想改變一個特定的窗口,那麼你的自定義樣式字符串更改爲類似

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)

+0

這聽起來不錯...我會嘗試它,只是它聽起來有點像它會改變/所有/窗口的顏色,我不想,讓我們看看... – Petr

+0

我無法找到替代gtk#中的那個c函數:( – Petr

+0

是的,它會改變你程序中的所有'GtkWindow's。我編輯了我的答案,並提供了關於如何更改一個特定窗口的說明。 – iain