05-10-12, 11:40 AM
الكود التالي يوضح شكل الكود الموجود بالمثال الثالث وذلك بعد تحويل جزء من الكود الي دوال Functions و روتينات Method/Sub
وستلاحظ عزيز القارئ ان الكود الموجود في الحدث Paint قد اصبح أقل بكثير عن سابقة في المثال الثالث وهنا تظهر أهمية تحويل اجزاء من الكود الي دوال أو روتينان.
المثال الرابع:
في المشاركة التالية سنعطي مثالا لكيفية استخدام الأفكار السابقة في بناء شئ مفيد
تقبلوا تحياتي
أخوكم عمر
وستلاحظ عزيز القارئ ان الكود الموجود في الحدث 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في المشاركة التالية سنعطي مثالا لكيفية استخدام الأفكار السابقة في بناء شئ مفيد
تقبلوا تحياتي
أخوكم عمر
