03-10-12, 01:54 AM
كاتب الموضوع : samerselo
تستخدم الـ GDI+ فئتان Class أساسيتان هما Pen و Brush لتحديد مظهر ما تقوم برسمه. حيث يحدد Pen Object كيفية رسم الخطوط كاللون والسماكة والاستمرارية والتنقيط فمثلا عندما تستخدم الدالة DrawPolygon لرسم مضلع فأنت تمرر لها Pen Object يحدد كيفية رسم خطوط ذلك المضلع في حين يحدد Brush Object كيفية ملئ المساحات كاللون وطريقة التهشير والتدرج اللوني فمثلا يمكنك استخدام Brush Object لتحديد كيفية ملئ المساحات المغلقة كما نفعل عند استخدام الدالة FillRectangle لرسم مستطيل مملوء.ويمكن تحديد الخطوات الأساسية لرسم أي شكل بشكل عام
1. إنشاء Graphics Objetc
2. تحديد Brush Object لملئ المساحات في ذلك الشكل
3. تحديد Pen Object لتحديد كيفية رسم خطوط ذلك الشكل
4. القيام بعملية الرسم باستخدام الدالة المناسبة لما نريد رسمه
فمثلا يمكنك استخدام الحدث Paint للنموذج للرسم عليه عندما يتم إعادة رسم النموذج كما في قطعة الكود التالية حيث يقدم لك إجراء الحدث Paint الوسيطة e.Graphics التي هي الـ Graphics Object الذي نستخدمه للرسم وعند انتهاء الحدث يقوم فيجول بايزيك بنقل ما تم رسمه على الـ Graphics Object إلى المنطقة المناسبة على النموذج التي يجب أن يتم الرسم عليها. حيث نقوم بإنشاء كائن SolidBrush المشتق من الكائن Brush الذي سنستخدمه لرسم دائرة مملوءة بتمريره للدالة FillEllipse ثم نقوم بإنشاء كائن Pen نقوم بتمريره للدالة DrawEllipse المستخدمة هنا لرسم دائرة تحيط بالدائرة السابقة
كود :
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim Sb As New SolidBrush(Color.YellowGreen)
e.Graphics.FillEllipse(Sb, 10, 10, 100, 100)
Dim Cp As New Pen(Color.Blue, 5)
e.Graphics.DrawEllipse(Cp, 10, 10, 100, 100)
End Sub
كود :
Graphics.FillEllipse (Brush, Rectangle)
Graphics.FillEllipse (Brush, RectangleF)
Graphics.FillEllipse (Brush, Int32, Int32, Int32, Int32)
Graphics.FillEllipse (Brush, Single, Single, Single, Single)
كود :
Dim Sb As New SolidBrush(Color.YellowGreen)
Dim Rc As New Rectangle(10, 10, 100, 100)
e.Graphics.FillEllipse(Sb, Rc)
Dim Cp As New Pen(Color.Blue, 5)
e.Graphics.DrawEllipse(Cp, Rc)
يمكنك استخدام فضاء الأسماء System.Drawing.Text لرسم الخطوط بحيث يحتوي على ثلاث فئات أساسية FontCollection و InstalledFontCollection و PrivateFontCollection
فللحصول على قائمة بالخطوط المركبة على الجهاز نستخدم الفئة InstalledFontCollection التي نستخدم دالتها Families للحصول على مصفوفة FontFamily تتضمن الخطوط المركبة حيث يمكننا ملئ قائمة بالخطوط المركبة على الجهاز كما في المثال
كود :
Dim Ifonts As New Drawing.Text.InstalledFontCollection
Dim Ffamilies() As FontFamily = Ifonts.Families()
For Each Ffamily As FontFamily In Ffamilies
lstFonts.Items.Add(Ffamily.Name)
Next Ffamily
lstFonts.SelectedIndex = 0
كود :
Dim the_font As New Font(Me.Font.FontFamily, _
40, FontStyle.Bold, GraphicsUnit.Pixel)
' Draw without anti-aliasing.
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
e.Graphics.DrawString("Alias", _
the_font, Brushes.Black, 5, 5)
' Draw with anti-aliasing.
e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel
e.Graphics.DrawString("Alias", _
the_font, Brushes.Black, 5, 50)
تمثل قطعة الكود التالية مثالا على استخدام الدالة DrawBezier حيث يبدأ الكود بتعريف مصفوفة نقاط من النوع Point تضم النقاط التي نريد استخدامها لرسم المنحنى ثم ننشئ قلم مخطط بلون أسود نستخدمه من خلال الدالة DrawLine لرسم خط يصل بين تلك النقاط ثم ننشئ قلم بلون أزرق نمرره للدالة DrawBezier لرسم المنحنى
كود :
' Define the Bezier curve’s control points.
Dim pts() As Point = { _
New Point(10, 10), _
New Point(200, 10), _
New Point(50, 200), _
New Point(200, 150)}
' Connect the points with dashed lines.
Dim dashed_pen As New Pen(Color.Black, 0)
dashed_pen.DashStyle = Drawing2D.DashStyle.Dash
For i As Integer = 0 To 2
e.Graphics.DrawLine(dashed_pen, pts(i), pts(i + 1))
Next i
' Draw the Bezier curve.
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
Dim bez_pen As New Pen(Color.Blue, 3)
e.Graphics.DrawBezier(bez_pen, pts(0), pts(1), pts(2), pts(3))
كود :
' Define the Bezier curve’s control points.
Dim pts() As Point = { _
New Point(10, 10), _
New Point(200, 10), _
New Point(50, 200), _
New Point(200, 150), _
New Point(250, 50), _
New Point(250, 200), _
New Point(100, 250)}
' Draw the Bezier curve.
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
Dim bez_pen As New Pen(Color.BlueViolet, 3)
e.Graphics.DrawBeziers(bez_pen, pts)
كود :
' Define the curve’s control points.
Dim pts() As Point = { _
New Point(10, 50), _
New Point(200, 30), _
New Point(20, 200), _
New Point(200, 150), _
New Point(250, 50), _
New Point(250, 200), _
New Point(100, 250)}
' Draw the closed curve.
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
Dim curve_pen As New Pen(Color.Black, 3)
e.Graphics.DrawClosedCurve(curve_pen, pts)
كود :
' Create a GraphicsPath.
Dim graphics_path As New Drawing2D.GraphicsPath
' Add some text to the path.
graphics_path.AddString("GraphicsPath", _
New FontFamily("Times New Roman"), _
CInt(FontStyle.Bold), _
80, New Point(10, 10), _
StringFormat.GenericTypographic)
' Draw the path.
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
e.Graphics.FillPath(Brushes.Yellow, graphics_path)
e.Graphics.DrawPath(New Pen(Color.Blue, 3), graphics_path)
كود :
Dim graphics_path As New Drawing2D.GraphicsPath
graphics_path.AddEllipse(10, 10, 50, 75)
graphics_path.AddRectangle(New Rectangle(50, 50, 100, 80))
graphics_path.AddPie(100, 50, 120, 200, 45, 210)
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
e.Graphics.FillPath(Brushes.Yellow, graphics_path)
e.Graphics.DrawPath(New Pen(Color.Blue, 3), graphics_path)