تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
مقال- أفكار في الجرافكس ..... الجزء الثالث
#3
الكود التالي يوضح شكل الكود الموجود بالمثال الثالث وذلك بعد تحويل جزء من الكود الي دوال Functions و روتينات Method/Sub

وستلاحظ عزيز القارئ ان الكود الموجود في الحدث Paint قد اصبح أقل بكثير عن سابقة في المثال الثالث وهنا تظهر أهمية تحويل اجزاء من الكود الي دوال أو روتينان.


المثال الرابع:


كود :
Public Class Form1

Dim colorLightLight As Color = ControlPaint.Light(Color.SteelBlue)
Dim colorDarkDark As Color = (Color.Blue)
Dim colorDark As Color = (Color.MediumBlue)
Dim colorLight As Color = ControlPaint.Light(Color.Cyan)
Dim colorFrameOut As Color = ControlPaint.Dark(Color.Navy)
Dim colorFrameIn As Color = ControlPaint.Light(Color.LightSteelBlue)
Dim rect As New Rectangle(20, 20, 100, 25)
Dim rect1 As New Rectangle(20, 50, 100, 25)

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)

DrawGlow(e.Graphics, rect, colorLightLight, colorDarkDark, colorDark, colorLight, 5)
DrawGlow(e.Graphics, rect1, colorLightLight, colorDarkDark, colorDark, colorLight, colorFrameOut, colorFrameIn, 5)

End Sub

Private Function GetBorderPath(ByVal rect As Rectangle, ByVal displacement As Single) As GraphicsPath

rect.Width -= 1
rect.Height -= 1
Dim path As New GraphicsPath

path.AddLine((rect.Left + displacement), CSng(rect.Top), (rect.Right - displacement), CSng(rect.Top))
path.AddLine((rect.Right - displacement), CSng(rect.Top), CSng(rect.Right), (rect.Top + displacement))
path.AddLine(CSng(rect.Right), (rect.Top + displacement), CSng(rect.Right), (rect.Bottom - displacement))
path.AddLine(CSng(rect.Right), (rect.Bottom - displacement), (rect.Right - displacement), CSng(rect.Bottom))
path.AddLine((rect.Right - displacement), CSng(rect.Bottom), (rect.Left + displacement), CSng(rect.Bottom))
path.AddLine((rect.Left + displacement), CSng(rect.Bottom), CSng(rect.Left), (rect.Bottom - displacement))
path.AddLine(CSng(rect.Left), (rect.Bottom - displacement), CSng(rect.Left), (rect.Top + displacement))
path.AddLine(CSng(rect.Left), (rect.Top + displacement), (rect.Left + displacement), CSng(rect.Top))
Return path

End Function

Private Function GetBorderPath(ByVal rect As Rectangle, ByVal exclude As Rectangle, ByVal displacement As Single) As GraphicsPath

If exclude.IsEmpty Then
Return GetBorderPath(rect, displacement)
End If

rect.Width -= 1
rect.Height -= 1

Dim list As New List(Of PointF)

Dim x As Single = rect.X
Dim y As Single = rect.Y
Dim right As Single = rect.Right
Dim bottom As Single = rect.Bottom

Dim pt1 As Single = (rect.X + displacement)
Dim pt2 As Single = (rect.Right - displacement)
Dim pt3 As Single = (rect.Y + displacement)
Dim pt4 As Single = (rect.Bottom - displacement)
Dim pt5 As Single = IIf((displacement = 0.0F), 1.0F, displacement)

If ((rect.Y >= exclude.Top) AndAlso (rect.Y <= exclude.Bottom)) Then

Dim pt6 As Single = ((exclude.X - 1) - displacement)
Dim pt7 As Single = (exclude.Right + displacement)

If (pt1 <= pt6) Then
list.Add(New PointF(pt1, y))
list.Add(New PointF(pt6, y))
list.Add(New PointF((pt6 + displacement), (y - pt5)))
Else
pt6 = (exclude.X - 1)
list.Add(New PointF(pt6, y))
list.Add(New PointF(pt6, (y - pt5)))
End If

If (pt2 > pt7) Then
list.Add(New PointF((pt7 - displacement), (y - pt5)))
list.Add(New PointF(pt7, y))
list.Add(New PointF(pt2, y))
Else
pt7 = exclude.Right
list.Add(New PointF(pt7, (y - pt5)))
list.Add(New PointF(pt7, y))
End If

Else
list.Add(New PointF(pt1, y))
list.Add(New PointF(pt2, y))
End If

list.Add(New PointF(right, pt3))
list.Add(New PointF(right, pt4))
list.Add(New PointF(pt2, bottom))
list.Add(New PointF(pt1, bottom))
list.Add(New PointF(x, pt4))
list.Add(New PointF(x, pt3))

Dim path As New GraphicsPath

For i As Integer = 1 To list.Count - 1
path.AddLine(list.Item((i - 1)), list.Item(i))
Next

path.AddLine(list.Item((list.Count - 1)), list.Item(0))
Return path

End Function

Private Function GetBorderPath(ByVal rect As Rectangle) As GraphicsPath
Return GetBorderPath(rect, 0)
End Function

