Control to semulate LED

Hi,

This is the first time I write in Microsoft Forums so I like to share some of my 'home made' controls Check out this control, it semulates LED, it's useful in desiging hardware automation programs, PCB semulators, .. etc.

Just create a new 'User Control' and replace all code inside it with the following:

Imports System.ComponentModel.EditorAttribute

Public Class LED

Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'UserControl overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

'

'LED

'

Me.Name = "LED"

Me.Size = New System.Drawing.Size(136, 136)

End Sub

#End Region

Private Sub LED_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub LED_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

Try

Dim gx As Graphics = e.Graphics

Dim rx As Rectangle = e.ClipRectangle

Dim BackBrush As Brush = New SolidBrush(Me.BackColor)

Dim LEDEdge As New Drawing.Drawing2D.GraphicsPath(Drawing2D.FillMode.Winding)

Dim SC As Color() = {Me.ForeColor}

If CircleShape Then

LEDEdge.AddEllipse(rx)

Else

LEDEdge.AddRectangle(rx)

End If

Dim CenterPoint As Point = New Point(rx.Width / 2, rx.Height / 2)

Dim LEDBrush As Drawing2D.PathGradientBrush = New Drawing2D.PathGradientBrush(LEDEdge)

Dim TextBrush As Brush = New SolidBrush(Color.Black)

If LEDON Then

LEDBrush.CenterColor = SetHue(Me.ForeColor, 200)

Else

LEDBrush.CenterColor = SetHue(Me.ForeColor, -20)

End If

LEDBrush.CenterPoint = New Drawing.PointF(CenterPoint.X, CenterPoint.Y)

LEDBrush.SurroundColors = SC

gx.FillRectangle(BackBrush, rx)

gx.FillPath(LEDBrush, LEDEdge)

Dim LeftText As Integer = Me.Width / 2

Dim TopText As Integer = Me.Height / 2

If Me.Text <> "" Then

Dim BF As Font = New Font(Me.Font.FontFamily, Me.Font.Size)

LeftText -= gx.MeasureString(Me.Text, BF).Width / 2

TopText -= gx.MeasureString(Me.Text, BF).Height / 2

gx.DrawString(Me.Text, BF, New SolidBrush(Color.Black), LeftText + 1, TopText + 1)

LeftText = Me.Width / 2

TopText = Me.Height / 2

LeftText -= gx.MeasureString(Me.Text, Me.Font).Width / 2

TopText -= gx.MeasureString(Me.Text, Me.Font).Height / 2

gx.DrawString(Me.Text, Me.Font, New SolidBrush(Color.White), LeftText, TopText)

End If

Catch ex As Exception

Debug.Write(ex.ToString)

End Try

End Sub

Private Sub LED_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize

Me.Refresh()

End Sub

Private Sub LED_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged

Me.Refresh()

End Sub

Private Sub LED_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged

Me.Refresh()

End Sub

Private _LEDON As Boolean

Public Property LEDON() As Boolean

Get

LEDON = _LEDON

'me.Refresh

End Get

Set(ByVal Value As Boolean)

_LEDON = Value

Me.Refresh()

End Set

End Property

Private _CircleShape As Boolean = True

Public Property CircleShape() As Boolean

Get

CircleShape = _CircleShape

End Get

Set(ByVal Value As Boolean)

_CircleShape = Value

Me.Refresh()

End Set

End Property

Private Function SetHue(ByVal valColor As Drawing.Color, ByVal valHue As Integer) As Color

Dim TempColor As Color

Dim TempR As Integer

Dim TempG As Integer

Dim TempB As Integer

TempR = valColor.R + valHue

If TempR > 255 Then

TempR = 255

ElseIf TempR < 0 Then

TempR = 0

End If

TempG = valColor.G + valHue

If TempG > 255 Then

TempG = 255

ElseIf TempG < 0 Then

TempG = 0

End If

TempB = valColor.B + valHue

If TempB > 255 Then

TempB = 255

ElseIf TempB < 0 Then

TempB = 0

End If

TempColor = Color.FromArgb(TempR, TempG, TempB)

Return TempColor

End Function

Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)

Me.Refresh()

End Sub

End Class



Enjoy :)

Abdullah Al-Rasheed



Answer this question

Control to semulate LED

  • Control to semulate LED