先来看一下效果图:
首先分析它的显示原理,
一、先把每一个数字都看成是一个8字的一部分,这里说的8是这个,把整个电子时钟分成八份,其中有六个。可以很容易的看出来这个8是有七根线构成的。
二、每次都是判断现在的时间,然后再根据相应的时间画出相应的线段。
再来说下整个程序的设计。
一、界面;
界面就是用来一个图片框作为背景(可以用窗体)。没有别的。
二、实现;
用类来实现。
①、分析类应该具备的功能:
能够得到系统的时间并且将时间的每个字符分离出来单独判断。
根据判断的结果画出相应的线段。
②、再进一步深入:
要得到系统的时间就需要一个接口,就是自定义类和窗体类之间的消息传递。
传递进去之后,将时间进行分离,并加以判断,包括画8位置的判断。
每次判断完都会画出相应的“时间”,这就需要调用一个自己定义的画时间(DrawTime)的过程。
从实现原理我们知道这个8字是有七根线段组成的。所以在画时间的过程中应该在调用画线段(DrawSegment)的过程,当然不调用过程也可以直接在画时间的过程中写,但是这就违背了OO的原则,降低了复用率。
在深入到具体的画线段的过程中。
三、代码:
类中:
Private Type Coordinate
X As Integer
Y As Integer
End Type
Dim BasePoint As Coordinate
Dim SegWidth As Integer
Dim SegHeight As Integer
Dim p As PictureBox
Property Let BackColor(Color As Long)
p.BackColor = Color
End Property
Private Sub DrawTime(Number As Integer)
Select Case Number
'画相应数字的图形
Case 0
DrawSegment (1)
DrawSegment (2)
DrawSegment (3)
DrawSegment (4)
DrawSegment (5)
DrawSegment (6)
Case 1
DrawSegment (2)
DrawSegment (3)
Case 2
DrawSegment (1)
DrawSegment (2)
DrawSegment (7)
DrawSegment (5)
DrawSegment (4)
Case 3
DrawSegment (1)
DrawSegment (2)
DrawSegment (7)
DrawSegment (3)
DrawSegment (4)
Case 4
DrawSegment (2)
DrawSegment (3)
DrawSegment (7)
DrawSegment (6)
Case 5
DrawSegment (1)
DrawSegment (6)
DrawSegment (7)
DrawSegment (3)
DrawSegment (4)
Case 6
DrawSegment (1)
DrawSegment (6)
DrawSegment (7)
DrawSegment (3)
DrawSegment (4)
DrawSegment (5)
Case 7
DrawSegment (1)
DrawSegment (2)
DrawSegment (3)
Case 8
DrawSegment (1)
DrawSegment (2)
DrawSegment (3)
DrawSegment (4)
DrawSegment (5)
DrawSegment (6)
DrawSegment (7)
Case 9
DrawSegment (1)
DrawSegment (2)
DrawSegment (3)
DrawSegment (4)
DrawSegment (6)
DrawSegment (7)
End Select
End Sub
Private Sub DrawSegment(SegNum As Integer)
'
' 1
' ___
' | |
' 6 | | 2
' |-7-|
' 5 | | 3
' |___|
'
' 4
'
Select Case SegNum
Case 1
p.Line (BasePoint.X + 1,BasePoint.Y)-(BasePoint.X + SegWidth - 1,BasePoint.Y)
p.Line (BasePoint.X + 2,BasePoint.Y + 1)-(BasePoint.X + SegWidth - 2,BasePoint.Y + 1)
p.Line (BasePoint.X + 3,BasePoint.Y + 2)-(BasePoint.X + SegWidth - 3,BasePoint.Y + 2)
Case 2
p.Line (BasePoint.X + SegWidth - 1,BasePoint.Y + 1)-(BasePoint.X + SegWidth - 1,BasePoint.Y + (SegHeight 2) - 1)
p.Line (BasePoint.X + SegWidth - 2,BasePoint.Y + 2)-(BasePoint.X + SegWidth - 2,BasePoint.Y + (SegHeight 2))
p.Line (BasePoint.X + SegWidth - 3,BasePoint.Y + 3)-(BasePoint.X + SegWidth - 3,BasePoint.Y + (SegHeight 2) - 1)
Case 3
p.Line (BasePoint.X + SegWidth - 1,BasePoint.Y + (SegHeight 2) + 2)-(BasePoint.X + SegWidth - 1,BasePoint.Y + SegHeight)
p.Line (BasePoint.X + SegWidth - 2,BasePoint.Y + (SegHeight 2) + 1)-(BasePoint.X + SegWidth - 2,BasePoint.Y + SegHeight - 1)
p.Line (BasePoint.X + SegWidth - 3,BasePoint.Y + (SegHeight 2) + 2)-(BasePoint.X + SegWidth - 3,BasePoint.Y + SegHeight - 2)
Case 4
p.Line (BasePoint.X + 3,BasePoint.Y + SegHeight - 2)-(BasePoint.X + SegWidth - 3,BasePoint.Y + SegHeight - 2)
p.Line (BasePoint.X + 2,BasePoint.Y + SegHeight - 1)-(BasePoint.X + SegWidth - 2,BasePoint.Y + SegHeight - 1)
p.Line (BasePoint.X + 1,BasePoint.Y + SegHeight)-(BasePoint.X + SegWidth - 1,BasePoint.Y + SegHeight)
Case 5
p.Line (BasePoint.X,BasePoint.Y + (SegHeight 2) + 2)-(BasePoint.X,BasePoint.Y + SegHeight)
p.Line (BasePoint.X + 1,BasePoint.Y + (SegHeight 2) + 1)-(BasePoint.X + 1,BasePoint.Y + SegHeight - 1)
p.Line (BasePoint.X + 2,BasePoint.Y + (SegHeight 2) + 2)-(BasePoint.X + 2,BasePoint.Y + SegHeight - 2)
Case 6
p.Line (BasePoint.X,BasePoint.Y + 1)-(BasePoint.X,BasePoint.Y + (SegHeight 2) - 1)
p.Line (BasePoint.X + 1,BasePoint.Y + 2)-(BasePoint.X + 1,BasePoint.Y + (SegHeight 2))
p.Line (BasePoint.X + 2,BasePoint.Y + 3)-(BasePoint.X + 2,BasePoint.Y + (SegHeight 2) - 1)
Case 7
p.Line (BasePoint.X + 3,BasePoint.Y + (SegHeight 2) - 1)-(BasePoint.X + SegWidth - 3,BasePoint.Y + (SegHeight 2) - 1)
p.Line (BasePoint.X + 2,BasePoint.Y + (SegHeight 2))-(BasePoint.X + SegWidth - 2,BasePoint.Y + (SegHeight 2))
p.Line (BasePoint.X + 3,BasePoint.Y + (SegHeight 2) + 1)-(BasePoint.X + SegWidth - 3,BasePoint.Y + (SegHeight 2) + 1)
End Select
End Sub
Public Property Let Caption(ByVal Value As String)
Dim OrigX As Integer
OrigX = BasePoint.X
p.Cls
While Value <> ""
If Left$(Value,1) <> ":" Then
DrawTime (Val(Left$(Value,1)))
BasePoint.X = BasePoint.X + SegWidth + 3
Else
p.Line (BasePoint.X + (SegWidth 2) - 4,BasePoint.Y + (SegHeight 2) - 6)-(BasePoint.X + (SegWidth 2),BasePoint.Y + (SegHeight 2) - 3),BF
p.Line (BasePoint.X + (SegWidth 2) - 4,BasePoint.Y + (SegHeight 2) + 4)-(BasePoint.X + (SegWidth 2),BasePoint.Y + (SegHeight 2) + 7),BF
BasePoint.X = BasePoint.X + SegWidth
End If
Value = Right$(Value,Len(Value) - 1)
Wend
BasePoint.X = OrigX
End Property
Property Let ForeColor(Color As Long)
p.ForeColor = Color
End Property
Public Sub NewLCD(PBox As PictureBox)
'初始化的过程
Set p = PBox
p.ScaleMode = 3 ' pixel
p.AutoRedraw = True
BasePoint.X = 2
BasePoint.Y = 2
SegHeight = p.ScaleHeight - 6
SegWidth = (SegHeight 2) + 2
End Sub
窗体:
Dim lcdTest As New Class1 '类的实例化
Private Sub Form_Load()
lcdTest.NewLCD Picture1
End Sub
Private Sub Picture1_Click()
Unload Form1
End Sub
Private Sub Timer1_Timer()
lcdTest.Caption = Time
End Sub