⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.vb

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 VB
字号:
Imports System.ComponentModel
Imports System.Drawing


Public Class Clock
    Inherits Control

    Private center As Point
    Private m_colorScheme As ColorScheme = New ColorScheme
    Private components As IContainer = Nothing
    Private doubleBuffer As Bitmap
    Private hour As Point
    Private hourHand As Integer
    Private hourMarks As Integer
    Private minute As Point
    Private minuteHand As Integer
    Private second As Point
    Private shortestSide As Integer
    Private timerClock As Timer

    Public Sub New()
        Me.InitializeComponent()
    End Sub

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If (disposing AndAlso (Not Me.components Is Nothing)) Then
            Me.components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private Shared Function GetHandPoint(ByVal center As Point, ByVal angle As Double, ByVal handlength As Integer) As Point
        Dim num As Integer
        Dim num2 As Integer
        If (angle > 270) Then
            angle = (angle - 270)
            num = Convert.ToInt32(CDbl((Math.Sin(angle) * handlength)))
            num2 = Convert.ToInt32(CDbl((Math.Cos(angle) * handlength)))
            Return New Point((center.X + num2), (center.Y + num))
        End If
        If (angle > 180) Then
            angle = (angle - 180)
            num2 = Convert.ToInt32(CDbl((Math.Sin(angle) * handlength)))
            num = Convert.ToInt32(CDbl((Math.Cos(angle) * handlength)))
            Return New Point((center.X - num2), (center.Y + num))
        End If
        If (angle > 90) Then
            angle = (angle - 90)
            num = Convert.ToInt32(CDbl((Math.Sin(angle) * handlength)))
            num2 = Convert.ToInt32(CDbl((Math.Cos(angle) * handlength)))
            Return New Point((center.X + num2), (center.Y + num))
        End If
        num2 = Convert.ToInt32(CDbl((Math.Sin(angle) * handlength)))
        num = Convert.ToInt32(CDbl((Math.Cos(angle) * handlength)))
        Return New Point((center.X + num2), (center.Y - num))
    End Function

    Private Sub InitializeComponent()
        Me.timerClock = New Timer
        MyBase.SuspendLayout()

        Me.timerClock.Enabled = True
        Me.timerClock.Interval = &H3E8

        AddHandler Me.timerClock.Tick, New EventHandler(AddressOf Me.timerClock_Tick)

        MyBase.ResumeLayout(False)
    End Sub

    Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
        pe.Graphics.DrawImage(Me.doubleBuffer, 0, 0)
        MyBase.OnPaint(pe)
    End Sub

    Protected Overrides Sub OnResize(ByVal e As EventArgs)

        If (MyBase.Width > MyBase.Height) Then
            Me.shortestSide = MyBase.Height
        Else
            Me.shortestSide = MyBase.Width
        End If

        Me.center = New Point((Me.shortestSide / 2), (Me.shortestSide / 2))

        If (Not Me.doubleBuffer Is Nothing) Then
            Me.doubleBuffer.Dispose()
        End If
        Me.doubleBuffer = New Bitmap(Me.shortestSide, Me.shortestSide)

        Me.minuteHand = ((Me.shortestSide / 2) - 4)
        Me.hourHand = (Me.shortestSide / 4)
        Me.hourMarks = (Me.shortestSide / 8)

        MyBase.OnResize(e)

    End Sub

    Private Sub timerClock_Tick(ByVal sender As Object, ByVal e As EventArgs)
        Dim dtNow As DateTime = DateTime.Now
        Dim angle As Double = (dtNow.Second * 0.10471975511965977)
        Me.second = Clock.GetHandPoint(Me.center, angle, Me.minuteHand)
        angle = (dtNow.Minute * 0.10471975511965977)
        Me.minute = Clock.GetHandPoint(Me.center, angle, Me.minuteHand)
        angle = (dtNow.Hour * 0.52359877559829882)
        Me.hour = Clock.GetHandPoint(Me.center, angle, Me.hourHand)
        Dim graphics As Graphics = graphics.FromImage(Me.doubleBuffer)
        graphics.Clear(Me.BackColor)
        Dim pen As New Pen(Me.colorScheme.HandColor, 2.0!)
        Dim pen2 As New Pen(Me.colorScheme.BorderColor)
        graphics.FillEllipse(New SolidBrush(Me.colorScheme.FaceColor), 0, 0, Me.shortestSide, Me.shortestSide)
        graphics.DrawEllipse(pen2, 0, 0, Me.shortestSide, Me.shortestSide)
        graphics.DrawLine(pen2, Me.center.X, 2, Me.center.X, Me.hourMarks)
        graphics.DrawLine(pen2, 2, Me.center.Y, Me.hourMarks, Me.center.Y)
        graphics.DrawLine(pen2, (Me.shortestSide - Me.hourMarks), Me.center.Y, (Me.shortestSide - 2), Me.center.Y)
        graphics.DrawLine(pen2, Me.center.X, (Me.shortestSide - Me.hourMarks), Me.center.X, (Me.shortestSide - 2))
        graphics.DrawLine(pen, Me.center.X, Me.center.Y, Me.hour.X, Me.hour.Y)
        graphics.DrawLine(pen, Me.center.X, Me.center.Y, Me.minute.X, Me.minute.Y)
        graphics.DrawLine(pen2, Me.center.X, Me.center.Y, Me.second.X, Me.second.Y)
        graphics.FillEllipse(New SolidBrush(m_colorScheme.HandColor), (Me.center.X - 2), (Me.center.Y - 2), 4, 4)
        graphics.Dispose()
        Me.Refresh()
    End Sub

    Public Property ColorScheme() As ColorScheme
        Get
            Return m_colorScheme
        End Get
        Set(ByVal value As ColorScheme)
            m_colorScheme = value
        End Set
    End Property

End Class

Public Class ColorScheme

    Private m_borderColor As Color = Color.Gray
    Private m_faceColor As Color = Color.LightBlue
    Private m_handColor As Color = Color.Black

    Public Property BorderColor() As Color
        Get
            Return m_borderColor
        End Get
        Set(ByVal value As Color)
            m_borderColor = value
        End Set
    End Property

    Public Property FaceColor() As Color
        Get
            Return m_faceColor
        End Get
        Set(ByVal value As Color)
            m_faceColor = value
        End Set
    End Property

    Public Property HandColor() As Color
        Get
            Return m_handColor
        End Get
        Set(ByVal value As Color)
            m_handColor = value
        End Set
    End Property

End Class


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -