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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # GTK# 中的自定義小部件 > 原文: [http://zetcode.com/gui/gtksharp/customwidget/](http://zetcode.com/gui/gtksharp/customwidget/) 工具箱通常僅提供最常見的窗口小部件,例如按鈕,文本窗口小部件,滑塊等。沒有工具箱可以提供所有可能的窗口小部件。 客戶端程序員可以創建更多專門的小部件。 他們使用工具箱提供的繪圖工具來完成此任務。 有兩種可能:程序員可以修改或增強現有的小部件,或者可以從頭開始創建自定義小部件。 ## 刻錄小部件 這是我們從頭開始創建的小部件的示例。 可以在各種媒體刻錄應用(例如 Nero 刻錄 ROM)中找到此小部件。 `burning.cs` ```cs using Gtk; using Cairo; using System; class Burning : DrawingArea { string[] num = new string[] { "75", "150", "225", "300", "375", "450", "525", "600", "675" }; public Burning() : base() { SetSizeRequest(-1, 30); } protected override bool OnExposeEvent(Gdk.EventExpose args) { Cairo.Context cr = Gdk.CairoHelper.Create(args.Window); cr.LineWidth = 0.8; cr.SelectFontFace("Courier 10 Pitch", FontSlant.Normal, FontWeight.Normal); cr.SetFontSize(11); int width = Allocation.Width; SharpApp parent = (SharpApp) GetAncestor (Gtk.Window.GType); int cur_width = parent.CurValue; int step = (int) Math.Round(width / 10.0); int till = (int) ((width / 750.0) * cur_width); int full = (int) ((width / 750.0) * 700); if (cur_width >= 700) { cr.SetSourceRGB(1.0, 1.0, 0.72); cr.Rectangle(0, 0, full, 30); cr.Clip(); cr.Paint(); cr.ResetClip(); cr.SetSourceRGB(1.0, 0.68, 0.68); cr.Rectangle(full, 0, till-full, 30); cr.Clip(); cr.Paint(); cr.ResetClip(); } else { cr.SetSourceRGB(1.0, 1.0, 0.72); cr.Rectangle(0, 0, till, 30); cr.Clip(); cr.Paint(); cr.ResetClip(); } cr.SetSourceRGB(0.35, 0.31, 0.24); for (int i=1; i<=num.Length; i++) { cr.MoveTo(i*step, 0); cr.LineTo(i*step, 5); cr.Stroke(); TextExtents extents = cr.TextExtents(num[i-1]); cr.MoveTo(i*step-extents.Width/2, 15); cr.TextPath(num[i-1]); cr.Stroke(); } ((IDisposable) cr.Target).Dispose(); ((IDisposable) cr).Dispose(); return true; } } class SharpApp : Window { int cur_value = 0; Burning burning; public SharpApp() : base("Burning") { SetDefaultSize(350, 200); SetPosition(WindowPosition.Center); DeleteEvent += delegate { Application.Quit(); }; VBox vbox = new VBox(false, 2); HScale scale = new HScale(0, 750, 1); scale.SetSizeRequest(160, 35); scale.ValueChanged += OnChanged; Fixed fix = new Fixed(); fix.Put(scale, 50, 50); vbox.PackStart(fix); burning = new Burning(); vbox.PackStart(burning, false, false, 0); Add(vbox); ShowAll(); } void OnChanged(object sender, EventArgs args) { Scale scale = (Scale) sender; cur_value = (int) scale.Value; burning.QueueDraw(); } public int CurValue { get { return cur_value; } } public static void Main() { Application.Init(); new SharpApp(); Application.Run(); } } ``` 我們在窗口底部放置一個`DrawingArea`并手動繪制整個窗口小部件。 所有重要的代碼都位于`Burning`類的`OnExposeEvent()`方法中。 此小部件以圖形方式顯示了介質的總容量和可供我們使用的可用空間。 該小部件由比例小部件控制。 自定義窗口小部件的最小值為 0,最大值為 750。如果值達到 700,則開始繪制紅色。 這通常表示過度燃燒。 ```cs string[] num = new string[] { "75", "150", "225", "300", "375", "450", "525", "600", "675" }; ``` 這些數字顯示在刻錄小部件上。 它們顯示了介質的容量。 ```cs SharpApp parent = (SharpApp) GetAncestor (Gtk.Window.GType); int cur_width = parent.CurValue; ``` 這兩行從刻度小部件獲取當前數字。 我們獲得父窗口小部件,并從父窗口小部件中獲得當前值。 ```cs int till = (int) ((width / 750.0) * cur_width); int full = (int) ((width / 750.0) * 700); ``` `till`參數確定要繪制的總大小。 該值來自滑塊小部件。 它占整個面積的一部分。 `full`參數確定我們開始用紅色繪制的點。 ```cs cr.SetSourceRGB(1.0, 1.0, 0.72); cr.Rectangle(0, 0, full, 30); cr.Clip(); cr.Paint(); cr.ResetClip(); ``` 此代碼在此處繪制了一個黃色矩形,直到介質充滿為止。 ```cs TextExtents extents = cr.TextExtents(num[i-1]); cr.MoveTo(i*step-extents.Width/2, 15); cr.TextPath(num[i-1]); cr.Stroke(); ``` 這里的代碼在刻錄小部件上繪制數字。 我們計算`TextExtents`來正確定位文本。 ```cs void OnChanged(object sender, EventArgs args) { Scale scale = (Scale) sender; cur_value = (int) scale.Value; burning.QueueDraw(); } ``` 我們從小部件中獲取值,并將其存儲在`cur_value`變量中以備后用。 我們重新繪制刻錄的小部件。 ![Burning widget](https://img.kancloud.cn/63/66/6366b1b4f5ab262b37baf7d6bdbd0857_358x228.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>

                              哎呀哎呀视频在线观看