Private Function GetBorderPath(ByVal rect As Rectangle, ByVal radius As Integer) As GraphicsPath
Dim path As New GraphicsPath
If (radius = 0) Then
path.AddRectangle(rect)
Return path
End If
Return GetRoundedPath(rect, radius)
End Function

Private Function GetRoundedPath(ByVal rect As Rectangle, ByVal radius As Integer) As GraphicsPath
Return GetRoundedPath(rect, radius, radius, radius, radius)
End Function

Private Function GetRoundedPath(ByVal rect As Rectangle, _
ByVal TopLeftRaduis As Integer, _
ByVal TopRightRaduis As Integer, _
ByVal BottomLeftRadius As Integer, _
ByVal BottomRightRadius As Integer) As GraphicsPath

If ((TopLeftRaduis = 0) OrElse (TopLeftRaduis < 0)) Then
TopLeftRaduis = 1
End If

If ((TopRightRaduis = 0) OrElse (TopRightRaduis < 0)) Then
TopRightRaduis = 1
End If

If ((BottomLeftRadius = 0) OrElse (BottomLeftRadius < 0)) Then
BottomLeftRadius = 1
End If

If ((BottomRightRadius = 0) OrElse (BottomRightRadius < 0)) Then
BottomRightRadius = 1
End If

Dim x As Integer = rect.X
Dim y As Integer = rect.Y
Dim width As Integer = rect.Width
Dim height As Integer = rect.Height
Dim path As New GraphicsPath

path.AddArc(x, y, TopLeftRaduis, TopLeftRaduis, 180.0F, 90.0F)

If (TopRightRaduis <> 1) Then
path.AddArc(((x + width) - TopRightRaduis), y, TopRightRaduis, TopRightRaduis, 270.0F, 90.0F)
Else
path.AddLine((x + width), y, (x + width), width)
End If

If (BottomRightRadius <> 1) Then
path.AddArc(((x + width) - BottomRightRadius), ((y + height) - BottomRightRadius), BottomRightRadius, BottomRightRadius, 0.0F, 90.0F)
Else
path.AddLine(CInt((x + width)), CInt((y + height)), CInt((x + width)), CInt((y + height)))
End If

path.AddArc(x, ((y + height) - BottomLeftRadius), BottomLeftRadius, BottomLeftRadius, 90.0F, 90.0F)
path.CloseFigure()

Return path

End Function

Private Sub DrawBorder(ByVal g As Graphics, _
ByVal rect As Rectangle, _
ByVal borderColor As Color, _
ByVal TopLeftRaduis As Integer, _
ByVal TopRightRaduis As Integer, _
ByVal BottomLeftRadius As Integer, _
ByVal BottomRightRadius As Integer)

Using borderPen As Pen = New Pen(borderColor)
Using path As GraphicsPath = GetRoundedPath(rect, TopLeftRaduis, TopRightRaduis, BottomLeftRadius, BottomRightRadius)
g.DrawPath(borderPen, path)
End Using
End Using
End Sub

Private Sub DrawBorder(ByVal g As Graphics, ByVal rect As Rectangle, ByVal borderColor As Color)
DrawBorder(g, rect, borderColor, 0)
End Sub

Private Sub DrawBorder(ByVal g As Graphics, ByVal rect As Rectangle, ByVal borderColor As Color, ByVal radius As Integer)

Using borderPen As Pen = New Pen(borderColor)
Using path As GraphicsPath = GetBorderPath(rect, radius)
g.DrawPath(borderPen, path)
End Using
End Using

End Sub

Private Sub DrawGlow(ByVal g As Graphics, _
ByVal rect As Rectangle, _
ByVal colorLightLight As Color, _
ByVal colorDarkDark As Color, _
ByVal colorDark As Color, _
ByVal colorLight As Color, _
ByVal colorFrameOut As Color, _
ByVal colorFrameIn As Color, _
ByVal radius As Integer)

Dim mode As SmoothingMode = g.SmoothingMode
g.SmoothingMode = SmoothingMode.AntiAlias

Dim r As New Rectangle(rect.X, rect.Y, rect.Width, (rect.Height / 2))

Using lgb As LinearGradientBrush = New LinearGradientBrush(r, colorLightLight, colorDarkDark, 90.0F, True)

Using lgb1 As LinearGradientBrush = New LinearGradientBrush(r, colorDark, colorLight, -90.0F, True)

lgb1.WrapMode = WrapMode.TileFlipXY
lgb.WrapMode = WrapMode.TileFlipXY

Dim r1 As New Rectangle(rect.X, rect.Y, rect.Width, ((rect.Height / 2) + 1))
g.FillPath(lgb, GetRoundedPath(r1, radius, radius, 0, 0))

Dim path As New GraphicsPath

Dim r2 As New Rectangle((rect.X - 10), (rect.Y + (rect.Height / 2)), (rect.Width + 20), rect.Height)
Dim r3 As New Rectangle(rect.X, (rect.Y + (rect.Height / 2)), rect.Width, ((rect.Height / 2) + 1))

