VB 倒计时器
发布网友
发布时间:2022-04-23 13:53
我来回答
共5个回答
热心网友
时间:2022-04-24 12:40
Option Explicit
'定义两个API,SetTimer是建立一个定时器,KillTimer是删除一个定时器
Public Declare Function SetTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32.dll" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
'这个函数启动一个定时器,其中iSeconds表示要计时的秒数
Public Sub TimerOn(hWnd As Long, iSeconds As Long)
Dim iTimer As Long
iSeconds = iSeconds * 1000 '转换为毫秒
'启动定时器,第一个参数是句柄,第二个参数是这个定时器的ID号,第三个参数是时间,第四个参数是当定时器时间到之后,要调用哪个函数
iTimer = SetTimer(hWnd, 1, iSeconds, AddressOf TimeOutProcessor)
End Sub
'关闭一个定时器
Public Sub TimerOff(hWnd As Long)
Dim iKillTimer As Long
'关闭ID为1的定时器,第一个参数是句柄,第二个参数是ID号
iKillTimer = KillTimer(hWnd, 1)
End Sub
'这个函数是计时器时间到之后执行的程序
Public Sub TimeOutProcessor(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
'弹出一个框
MsgBox ("It works!")
End Sub
使用方法
新建一个模块,把这些代码放在模块里面,然后在窗体上放两个按钮,写入如下代码
Private Sub Command1_Click()
TimerOn Me.hWnd, 2
End Sub
Private Sub Command2_Click()
TimerOff Me.hWnd
End Sub
这样当你按了Command1后,每过2秒,将弹出一个框,直到你按了Command2
热心网友
时间:2022-04-24 13:58
'建立一个text1,一个label1,一个timer1,两个command1-2,不用设置任何属性。
Option Explicit
Dim M As Single, S As Long
Private Sub Form_Load()
form1.Width = 3800
form1.Height = 1800
form1.BackColor = RGB(150, 120, 80)
Timer1.Interval = 1000
Timer1.Enabled = False
Text1.Width = 2400
Text1.Height = 300
Text1.Left = 100
Text1.Top = 400
Text1.ForeColor = &H909090
Text1.Visible = True
Command1.Width = 900
Command1.Height = 400
Command1.Left = 2600
Command1.Top = 150
Command2.Width = 900
Command2.Height = 400
Command2.Left = 2600
Command2.Top = 650
Label1.Width = 2400
Label1.Height = 1000
Label1.Left = 100
Label1.Top = 100
Label1.BackColor = &H0
Label1.ForeColor = &H4410FF
Label1.Alignment = 1
Label1.FontSize = 50
Label1.Caption = ""
Label1.Visible = False
Text1.Text = "在此输入分钟,再点击开始"
Command1.Caption = "开始"
Command2.Caption = "退出"
Command1.Enabled = False
End Sub
Private Sub command1_Click()
If Command1.Caption = "开始" Then
If Text1.Text = "" Then Exit Sub
M = CSng(Val(Text1.Text))
If M <= 0 Then Text1.Text = "": Exit Sub
S = M * 60
If S = 0 Then Text1.Text = "": Exit Sub
Text1.Visible = False
Label1.Caption = Str(S)
Label1.Visible = True
Timer1.Enabled = True
Command1.Caption = "停止"
ElseIf Command1.Caption = "停止" Then
Timer1.Enabled = False
Command1.Caption = "继续"
Command2.Caption = "重新开始"
ElseIf Command1.Caption = "继续" Then
Timer1.Enabled = True
Command1.Caption = "停止"
Command2.Caption = "退出"
End If
End Sub
Private Sub command2_Click()
If Command2.Caption = "重新开始" Then
Form_Load
ElseIf Command2.Caption = "退出" Then
End
End If
End Sub
Private Sub Text1_Change()
Command1.Enabled = True
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Text1.Text = "在此输入分钟,再点击开始" Then
Text1.ForeColor = &H0
Text1.Text = ""
End If
End Sub
Private Sub Timer1_Timer()
S = S - 1
Label1.Caption = Trim(Str(S))
If S = 0 Then
MsgBox "时间到!!!"
Beep
Timer1.Enabled = False
Label1.Visible = False
Text1.Visible = True
Command1.Caption = "开始"
End If
End Sub
热心网友
时间:2022-04-24 15:33
SetTimer 和 KillTimer其实也是创建一个计时器,与系统的计时器控件差别不大
不错,Timer控件是有一些误差,但我们可以换一种思路,我们可以用系统的时间为准,来做倒记时,比如:
'在你原来的控件的基础上,添加一个计时器控件,添如下代码
Dim EndTime As Double
Private Sub Command1_Click()
Dim h As Integer, m As Integer, s As Integer
h = Val(Text1.Text)
m = Val(Text2.Text)
s = Val(Text3.Text)
EndTime = Now + h / 24# + m / (24# * 60) + s / (24# * 3600)
Timer1.Interval = 100
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
If EndTime = Now Then
Timer1.Enabled = False
Label1.Caption = "计时完毕"
Else
Label1.Caption = Format(EndTime - Now, "HH:MM:SS")
End If
End Sub
热心网友
时间:2022-04-24 17:24
只会timer,一起学学