用DateDiff函数计算出两个时间的差值,然后通过timer控件,每秒钟获取一次时间差值,然后显示出来!
用Graphics绘制,实现文字和图片的遮罩(请看第二篇文章)
Public Class Count_down
Dim L As Long, T As Long, X As Long, Y As Long
''' <summary>
''' 构造时差
''' </summary>
Structure Date_time
Dim SC_D As Long ''时差天
Dim SC_H As Long ''时差时
Dim SC_M As Long ''时差分
Dim SC_S As Long ''时差秒
End Structure
''' <summary>
''' 获取时差
''' </summary>
''' <param name="D"></param>
''' <returns></returns>
Function GET_SC(ByVal D As Date) As Date_time ''取时差
If IsDate(D) Then
Dim SD As New Date_time With {
.SC_D = DateDiff("d", Now, D), '取相差天数
.SC_H = DateDiff("h", Now, D) Mod 24, '取相差时数
.SC_M = DateDiff("n", Now, D) Mod 60, '取相差分数
.SC_S = DateDiff("s", Now, D) Mod 60 '取相差秒数
}
Return SD
Else
MsgBox($"请输入标准时间格式", MsgBoxStyle.Critical, "时间错误")
End If
End Function
''' <summary>
''' 计时器
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim S As Date_time = GET_SC("2020/06/07 8:00:00")
Timer1.Interval = 1000
If S.SC_D = 0 And S.SC_H = 0 And S.SC_M = 0 And S.SC_S = 0 Then
MASK_IMG("2020年高考开始啦!", $"同学们准备好了吗?")
Timer1.Enabled = False
Exit Sub
Else
MASK_IMG("距离 2020年 高考", $"还剩 {S.SC_D} 天 {S.SC_H} 小时 {S.SC_M} 分 {S.SC_S}秒")
End If
End Sub
''' <summary>
''' LED
''' </summary>
''' <param name="s"></param>
''' <param name="STR"></param>
Private Sub MASK_IMG(ByVal s As String, ByVal STR As String)
Dim B As New Bitmap(Width, Height)
Using G As Graphics = Graphics.FromImage(B)
G.Clip = New Region(New Rectangle(0, 0, Width, Height))
G.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed
G.DrawString("微信公众号关注:VB小源码", New Font("黑体", Led1.Width * 0.011), Brushes.Red, New Point(Led1.Width * 0.25, Led1.Height * 0.42))
G.DrawString(s, New Font("微软雅黑", Led1.Width * 0.019), Brushes.Yellow, New Point(Led1.Width * 0.01, Led1.Height * 0.05))
G.DrawString(STR, New Font("宋体", Led1.Width * 0.018), Brushes.LimeGreen, New Point(Led1.Width * 0.06, Led1.Height * 0.2))
G.DrawString("决战高考,改变命运。屡挫屡战,笑傲群雄。", New Font("仿宋", Led1.Width * 0.015), Brushes.Yellow, New Point(Led1.Width * 0.01, Led1.Height * 0.32))
End Using
Led1.INPUT_IMG = B
End Sub
Private Sub Count_down_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim S As Date_time = GET_SC("2020/06/07 8:00:00")
If (S.SC_D < 0 Or S.SC_H < 0 Or S.SC_M < 0 Or S.SC_S < 0) = True Then
Exit Sub
End If
Timer1.Interval = 1
Timer1.Enabled = True
End Sub
Private Sub Count_down_Closed(sender As Object, e As EventArgs) Handles Me.Closed
End
End Sub
Private Sub Led1_MouseDown(sender As Object, e As MouseEventArgs) Handles Led1.MouseDown
L = Led1.Left
T = Led1.Top
X = e.X
Y = e.Y
End Sub
Private Sub Led1_MouseMove(sender As Object, e As MouseEventArgs) Handles Led1.MouseMove
If Led1.Capture = True Then
Top = e.Y + T - Y
Left = e.X + L - X
L = Left
T = Top
End If
End Sub
Private Sub Led1_KeyDown(sender As Object, e As KeyEventArgs) Handles Led1.KeyDown
If e.KeyCode = Keys.Escape Then
If MsgBox("是否要退出倒计时?", MsgBoxStyle.YesNo, "退出") = MsgBoxResult.Yes Then End
End If
End Sub
Private Sub Led1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles Led1.MouseDoubleClick
If WindowState = FormWindowState.Normal Then
Led1.INPUT_IMG = New Bitmap(1, 1)
Timer1.Interval = 1
WindowState = FormWindowState.Maximized
Else
WindowState = FormWindowState.Normal
End If
End Sub
End Class
实例下载:
https://www.lanzous.com/i72udmj
今天教程到此结束啦!