الأدوات (الويدجات) هى وحدة بناء الواجهة، وتقدم لنا GTK# الكثير من الأدوات المختلفة كالأزرار وصناديق الإختبار والمنزلقات والقوائم.. الخ ،كل مايحتاجه المبرمج فى عمله. سنتعرض لبعض الأدوات المفيدة فى هذه الجزئية
Widgets are basic building blocks of a GUI application. Over the years, several widgets became a standard in all toolkits on all OS platforms. For example a button, a check box or a scroll bar. The GTK# toolkit's philosophy is to keep the number of widgets at a minimum level. More specialized widgets are created as custom GTK# widgets.
يستخدم ال label لعرض نص ساكن
label.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(); } }
فى المثال نعرض كلمات اغنية على نص ساكن
string text = @"Meet you downstairs in the bar and heard your rolled up sleeves and your skull t-shirt ...
فى لغة C# تستطيع مد السلسلة النصية على اكثر من سطر بسبقها بالحرف @
BorderWidth = 8;ال label محاط ببعض المساحة.
Label lyrics = new Label(text); Add(lyrics);
انشاء ال label واضافته على النافذة
Figure: Label Widget
صندوق الإختيار CheckButton يستخدم لعرض حالتين on, off (منشط او غير منشط) ويستخدم من اجل الإختيارات المنطقية
checkbutton.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 cb = new CheckButton("Show title");انشاء زر الإختيار
cb.Active = true;ننشطه لنجعل العنوان متاحا فى البداية
CheckButton cb = (CheckButton) sender;نحول المرسل الى زر يدويا بإستخدام التحويل القسرى
if (cb.Active) { Title = "CheckButton"; } else { Title = " "; }
بإختبار الخاصية Active لنحدد هل سنظهر او نخفى العنوان الخاص بالنافذة
Figure: CheckButton
ال ComboBox يسمح بإلإختيار من قائمة ما
combobox.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(); } }
الأمثلة تعرض ComboBox (تشمل 6 عناصر وهى اسماء لتوزيعات لينكس) وlabel ليستخدم لعرض التوزيعة المختارة
string[] distros = new string[] {"Ubuntu", "Mandriva", "Red Hat", "Fedora", "Gentoo" };
مصفوفة من النصوص اللتى تمثل العناصر التى تعرض على ال ComboBox
ComboBox cb = new ComboBox(distros);انشاء ال ComboBox ونمرر اسماء التوزيعات لمشيدها
void OnChanged(object sender, EventArgs args) { ComboBox cb = (ComboBox) sender; label.Text = cb.ActiveText; }
فى الطريقة OnChanged، نحصل على النص المختار ونعرضه على ال label
Figure: ComboBox
فى المثال، سنقدم ال Image ويدجت المستخدم لعرض الصور
image.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(); } }
نعرض قلعة الصخرة الحمراء على النافذة
try { castle = new Gdk.Pixbuf("redrock.png"); } catch { Console.WriteLine("Image not found"); Environment.Exit(1); }
ننشئ كائن Gdk.Pixbuf ، ونضع المشيد بين try, catch لنتمكن من معالجة الأخطاء
Image image = new Image(castle); Add(image);
هنا تم انشاءها، وإضافتها للنافذة
Figure: Image
تعرضنا لبعض الويدجات الأساسية فى GTK#
Home Contents Top of Page