<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # GTK# 中的小部件 > 原文: [http://zetcode.com/gui/gtksharp/widgets/](http://zetcode.com/gui/gtksharp/widgets/) 在 GTK# 編程教程的這一部分中,我們將介紹一些 GTK# 小部件。 小部件是 GUI 應用的基本構建塊。 多年來,幾個小部件已成為所有 OS 平臺上所有工具包中的標準。 例如,按鈕,復選框或滾動條。 GTK# 工具箱的理念是將小部件的數量保持在最低水平。 將創建更多專門的小部件作為自定義 GTK# 小部件。 ## `Label` `Label`小部件顯示文本。 `label.cs` ```cs using Gtk; class SharpApp : Window { string text = @"Meet you downstairs in the bar and heard your rolled up sleeves and your skull t-shirt You say why did you do it with him today? and sniff me out like I was Tanqueray cause you're my fella, my guy hand me your stella and fly by the time I'm out the door you tear men down like Roger Moore I cheated myself like I knew I would I told ya, I was trouble you know that I'm no good"; public SharpApp() : base("You know I'm No Good") { BorderWidth = 8; SetPosition(WindowPosition.Center); DeleteEvent += delegate { Application.Quit(); }; Label lyrics = new Label(text); Add(lyrics); ShowAll(); } public static void Main() { Application.Init(); new SharpApp(); Application.Run(); } } ``` 該代碼示例在窗口上顯示了一些歌詞。 ```cs string text = @"Meet you downstairs in the bar and heard your rolled up sleeves and your skull t-shirt ... ``` 在 C# 編程語言中,多行字符串以`@`字符開頭。 ```cs BorderWidth = 8; ``` `Label`周圍有一些空白。 ```cs Label lyrics = new Label(text); Add(lyrics); ``` `Label`小部件已創建并添加到窗口。 ![Label Widget](https://img.kancloud.cn/84/62/8462eb32a94faa62eaf39b7063ad1ac0_297x282.jpg) 圖:`Label`小部件 ## `CheckButton` `CheckButton`是具有兩種狀態的窗口小部件:打開和關閉。 開狀態通過復選標記顯示。 它用來表示一些布爾屬性。 `checkbutton.cs` ```cs using Gtk; using System; class SharpApp : Window { public SharpApp() : base("CheckButton") { SetDefaultSize(250, 200); SetPosition(WindowPosition.Center); DeleteEvent += delegate { Application.Quit(); }; CheckButton cb = new CheckButton("Show title"); cb.Active = true; cb.Toggled += OnToggle; Fixed fix = new Fixed(); fix.Put(cb, 50, 50); Add(fix); ShowAll(); } void OnToggle(object sender, EventArgs args) { CheckButton cb = (CheckButton) sender; if (cb.Active) { Title = "CheckButton"; } else { Title = " "; } } public static void Main() { Application.Init(); new SharpApp(); Application.Run(); } } ``` 根據`CheckButton`的狀態,我們將在窗口的標題欄中顯示標題。 ```cs CheckButton cb = new CheckButton("Show title"); ``` `CheckButton`小部件已創建。 ```cs cb.Active = true; ``` 默認情況下標題是可見的,因此我們默認情況下選中復選按鈕。 ```cs CheckButton cb = (CheckButton) sender; ``` 在這里,我們將發送方對象轉換為`CheckButton`類。 ```cs if (cb.Active) { Title = "CheckButton"; } else { Title = " "; } ``` 根據`CheckButton`的`Active`屬性,我們顯示或隱藏窗口的標題。 ![CheckButton](https://img.kancloud.cn/fd/59/fd59f54c09dc8b38127f91506a2ee59c_258x228.jpg) 圖:`CheckButton` ## `ComboBox` `ComboBox`是一個小部件,允許用戶從選項列表中進行選擇。 `combobox.cs` ```cs using Gtk; using System; class SharpApp : Window { Label label; public SharpApp() : base("ComboBox") { string[] distros = new string[] {"Ubuntu", "Mandriva", "Red Hat", "Fedora", "Gentoo" }; SetDefaultSize(250, 200); SetPosition(WindowPosition.Center); BorderWidth = 7; DeleteEvent += delegate { Application.Quit(); }; Fixed fix = new Fixed(); ComboBox cb = new ComboBox(distros); cb.Changed += OnChanged; label = new Label("-"); fix.Put(cb, 50, 30); fix.Put(label, 50, 140); Add(fix); ShowAll(); } void OnChanged(object sender, EventArgs args) { ComboBox cb = (ComboBox) sender; label.Text = cb.ActiveText; } public static void Main() { Application.Init(); new SharpApp(); Application.Run(); } } ``` 該示例顯示了一個組合框和一個標簽。 組合框具有六個選項的列表。 這些是 Linux 發行版的名稱。 標簽窗口小部件顯示了從組合框中選擇的選項。 ```cs string[] distros = new string[] {"Ubuntu", "Mandriva", "Red Hat", "Fedora", "Gentoo" }; ``` 這是一個字符串數組,將顯示在`ComboBox`小部件中。 ```cs ComboBox cb = new ComboBox(distros); ``` `ComboBox`小部件已創建。 構造器將字符串數組作為參數。 ```cs void OnChanged(object sender, EventArgs args) { ComboBox cb = (ComboBox) sender; label.Text = cb.ActiveText; } ``` 在`OnChanged()`方法內部,我們從組合框中獲取選定的文本并將其設置為標簽。 ![ComboBox](https://img.kancloud.cn/7e/00/7e00c101f44c5c34521a050f225cd787_258x228.jpg) 圖:`ComboBox` ## `Image` 下一個示例介紹`Image`小部件。 此小部件顯示圖片。 `image.cs` ```cs using Gtk; using System; class SharpApp : Window { Gdk.Pixbuf castle; public SharpApp() : base("Red Rock") { BorderWidth = 1; SetPosition(WindowPosition.Center); DeleteEvent += delegate { Application.Quit(); }; try { castle = new Gdk.Pixbuf("redrock.png"); } catch { Console.WriteLine("Image not found"); Environment.Exit(1); } Image image = new Image(castle); Add(image); ShowAll(); } public static void Main() { Application.Init(); new SharpApp(); Application.Run(); } } ``` 我們在窗口中顯示紅色巖石城堡。 ```cs try { castle = new Gdk.Pixbuf("redrock.png"); } catch { Console.WriteLine("Image not found"); Environment.Exit(1); } ``` 我們創建`Gdk.Pixbuf`小部件。 我們將構造器放在`try`和`catch`關鍵字之間,以處理可能的錯誤。 ```cs Image image = new Image(castle); Add(image); ``` `Image`小部件已創建并添加到窗口。 ![Image](https://img.kancloud.cn/e1/4b/e14b55be241f8a49859e4929bb61fb92_460x283.jpg) 圖:圖像 在本章中,我們展示了 GTK# 編程庫的第一組基本小部件。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看