05-10-12, 11:24 AM
في المثال التالي سنحاول أن نخلط بين جميع الأمثلة السابقة لكي نخرج بالشكل النهائي لما نريد أن نرسمه ومن ثم نحاول ان نكتب روتين نستخدمه فيما بعد في اي مشروع
المثال التاسع:
بالتأكيد يمكننا ان نرسم ما نريد أو نعدل بالكود كيفما شئنا ولكنك سوف تلاحظ عزيزي القارئ أننا لم نأتي بشئ جديد كل ما في الأمر أننا استخدمنا اوامر +GDI المختلفة والموجودة بالفعل بالدوت نت ولكن بصورة أكثر فعالية حتي نحصل علي الشكل النهائي لما نريد
وفي الكود أعلاه حاول أن تلاحظ كيفية استخدام الكلاسات Blend و LinearGradientBrush و PathGradientBrush ولاحظ أيضا كيف نتأكد من Brightness الخاصة بلون ما فإذا كانت أقل من ما نريد فإننا نقوم بتغيير اللون الي لون أخر قاتم قليلا ولكنه يأتي من نفس اللون
الكود التالي يوضح شكل الروتين النهائي الذي من الممكن ان نستخدمه في أي مشروع
بحيث نمرر له بعض المتغيرات مثل Graphics Object و Rectangle وأيضا لونين مختلفين أحدهما سيمثل اللون الغامق والأخر عبارة عن اللون الفاتح
المثال التاسع:
كود :
Imports System.Drawing.Drawing2D
Public Class Form1
Private DarkColor As Color = Color.DarkOrange
Private LightColor As Color = Color.Orange
Private rect As New Rectangle(10, 10, 150, 150)
Private angle As Integer = 180
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim DarkColor As Color = Color.Orange
Dim LightColor As Color = Color.Yellow
Dim rect As New Rectangle(10, 10, 100, 100)
Dim WhiteColor As Color = Color.White
Dim darkRatio As Integer = 16
Dim scale As SizeF = New SizeF(CSng(rect.Width / 100), CSng(rect.Height / 100))
Dim sigmaBlend As New Blend
sigmaBlend.Factors = New Single() {0.0F, 0.4F, 0.8F, 1.0F}
sigmaBlend.Positions = New Single() {0.0F, 0.3F, 0.4F, 1.0F}
Dim darkerColor As Color = DarkColor
Dim mode As SmoothingMode = e.Graphics.SmoothingMode
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
Dim darkerColorAlpha As Integer = darkerColor.A
Dim darkerColorRed As Integer = darkerColor.R - CInt(((darkerColor.R) / 100) * darkRatio)
Dim darkerColorGreen As Integer = darkerColor.G - CInt(((darkerColor.G) / 100) * darkRatio)
Dim darkerColorBlue As Integer = darkerColor.B - CInt(((darkerColor.B) / 100) * darkRatio)
Dim glowColor As Color = Color.FromArgb(darkerColorAlpha, darkerColorRed, darkerColorGreen, darkerColorBlue)
Using Path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath()
Path.AddEllipse(rect)
Dim pgb As New Drawing2D.PathGradientBrush(Path)
pgb.CenterPoint = New PointF(rect.Left + (rect.Width / 2), rect.Bottom + (33 * scale.Height))
pgb.CenterColor = Color.FromArgb(255, LightColor)
' Creates a gradient falloff based on a bell-shaped curve.
pgb.SetSigmaBellShape(0.85)
If darkerColor.GetBrightness >= 0.3 Then
darkerColor = glowColor
End If
pgb.SurroundColors = New Color() {Color.FromArgb(255, darkerColor)}
e.Graphics.FillEllipse(pgb, rect)
End Using
Using Path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath()
Path.AddArc(rect, angle, angle)
Using p As Pen = New Pen(glowColor)
Using lgb As New LinearGradientBrush(Path.GetBounds(), Color.FromArgb(64, WhiteColor), Color.FromArgb(84, WhiteColor), LinearGradientMode.Vertical)
lgb.Blend = sigmaBlend
e.Graphics.FillPath(lgb, Path)
End Using
e.Graphics.DrawPath(p, Path)
End Using
End Using
Dim blender As Drawing2D.Blend = New Drawing2D.Blend()
blender.Positions = New Single() {0, 0.9, 1}
blender.Factors = New Single() {0, 1, 1}
Dim sweepRectangle As Rectangle = rect
sweepRectangle.Height -= (rect.Height / 2)
sweepRectangle.Inflate(-16 * scale.Width, -3 * scale.Height)
Using lgb As New Drawing2D.LinearGradientBrush(sweepRectangle, WhiteColor, Color.Empty, 90, True)
sweepRectangle.Height -= scale.Height / 2
lgb.Blend = blender
e.Graphics.FillEllipse(lgb, sweepRectangle)
End Using
Using borderPen As New Pen(glowColor, 2)
Dim borderRect As Rectangle = rect
e.Graphics.DrawEllipse(borderPen, borderRect)
End Using
e.Graphics.SmoothingMode = mode
End Sub
End Classبالتأكيد يمكننا ان نرسم ما نريد أو نعدل بالكود كيفما شئنا ولكنك سوف تلاحظ عزيزي القارئ أننا لم نأتي بشئ جديد كل ما في الأمر أننا استخدمنا اوامر +GDI المختلفة والموجودة بالفعل بالدوت نت ولكن بصورة أكثر فعالية حتي نحصل علي الشكل النهائي لما نريد
وفي الكود أعلاه حاول أن تلاحظ كيفية استخدام الكلاسات Blend و LinearGradientBrush و PathGradientBrush ولاحظ أيضا كيف نتأكد من Brightness الخاصة بلون ما فإذا كانت أقل من ما نريد فإننا نقوم بتغيير اللون الي لون أخر قاتم قليلا ولكنه يأتي من نفس اللون
الكود التالي يوضح شكل الروتين النهائي الذي من الممكن ان نستخدمه في أي مشروع
بحيث نمرر له بعض المتغيرات مثل Graphics Object و Rectangle وأيضا لونين مختلفين أحدهما سيمثل اللون الغامق والأخر عبارة عن اللون الفاتح
كود :
Private Sub DrawGlowRibbon(ByVal g As Graphics, ByVal rect As Rectangle, ByVal darkColor As Color, ByVal lightColor As Color)
Dim mode As SmoothingMode = g.SmoothingMode
g.SmoothingMode = SmoothingMode.AntiAlias
Dim darkRatio As Integer = 16
Dim WhiteColor As Color = Color.White
Dim sigmaBlend As New Blend
sigmaBlend.Factors = New Single() {0.0F, 0.4F, 0.8F, 1.0F}
sigmaBlend.Positions = New Single() {0.0F, 0.3F, 0.4F, 1.0F}
Dim scale As SizeF = New SizeF(CSng(rect.Width / 100), CSng(rect.Height / 100))
Dim darkerColor As Color = darkColor
Dim darkerColorAlpha As Integer = darkerColor.A
Dim darkerColorRed As Integer = darkerColor.R - CInt(((darkerColor.R) / 100) * darkRatio)
Dim darkerColorGreen As Integer = darkerColor.G - CInt(((darkerColor.G) / 100) * darkRatio)
Dim darkerColorBlue As Integer = darkerColor.B - CInt(((darkerColor.B) / 100) * darkRatio)
Dim glowColor As Color = Color.FromArgb(darkerColorAlpha, darkerColorRed, darkerColorGreen, darkerColorBlue)
Using Path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath()
Path.AddEllipse(rect)
Dim pgb As New Drawing2D.PathGradientBrush(Path)
pgb.CenterPoint = New PointF(rect.Left + (rect.Width / 2), rect.Bottom + (33 * scale.Height))
pgb.CenterColor = Color.FromArgb(255, lightColor)
pgb.SetSigmaBellShape(0.85)
If darkerColor.GetBrightness >= 0.3 Then
darkerColor = glowColor
End If
pgb.SurroundColors = New Color() {Color.FromArgb(255, darkerColor)}
g.FillEllipse(pgb, rect)
End Using
Using Path As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath()
Path.AddArc(rect, 180, 180)
Using p As Pen = New Pen(glowColor)
Using lgb As New LinearGradientBrush(Path.GetBounds(), Color.FromArgb(64, WhiteColor), Color.FromArgb(84, WhiteColor), LinearGradientMode.Vertical)
lgb.Blend = sigmaBlend
g.FillPath(lgb, Path)
End Using
g.DrawPath(p, Path)
End Using
End Using
Dim blender As Drawing2D.Blend = New Drawing2D.Blend()
blender.Positions = New Single() {0, 0.9, 1}
blender.Factors = New Single() {0, 1, 1}
Dim sweepRectangle As Rectangle = rect
sweepRectangle.Height -= (rect.Height / 2)
sweepRectangle.Inflate(-16 * scale.Width, -3 * scale.Height)
Using lgb As New Drawing2D.LinearGradientBrush(sweepRectangle, WhiteColor, Color.Empty, 90, True)
sweepRectangle.Height -= scale.Height / 2
lgb.Blend = blender
g.FillEllipse(lgb, sweepRectangle)
End Using
Using borderPen As New Pen(darkerColor, 2)
Dim borderRect As Rectangle = rect
g.DrawEllipse(borderPen, borderRect)
End Using
g.SmoothingMode = mode
End Sub