怎样用VB来编写一个倒计时的软件呢?
1个回答
2014-03-07
展开全部
您好,很高兴为你回答问题,按你说的首先应该添加 Command1 Command2 Text1 Timer1 这四个然后写入代码如下: Private Declare Function GetTickCount& Lib "kernel32" ()
Dim starttm&, forcestop As Boolean
Private Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = False
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
Me.Caption = "请输入计时时间, 单位(秒)"
Text1.Move (Me.Width - Text1.Width) \ 2, (Me.ScaleHeight - Text1.Height) \ 2
Command1.Caption = "开始计时"
Command2.Caption = "停止计时"
Command1.Move (Me.ScaleWidth - Command1.Width - 100 - Command2.Width) \ 2, Me.ScaleHeight - Command1.Height
Command2.Move Command1.Left + Command1.Width + 100, Command1.Top
End Sub
Private Sub Form_Activate()
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then KeyAscii = 0: Command1_Click
End Sub
Private Sub Command1_Click()
If Val(Text1.Text) > 0 Then
forcestop = False
starttm = GetTickCount
Timer1.Enabled = True
Else
MsgBox "请输入计时时间, 单位(秒)"
Text1.SetFocus
End If
End Sub
Private Sub Command2_Click()
forcestop = True
End Sub
Private Sub Timer1_Timer()
Me.Caption = "剩下时间:" & Str(Val(Text1.Text) - Int((GetTickCount - starttm) / 1000)) & " 秒"
If GetTickCount - starttm > CLng(Val(Text1.Text)) * 1000 Or forcestop = True Then
If forcestop Then
MsgBox "终止计时"
Else
MsgBox "设定时间到"
End If
Timer1.Enabled = False
Me.Caption = "请输入计时时间, 单位(秒)"
Text1.SetFocus
End If
End Sub 如果我的回答对您有帮助请采纳,谢谢。
Dim starttm&, forcestop As Boolean
Private Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = False
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
Me.Caption = "请输入计时时间, 单位(秒)"
Text1.Move (Me.Width - Text1.Width) \ 2, (Me.ScaleHeight - Text1.Height) \ 2
Command1.Caption = "开始计时"
Command2.Caption = "停止计时"
Command1.Move (Me.ScaleWidth - Command1.Width - 100 - Command2.Width) \ 2, Me.ScaleHeight - Command1.Height
Command2.Move Command1.Left + Command1.Width + 100, Command1.Top
End Sub
Private Sub Form_Activate()
Text1.Text = ""
Text1.SetFocus
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then KeyAscii = 0: Command1_Click
End Sub
Private Sub Command1_Click()
If Val(Text1.Text) > 0 Then
forcestop = False
starttm = GetTickCount
Timer1.Enabled = True
Else
MsgBox "请输入计时时间, 单位(秒)"
Text1.SetFocus
End If
End Sub
Private Sub Command2_Click()
forcestop = True
End Sub
Private Sub Timer1_Timer()
Me.Caption = "剩下时间:" & Str(Val(Text1.Text) - Int((GetTickCount - starttm) / 1000)) & " 秒"
If GetTickCount - starttm > CLng(Val(Text1.Text)) * 1000 Or forcestop = True Then
If forcestop Then
MsgBox "终止计时"
Else
MsgBox "设定时间到"
End If
Timer1.Enabled = False
Me.Caption = "请输入计时时间, 单位(秒)"
Text1.SetFocus
End If
End Sub 如果我的回答对您有帮助请采纳,谢谢。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询