تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
مقال- أفكار في الجرافكس ....... الجزء الثاني
#4
التطبيق النهائي:

في المثال التالي سنحاول ان نبني باتون شكله لطيف وهي فكرة بسيطة جدا لكيفية استخدام الأفكار أعلاه


كود :
Imports System.Drawing.Drawing2D

Public Class RibbonButton
Inherits Control

Private DarkColor As Color
Private LightColor As Color
Private rect As Rectangle
Private Path As GraphicsPath
Private m_state As ButtonState = ButtonState.Normal

Public Sub New()

Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.UserPaint, True)
Me.UpdateStyles()

Me.SetNormalstate()
Me.SetGraphicsData()

End Sub

Protected Overrides ReadOnly Property DefaultSize() As System.Drawing.Size
Get
Return New Size(50, 50)
End Get
End Property

Private Sub SetGraphicsData()
Path = New GraphicsPath()
Path.AddEllipse(Me.ClientRectangle)
Dim rgn As New Region(Path)
Me.Region = rgn
rect = New Rectangle(Me.ClientRectangle.X + 1, Me.ClientRectangle.Y + 1, Me.ClientRectangle.Width - 2, Me.ClientRectangle.Height - 2)
End Sub

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

Select Case m_state

Case ButtonState.Default, ButtonState.Disabled, ButtonState.Normal
SetNormalstate()
DrawGlowRibbon(e.Graphics, rect, DarkColor, LightColor)

Exit Select

Case ButtonState.Hot
SetHoverstate()
DrawGlowRibbon(e.Graphics, rect, DarkColor, LightColor)
Exit Select
Case ButtonState.Pressed

SetPressedState()
DrawGlowRibbon(e.Graphics, rect, DarkColor, LightColor)
End Select

End Sub

Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
MyBase.OnMouseEnter(e)
m_state = ButtonState.Hot
Invalidate()
End Sub

Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)

m_state = ButtonState.Pressed
Invalidate()

End Sub

Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseUp(e)
OnMouseEnter(e)
End Sub

Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseMove(e)

If (e.Button And MouseButtons.Left) = MouseButtons.Left And Not rect.Contains(e.Location) And m_state = ButtonState.Pressed Then
OnMouseLeave(e)
End If
End Sub

Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
MyBase.OnMouseLeave(e)

m_state = ButtonState.Default
Invalidate()

End Sub

Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
MyBase.OnResize(e)
SetGraphicsData()
Invalidate()
End Sub

Protected Overrides Sub OnSizeChanged(ByVal e As EventArgs)
MyBase.OnSizeChanged(e)
SetGraphicsData()
End Sub

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

Private Sub SetNormalstate()
DarkColor = Color.Blue
LightColor = Color.Cyan
End Sub

Private Sub SetHoverstate()
DarkColor = Color.Maroon
LightColor = Color.LightYellow
End Sub

Private Sub SetPressedState()
DarkColor = Color.DarkOrange
LightColor = Color.Yellow
End Sub

#Region " Enum "

<Flags()> _
Private Enum ButtonState
Normal = &H0
Hot = &H1
Pressed = &H2
Disabled = &H4
[Default] = &H8
End Enum

#End Region

End Class

بالمرفقات ستجدون مثالا لكيفية استخدام الكلاس أعلاه وهو سيكون مثل أي باتون أخر ولكن بشكل مختلف قليلا

المثال بالمرفقات بنسخة الفيجوال استوديو 2008
أتمني ان تكون الأفكار التي تحدثنا عنها مفيدة للبعض منكم بشكل ما ...... أعتقد أنكم يمكنك تطوير الكلاس مثل رسم Text أو رسم صورة ما داخل Ribbon أو إضافة الكثير من Property له ليتناسب مع ما احتياجاتكم

أتمني أن نري إبداعاتكم المختلفة من خلال مشاركتكم اللاحقة

تقبلوا تحياتي
أخوكم عمر


الملفات المرفقة
.rar   GlowButtonExample.rar (الحجم : 64.47 ك ب / التحميلات : 63)
}}}
تم الشكر بواسطة:


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

المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  الجزء الثالث من:كيف تجعل الـ Text Box ذكي!يترجم العمليات الحسابية ويخرج الناتج (الأقواس المتعددة) !! أنس محمود 10 8,376 19-07-22, 12:15 AM
آخر رد: StartLight4000
  مقال: الكومبو بوكس ComboBox كيف تضيف أيقونات Blue Sky 1 3,452 30-06-19, 10:41 AM
آخر رد: invocker
  حساب قيمة معادلة(اقصد صيغة دون مجاهيل) مكتوبة بالتكست : الجزء الخامس والاخير محمد شريقي 4 4,826 23-02-18, 10:44 PM
آخر رد: العواد الصغير
  أفكار في الجرافكس AlignRectangle silverlight 0 1,714 14-10-17, 02:02 PM
آخر رد: silverlight
  مقدمة إلي إخفاء المعلومات - الجزء الأول silverlight 5 4,501 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,002 22-02-13, 12:39 AM
آخر رد: أنس محمود
  مقال- كيفية الاستغناء عن الداتا بيز التقليدية في برامجنا – ألجزء الأول RaggiTech 1 3,684 06-10-12, 12:23 AM
آخر رد: RaggiTech
  مقال- تطوير الكونترول Property Attributes الجزء الثالث RaggiTech 0 2,470 06-10-12, 12:20 AM
آخر رد: RaggiTech

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


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