المحتويات

الأداوت الأساسية فى مكتبات Winforms

هذا الجزء من الدرس سوف يدور حول بعص الأداوت الأساسية فى مكتبات Winforms.

الأدوات فى مكتبات Winforms هى المكون الأساسى للتطبيق و مكتبات Winforms تحتوى على العديد من الأداوت المختلفة مثل الأزرار و مربعات الإختيار و شرائط التمرير و العناوين...إلخ.أى شىء يحتاجة المبرمج لعمله و فى هذا الجزء من الدرس سوف نتعرض للعديد من الأداوت المفيدة.

أداة العنوان Label

Label هى عبارة ان أداة بسيطة لعرض نص أو صورة و هى أداة لا تستقبل التنشيط focus.

label.py

#!/usr/bin/ipy

import sys
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import Application, Form, Label
from System.Drawing import Size, Point, Font


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

Upstairs in bed, with my ex boy
He's in a place, but I cant get joy
Thinking of you in the final throws, this is when my buzzer goes"""


class IForm(Form):

    def __init__(self):

        self.Text = "You know I'm No Good"

        font = Font("Serif", 10)

        lyrics = Label()
        lyrics.Parent = self
        lyrics.Text = text
        lyrics.Font = font
        lyrics.Location = Point(10, 10)
        lyrics.Size = Size(290, 290)

        self.CenterToScreen()


Application.Run(IForm())



فى هذا المثال عرضنا بعض الكلمات على النافذة.

 lyrics = Label()

تم إنشاء الأداة Label.

 text = """Meet you downstairs in the bar and heard 
           ... """

ها هو النص.

 font = Font("Serif", 10)
 ...
 lyrics.Font = font

تم تغيير الخط إلى Serif بحجم 10 بكسل.


Label

Figure: Label

مربع الإختيار CheckBox

CheckBox عبارة عن أداة تحتوى على حالتين إما منشظة أو غير منشطة و هى صندوق بعنوان أو بصورة و إذا كانت منشظة فسوف تكون هناك علامة (صح) بداخل الصندوق و يمكن استخدمها فى تحديد إظهار أو إخفاء شاشة الإقلاع أو أشرطة الأدوات.......إلخ.

checkbox.py

#!/usr/bin/ipy

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import Application, Form, CheckBox
from System.Drawing import Size, Point



class IForm(Form):

    def __init__(self):

        self.Text = "CheckBox"
        self.Size = Size(220, 170)

        cb = CheckBox()
        cb.Parent = self
        cb.Location = Point(30, 30)
        cb.Text = "Show Title"
        cb.Checked = True

        cb.CheckedChanged += self.OnChanged


        self.CenterToScreen()

    def OnChanged(self, sender, event):
        if sender.Checked:
            self.Text = "CheckBox"
        else:
            self.Text = ""
        
    
Application.Run(IForm())

و فى هذا المثال بيتم تحديد إظهار أو إخفار عنوان النافذة عن طريق حالة مربع الإختيار.

 cb = CheckBox()

تم إنشاء CheckBox.

 cb.Text = "Show Title"
 cb.Checked = True

عند بدأ البرنامج نعرض العنوان و بالتالى يتم تنشيط الأداة CheckBox.

 cb.CheckedChanged += self.OnChanged

عند الضغط على الأداة CheckBox الحدث CheckedChanged ينشط.

 if sender.Checked:
     self.Text = "CheckBox"
 else:
     self.Text = ""

هنا نربط تنشيط مربع الإختيار بظهور أو إخفاء عنوان النافذة.


CheckBox

Figure: CheckBox

شريط الإنزلاق TrackBar

TrackBar عبارة عن أداة تسمح للمستخدم بتحديد قيمة معينة عن طريق تحريك قطعة صغيرة رسومياً على شريط محدد بقيمة معينة و فى المثال سوف نعرض متحكم فى الصوت.

trackbar.py

#!/usr/bin/ipy

import sys
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import Application, Form, PictureBox
from System.Windows.Forms import TrackBar, TickStyle
from System.Drawing import Size, Point, Bitmap


class IForm(Form):

    def __init__(self):
        self.Text = 'TrackBar'
        self.Size = Size(260, 190)
      

        tb = TrackBar()
        tb.Parent = self
        tb.Size = Size(150, 30)
        tb.Location = Point(30, 50)
        tb.TickStyle = TickStyle.None
        tb.SetRange(0, 100)

        tb.ValueChanged += self.OnChanged

        self.LoadImages()

        self.pb = PictureBox()
        self.pb.Parent = self
        self.pb.Location = Point(210, 50)
        self.pb.Image = self.mutep
        
        self.CenterToScreen()


    def LoadImages(self):
        try:
            self.mutep = Bitmap("mute.png")
            self.minp = Bitmap("min.png")
            self.medp = Bitmap("med.png")
            self.maxp = Bitmap("max.png")
        except Exception, e:
            print "Error reading images"
            print e.msg
            sys.exit(1)
    


    def OnChanged(self, sender, event): 
        val = sender.Value

        if val == 0: 
            self.pb.Image = self.mutep
        elif val > 0 and val <= 30:
            self.pb.Image = self.minp
        elif val > 30 and val < 80:
            self.pb.Image = self.medp
        else: self.pb.Image = self.maxp



Application.Run(IForm())

فى الكود السابق عرضنا شريط انزلاق TrackBar و صندوق صورة PictureBox و بتحريك شريط الانزلاق نغير الصورة التى فى أداة صندوق الصورة PictureBox.

 tb = TrackBar()

تم إنشاء الأداة TrackBar.

 tb.TickStyle = TickStyle.None

لم نعرض أى تدريح لشريط الانزلاق.

 self.pb = PictureBox()
 ...
 self.pb.Image = self.mutep

تم إنشاء صندوق صورة PictureBox ليستخدم فى عرض الصورة و فى البداية يظهر صورة الصوت المكتوم.

 self.mutep = Bitmap("mute.png")
 self.minp = Bitmap("min.png")
 self.medp = Bitmap("med.png")
 self.maxp = Bitmap("max.png")

هنا أنشأنا أرعة صور سوف نستخدمها.

 val = sender.Value

 if val == 0: 
     self.pb.Image = self.mutep
 elif val > 0 and val <= 30:
     self.pb.Image = self.minp
 elif val > 30 and val < 80:
     self.pb.Image = self.medp
 else: self.pb.Image = self.maxp

نحدد قيمة شريط الانزلاق و بالإعتماد على قيمتها نحدث الثورة فى صندوق الصورة.


TrackBar

Figure: TrackBar

صندوق القائمة المنسدلةComboBox

ComboBox هى أداة تجمع بين زر أو مكان للنص قابل للتعديل و قائمة منسدلة و التى يمكن للمستخدم تحديد قيمه معينه منها و التى تظهر عند طلب المستخدم و إذا أردت أن تكون الأداة قابلة للتعديل فسوف يكون هناك مكان للمستخدم لكتابة نص بداخله.

combobox.py

#!/usr/bin/ipy

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")

from System.Windows.Forms import Application, Form
from System.Windows.Forms import ComboBox, Label
from System.Drawing import Size, Point


class IForm(Form):

    def __init__(self):

        self.Text = "ComboBox"
        self.Size = Size(240, 240)

        cb = ComboBox()
        cb.Parent = self
        cb.Location = Point(50, 30)

        cb.Items.AddRange(("Ubuntu",
            "Mandriva",
            "Red Hat",
            "Fedora",
            "Gentoo"))

        cb.SelectionChangeCommitted += self.OnChanged

        self.label = Label()
        self.label.Location = Point(50, 140)
        self.label.Parent = self
        self.label.Text = "..."


        self.CenterToScreen()

    def OnChanged(self, sender, event):
         self.label.Text = sender.Text
    
    
Application.Run(IForm())

الكود السابق يعرض صندوق قائمة منسدلة يحتى على خمس عناصر و العنصر المحدد يظهر فى أداة العنوان.

 cb = ComboBox()

تم إنشاء أدة ComboBox.

 cb.Items.AddRange(("Ubuntu",
     "Mandriva",
     "Red Hat",
     "Fedora",
     "Gentoo"))

تم ملأ الأداة بالعناصر.

 cb.SelectionChangeCommitted += self.OnChanged

إذا حددنا عنصر من صندوق القائمة المنسدلة فسوف ينشط الحدث SelectionChangeCommitted.

 def OnChanged(self, sender, event):
     self.label.Text = sender.Text

هنا سوف يظهر النص المحدد من صندوق القائمة المسندلة على أداة العنوان.


ComboBox

Figure: ComboBox

ها هنا أنهينا الجزء المخصص من الدرس للأداوت الأساسية فى مكتبات WinForms.


المحتويات