path.AddRectangle(r2)

Dim pgb As New PathGradientBrush(path)
pgb.SurroundColors = New Color() {colorDark, colorDark, ControlPaint.Light(colorLight), ControlPaint.Light(colorLight)}
pgb.CenterColor = ControlPaint.Light(colorLight)
g.FillPath(pgb, GetRoundedPath(r3, 0, 0, radius, radius))
path.Dispose()
End Using
End Using

Dim r4 As New Rectangle((rect.X + 1), (rect.Y + 1), (rect.Width - 2), (rect.Height - 2))

DrawBorder(g, r4, colorFrameIn, radius)
DrawBorder(g, rect, colorFrameOut, radius)

g.SmoothingMode = mode

End Sub

Private Sub DrawGlow(ByVal g As Graphics, _
ByVal rect As Rectangle, _
ByVal colorLightLight As Color, _
ByVal colorDarkDark As Color, _
ByVal colorDark As Color, _
ByVal colorLight As Color, _
ByVal radius As Integer)

Dim mode As SmoothingMode = g.SmoothingMode
g.SmoothingMode = SmoothingMode.AntiAlias
Dim r As New Rectangle(rect.X, rect.Y, rect.Width, (rect.Height / 2))
Using lgb As LinearGradientBrush = New LinearGradientBrush(r, colorLightLight, colorDarkDark, 90.0F, True)

Using lgb1 As LinearGradientBrush = New LinearGradientBrush(r, colorDark, colorLight, -90.0F, True)

lgb1.WrapMode = WrapMode.TileFlipXY
lgb.WrapMode = WrapMode.TileFlipXY

Dim r1 As New Rectangle(rect.X, rect.Y, rect.Width, ((rect.Height / 2) + 1))
g.FillPath(lgb, GetRoundedPath(r1, radius, radius, 0, 0))

Dim path As New GraphicsPath

Dim r2 As New Rectangle((rect.X - 10), (rect.Y + (rect.Height / 2)), (rect.Width + 20), rect.Height)
Dim r3 As New Rectangle(rect.X, (rect.Y + (rect.Height / 2)), rect.Width, ((rect.Height / 2) + 1))

path.AddRectangle(r2)

Dim pgb As New PathGradientBrush(path)
pgb.SurroundColors = New Color() {colorDark, colorDark, ControlPaint.Light(colorLight), ControlPaint.Light(colorLight)}
pgb.CenterColor = ControlPaint.Light(colorLight)
g.FillPath(pgb, GetRoundedPath(r3, 0, 0, radius, radius))
path.Dispose()
End Using
End Using


g.SmoothingMode = mode

End Sub

End Class

في المشاركة التالية سنعطي مثالا لكيفية استخدام الأفكار السابقة في بناء شئ مفيد


تقبلوا تحياتي
أخوكم عمر
}}}
تم الشكر بواسطة:


الردود في هذا الموضوع
مقال- أفكار في الجرافكس ..... الجزء الثالث - بواسطة Raggi Tech - 05-10-12, 11:40 AM

المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  الجزء الثالث من:كيف تجعل الـ Text Box ذكي!يترجم العمليات الحسابية ويخرج الناتج (الأقواس المتعددة) !! أنس محمود 10 8,383 19-07-22, 12:15 AM
آخر رد: StartLight4000
  مقال: الكومبو بوكس ComboBox كيف تضيف أيقونات Blue Sky 1 3,454 30-06-19, 10:41 AM
آخر رد: invocker
  حساب قيمة معادلة(اقصد صيغة دون مجاهيل) مكتوبة بالتكست : الجزء الخامس والاخير محمد شريقي 4 4,827 23-02-18, 10:44 PM
آخر رد: العواد الصغير
  أفكار في الجرافكس AlignRectangle silverlight 0 1,715 14-10-17, 02:02 PM
آخر رد: silverlight
  مقدمة إلي إخفاء المعلومات - الجزء الأول silverlight 5 4,503 07-01-17, 10:44 PM
آخر رد: Basil Abdallah
  مقدمة إلي إخفاء المعلومات - الجزء الثاني silverlight 1 3,209 06-01-17, 11:52 AM
آخر رد: silverlight
  تحويل الفيديو في برامجك-الجزء الثاني( إصلاح للمشاكل + تعديل للروابط + توضيح للأمر ) RaggiTech 1 3,509 10-12-14, 06:37 PM
آخر رد: abulayth
  الجزء الثاني من:كيف تجعل الـ Text Box ذكي!يترجم العمليات الحسابية ويخرج الناتج ( العمليات المتعددة)! أنس محمود 0 3,003 22-02-13, 12:39 AM
آخر رد: أنس محمود
  مقال- كيفية الاستغناء عن الداتا بيز التقليدية في برامجنا – ألجزء الأول RaggiTech 1 3,684 06-10-12, 12:23 AM
آخر رد: RaggiTech
  مقال- تطوير الكونترول Property Attributes الجزء الثالث RaggiTech 0 2,474 06-10-12, 12:20 AM
آخر رد: RaggiTech

التنقل السريع :


يقوم بقرائة الموضوع: