(vb编程)制作秒表
VB编程中的精确计时及定时
VB高精度计时器编程在很多场合下编程(例如工业控制、游戏)中需要比较精确的记时器,本文讨论的是在VB下实现记时器的若干方法以及它们的精度控制问题。
在VB中最常用的是Timer 控件,理论上它的记时精度可以达到1ms(毫秒)。
但实际上Timer控件的计时精度无法保证,对于1s的定时事件,也往往会出现50ms以上的计时误差,这对于脉冲信号的采样是不能容忍的。
它只适用于对于精度要求不太高的场合。
这里介绍的是两中利用Windows API函数实现精确记时的方法。
一高性能频率记数法利用这种方法要使用两个API函数QueryPerformanceFrequency和QueryPerformanceCounter。
1、QueryPerformanceFrequency(Frequency)函数其调用格式为:QueryPerformanceFrequency(Frequency) ,调用该函数之后将计算机内部系统定时器的时钟频率存放于变量Frequency中,在VB语言中,可将Frequency定义为Currency类型。
所获取频率值的单位为:HZ函数调用成功后返回非0值,如果安装的硬件不支持高精度计时器,返回02、QueryPerformanceCounter(Counter)函数其调用格式为QueryPerformanceCounter(Counter),调用函数之后,将计算机内部系统定时器的时钟计数值存放于变量Counter中,在VB语言中,可将Counter定义为Currency 类型。
所获取计数值的单位为:个函数调用成功后返回非,0值,如果安装的硬件不支持高精度计时器,返回0分别调用两次QueryPerformanceCounter()函数,并利用两次获得的计数之差除以调用QueryPerformanceFrequency(Frequency)函数所获取的时钟频率,就可以精确计算出两次计数所经过的时间,单位为秒,可以精确到微秒级。
VB计时器的编辑
m = Val(Mid(strNow1, 4, 3))
s = s + h * 3600 + m * 60 '今天已经过去的秒数
nSec = 86400 - s + tSec '目标当天到截止点的秒数与今天已过去的秒数之和
If nSec > 86400 Then
3)程序中调用系统时间使用了“Now”函数,可以按照“小时:分钟:秒”的形式返回当前时间。
4)为保证应用程序的灵活性,应在倒计时牌上设有暂停、继续、时间校正、时区切换,结束运行等特殊要求的激发点。
5)按照需要构筑程序框架并编写应用程序。
六、调试过程及实验结果
实验界面:
七、附录(源程序清单)
Dim a As Long, b As Long, c As Long, mmonth As Integer
毅字楼404西北工业大学20102011学年第一学期软件技术实验报告七附录源程序清单dimaaslongbaslongcaslongmmonthasintegerdimnyearasintegerndayasintegernmonthasintegermdayasinteger本年的年月日变量以及目标年不足一年的天数与当前年剩余天数之和dimtyearasintegertdayasintegeroyearasintegertmonthasinteger目标年的年天数本年与目标年之差dimnsecaslongtsecaslong今天已经过去的秒数目标天0点距离截止点的秒数其中nsec最终为倒计时不足一天的剩余秒数dimstrnow1asstringstrnow2asstringzhutiasstring取今天年月日和今天时分秒的字符串变量dimstrend1asstringstrend2asstring取目标截止时的年月日时分秒的字符串变量dimhaslongmaslongsaslong时分秒计数变量dimodayasintegerpdayasintegerxdayasintegerzdayasinteger今天和目标天秒数之和大于1天的临时变量值和今年中本月以前的天数累加和计数变量判月函数本月最大值取值dimiasintegerkasintegerflagasinteger循环控制变量以及闰年处理中二月天数处理变量privatesubcheck1clicktext1text南非世界杯
vb时钟设计课程设计
vb时钟设计课程设计一、课程目标知识目标:1. 学生能理解VB编程中时间相关的数据类型和函数。
2. 学生掌握使用Visual Basic设计一个简单时钟界面和实现时间显示功能的基本步骤。
3. 学生能够解释时钟程序中涉及的时间概念,如时、分、秒及它们之间的关系。
技能目标:1. 学生能够运用VB语言编写程序,实现实时时间显示功能。
2. 学生能够通过编程实践,培养解决问题的能力和逻辑思维能力。
3. 学生能够对程序进行调试和修改,提高程序优化和故障排查的能力。
情感态度价值观目标:1. 学生通过动手实践,培养对计算机编程的兴趣和热情。
2. 学生在团队协作中,学会相互沟通与协作,培养集体荣誉感。
3. 学生通过编程创作,体会科技改变生活的实际应用,激发创新精神。
分析课程性质、学生特点和教学要求:1. 课程性质:本课程为信息技术课程,结合Visual Basic编程,以实践操作为主。
2. 学生特点:六年级学生具备一定的计算机操作能力和逻辑思维能力,对编程有好奇心。
3. 教学要求:注重培养学生的实际操作能力,激发学生的学习兴趣,提高学生的团队合作意识。
1. 独立编写一个具有基本时间显示功能的VB时钟程序。
2. 在编程过程中,运用所学知识解决实际问题,提高逻辑思维能力。
3. 积极参与团队合作,与同伴共同完成时钟设计任务,提高沟通协作能力。
二、教学内容1. VB编程基础:- 数据类型:整数、字符串、日期时间。
- 控件使用:标签、定时器。
- 常用函数:Time、Timer。
2. 时钟设计原理:- 时间概念:时、分、秒及其进制转换。
- 时钟界面设计:布局、颜色、字体。
3. 编程实现时钟:- 时钟程序结构:初始化、时间更新、界面显示。
- 代码编写:获取系统时间、时间格式化、定时刷新。
4. 教学大纲安排:- 第一课时:回顾VB基础,介绍时钟设计原理。
- 第二课时:学习使用控件,设计时钟界面。
- 第三课时:编写代码实现时间显示功能。
Visual basic制做秒表
C mp t K o  ̄ g n e h o g o ue n we ea dT c n l y电脑 知 识 与技术 r d o
Vo., .6, u e2 0 P .4 0 1 No1 J n 01 , P4 4 6
Viu l a i s a sc制做 秒表 b
杨 ,春 常 吉 王 红, 晶
2程 序 代 码
Di t,t,t AsDae m c s p t Di m,t m fAsI t g r n e e
P iaeS b F r L a 0 rvt u om o d
m = 一l f = Tm e
Ti mer .n eva 1I t r l= 1
T me 1E a ld =fl i r .n be as
C mp u e E a ld =fI o a s .n be as E dSb n u
图 1
P v t S b Tm r Tm r i r ae u i e l i eO t s=Dae f ”” t, i 1 tDi(S ,e Tme
t =I( :T ,sM d6,t sMo 0 s If me t o 0 ( f p+t ) d6 )
f c insofsat d un to tre ,pa e e u e ,r st xt d a d S n,h saw i a g p iai n n av rey ofr a—tm e c r 1 us,r s m d e e,e ie n O o a der n e ofa plc to si a t e l i onto . i K e wor s y d :Viua sc ;so s lbai t pwac th
Ab ta t n t i p p r v 60 f rwid ws i e t i r g a : t p th . e p o rm ih c l h i n c mp t r y tm t sr c :I s a e , b . o n o v s u it p o rm so wac , Th r g a wh c a s e t h g a ly lt mei o u e s s e wi h
VB2015编写时间小工具_倒顺计时_定时关机_闹钟等
PublicClass Form1Dim x, y, z AsBytePrivateSub Form1_Load(sender AsObject, e As EventArgs) HandlesMyBase.Loady = 0z = 0Dim thisDay AsInteger = Microsoft.VisualBasic.DateAndTime.Day(Now)'使用 Day 函数,可能需要用 Microsoft.VisualBasic 命名空间限定该函数,因为 System.Windows.Forms 命名空间将 Day 定义为枚举TextBox1.Text = 0TextBox2.Text = 0TextBox3.Text = 0TextBox5.Text = Year(Now)TextBox6.Text = Month(Now)TextBox7.Text = thisDayTextBox8.Text = Hour(Now)TextBox9.Text = Minute(Now)TextBox10.Text = Year(Now)TextBox11.Text = Month(Now)TextBox12.Text = thisDayTextBox13.Text = Hour(Now)TextBox14.Text = Minute(Now)EndSub'Private Sub AxMMControl1_BackClick(sender As Object, e As AxMCI.DmciEvents_BackClickEvent) Handles AxMMControl1.BackClick'使用COM有件中的:Microsoft Multimedia Contril (SP3) 音乐播放'End SubPrivateSub Button1_Click(sender AsObject, e As EventArgs) Handles Button1.ClickTimer1.Enabled = Truex = 1Button4.Enabled = FalseEndSubPrivateSub Button2_Click(sender AsObject, e As EventArgs) Handles Button2.Click'mand = "close" '使用COM有件中的:Microsoft Multimedia Contril (SP3) 音乐播放AxWindowsMediaPlayer1.close() ''使用COM有件中的Microsoft Media Player.这里是关闭播放x = 0EndSubPrivateSub Button3_Click(sender AsObject, e As EventArgs) Handles Button3.ClickOpenFileDialog1.InitialDirectory = "c:\"Dim result? AsBoolean = OpenFileDialog1.ShowDialog()If result = TrueThenTextBox4.Text = OpenFileDialog1.FileNameEndIf'AxMMControl1.FileName = TextBox4.Text '使用COM有件中的:Microsoft Multimedia Contril (SP3) 音乐播放EndSubPrivateSub Button4_Click(sender AsObject, e As EventArgs) Handles Button4.ClickTimer1.Enabled = Truex = 2Button1.Enabled = FalseEndSubPrivateSub Button5_Click(sender AsObject, e As EventArgs) Handles Button5.Clicky = 0x = 0z = 0TextBox1.Text = 0TextBox2.Text = 0TextBox3.Text = 0'mand = "close" '使用COM有件中的:Microsoft Multimedia Contril (SP3) 音乐播放 AxWindowsMediaPlayer1.close() ''使用COM有件中的Microsoft Media Player.这里是关闭播放 Button6.BackColor = Color.GoldButton6.ForeColor = Color.BlueButton6.Text = "启动"TextBox1.Enabled = TrueTextBox2.Enabled = TrueTextBox3.Enabled = TrueButton1.Enabled = TrueButton4.Enabled = TrueButton6.Enabled = TrueButton7.Enabled = TrueTextBox5.Enabled = TrueTextBox6.Enabled = TrueTextBox7.Enabled = TrueTextBox8.Enabled = TrueTextBox9.Enabled = TrueTextBox10.Enabled = TrueTextBox11.Enabled = TrueTextBox12.Enabled = TrueTextBox13.Enabled = TrueTextBox14.Enabled = TrueButton7.BackColor = Color.LimeButton7.ForeColor = Color.BlueButton7.Text = "启用"Label5.Text = "倒计时/顺计时时间:"Label5.ForeColor = Color.GreenEndSubPrivateSub Timer1_Tick(sender AsObject, e As EventArgs) Handles Timer1.TickIf x = 1 ThenTextBox1.Enabled = FalseTextBox2.Enabled = FalseTextBox3.Enabled = FalseLabel5.Text = "倒计时时间:"Label5.ForeColor = Color.RedIf TextBox1.Text <> 0 ThenTextBox1.Text -= 1EndIfIf (TextBox1.Text + TextBox2.Text + TextBox3.Text) <> 0 ThenIf TextBox1.Text = 0 ThenIf TextBox2.Text = 0 ThenTextBox1.Text = 60TextBox2.Text = 60TextBox3.Text -= 1EndIfIf TextBox2.Text <> 0 ThenTextBox1.Text = 60TextBox2.Text -= 1EndIfEndIfEndIfIf x = 1 Or x = 2 ThenIf (TextBox1.Text + TextBox2.Text + TextBox3.Text) = 0 ThenTimer1.Enabled = FalseTextBox1.Enabled = TrueTextBox2.Enabled = TrueTextBox3.Enabled = TrueButton1.Enabled = TrueButton4.Enabled = True'mand = "open" '使用COM有件中的:Microsoft Multimedia Contril (SP3) 音乐播放'mand = "play" '使用COM有件中的:Microsoft Multimedia Contril (SP3) 音乐播放AxWindowsMediaPlayer1.URL = TextBox4.Text '使用COM有件中的Microsoft Media Player.这里是播放的文件MsgBox("时间到!", 48, "提示!")Label5.Text = "倒计时/顺计时时间:"Label5.ForeColor = Color.GreenEndIfEndIfEndIfIf x = 2 ThenLabel5.Text = "顺计时时间:"Label5.ForeColor = Color.RedTextBox1.Enabled = FalseTextBox2.Enabled = FalseTextBox3.Enabled = FalseTextBox1.Text += 1If TextBox1.Text = 59 ThenTextBox1.Text = 0TextBox2.Text += 1If TextBox2.Text = 59 ThenTextBox2.Text = 0TextBox3.Text += 1EndIfEndIfEndIfEndSubPrivateSub Timer2_Tick(sender AsObject, e As EventArgs) Handles Timer2.TickDim a AsBytea = Weekday(Now)If a = 1 ThenLabel8.Text = "星期天"EndIfIf a = 2 ThenLabel8.Text = "星期一"EndIfIf a = 3 ThenLabel8.Text = "星期二"EndIfIf a = 4 ThenLabel8.Text = "星期三"EndIfIf a = 5 ThenLabel8.Text = "星期四"EndIfIf a = 6 ThenLabel8.Text = "星期五"EndIfIf a = 7 ThenLabel8.Text = "星期六"EndIfLabel6.Text = DateStringLabel7.Text = TimeStringDim thisDay AsInteger = Microsoft.VisualBasic.DateAndTime.Day(Now)If y = 1 ThenIf TextBox5.Text = Year(Now) ThenIf TextBox6.Text = Month(Now) ThenIf TextBox7.Text = thisDay ThenIf TextBox8.Text = Hour(Now) ThenIf TextBox9.Text = Minute(Now) Theny = 0Button7.BackColor = Color.LimeButton7.ForeColor = Color.BlueButton7.Text = "启用"Button7.Enabled = TrueTextBox5.Enabled = TrueTextBox6.Enabled = TrueTextBox7.Enabled = TrueTextBox8.Enabled = TrueTextBox9.Enabled = TrueAxWindowsMediaPlayer1.URL = TextBox4.Text MsgBox("闹钟时间到!", 48, "闹钟提示!") EndIfEndIfEndIfEndIfEndIfEndIfIf z = 1 ThenIf TextBox10.Text = Year(Now) ThenIf TextBox11.Text = Month(Now) ThenIf TextBox12.Text = thisDay ThenIf TextBox13.Text = Hour(Now) ThenIf TextBox14.Text = Minute(Now) Thenz = 0Button6.BackColor = Color.GoldButton6.ForeColor = Color.BlueButton6.Text = "启动"Button6.Enabled = TrueTextBox10.Enabled = TrueTextBox11.Enabled = TrueTextBox12.Enabled = TrueTextBox13.Enabled = TrueTextBox14.Enabled = TrueShell("shutdown.exe -f -s -t 3")'MsgBox("时间到!", 48, "错误提示!")EndIfEndIfEndIfEndIfEndIfEndIfEndSubPrivateSub TextBox1_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox1.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenTextBox2.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox1_LostFocus(sender AsObject, e As EventArgs) Handles TextBox1.LostFocusDim a AsStringa = Val(TextBox1.Text) 'Val'转换为数值型If TextBox1.Text <>""ThenIf a > 60 Or a < 0 ThenMsgBox("请输入正确的时间;0~60!", 48, "错误提示!")TextBox1.Focus() '重新获取焦点TextBox1.Text = ""EndIfEndIfEndSubPrivateSub TextBox2_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox2.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenTextBox3.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox2_LostFocus(sender AsObject, e As EventArgs) Handles TextBox2.LostFocusDim a AsStringa = Val(TextBox2.Text) 'Val'转换为数值型If TextBox2.Text <>""ThenIf a > 60 Or a < 0 ThenMsgBox("请输入正确的时间;0~60!", 48, "错误提示!")TextBox2.Focus() '重新获取焦点TextBox2.Text = ""EndIfEndSubPrivateSub TextBox3_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox3.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenButton1.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub Timer3_Tick(sender AsObject, e As EventArgs) Handles Timer3.TickEndSubPrivateSub Button6_Click(sender AsObject, e As EventArgs) Handles Button6.ClickDim a AsByteDim thisDay AsInteger = Microsoft.VisualBasic.DateAndTime.Day(Now)If Val(TextBox10.Text + TextBox11.Text + TextBox12.Text + TextBox13.Text + TextBox14.Text) >Val(Year(Now) & Month(Now) & thisDay & Hour(Now) & Minute(Now)) Thena = MsgBox("确认启用定时关机!", 305, "警示!")If a = vbOK Thenz = 1Button6.BackColor = Color.RedButton6.ForeColor = Color.BlackButton6.Text = "启动中"Button6.Enabled = FalseTextBox10.Enabled = FalseTextBox11.Enabled = FalseTextBox12.Enabled = FalseTextBox13.Enabled = FalseTextBox14.Enabled = FalseElsez = 0Button6.Enabled = TrueTextBox10.Enabled = TrueTextBox11.Enabled = TrueTextBox12.Enabled = TrueTextBox13.Enabled = TrueTextBox14.Enabled = TrueEndIfElseMsgBox("请输入正确的时间!", 48, "错误提示!")TextBox14.Focus() '重新获取焦点EndIfEndSubPrivateSub Button7_Click(sender AsObject, e As EventArgs) Handles Button7.ClickDim thisDay AsInteger = DateAndTime.Day(Now)If Val(TextBox5.Text + TextBox6.Text + TextBox7.Text + TextBox8.Text + TextBox9.Text) >Val(Year(Now) & Month(Now) & thisDay & Hour(Now) & Minute(Now)) Theny = 1MsgBox("请输入正确的时间!", 48, "错误提示!")TextBox9.Focus() '重新获取焦点EndIfIf y = 1 ThenButton7.BackColor = Color.CrimsonButton7.ForeColor = Color.BlackButton7.Text = "启用中"Button7.Enabled = FalseTextBox5.Enabled = FalseTextBox6.Enabled = FalseTextBox7.Enabled = FalseTextBox8.Enabled = FalseTextBox9.Enabled = FalseEndIfEndSub'Private Sub TextBox3_LostFocus(sender As Object, e As EventArgs) Handles TextBox3.LostFocus' Dim a As String' a = Val(TextBox3.Text) 'Val'转换为数值型' If TextBox3.Text <> "" Then' If a > 60 Or a < 0 Then' MsgBox("请输入正确的时间;0~60!", 48, "错误提示!")' TextBox3.Focus() '重新获取焦点' TextBox3.Text = ""' End If' End If'End SubPrivateSub TextBox5_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox5.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenTextBox6.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox5_LostFocus(sender AsObject, e As EventArgs) Handles TextBox5.LostFocusDim a AsStringa = Val(TextBox5.Text) 'Val'转换为数值型If TextBox5.Text <>""ThenIf a > 9999 Or a < Year(Now) ThenMsgBox("请输入正确的时间,当前年份~9999年!", 48, "错误提示!")TextBox5.Focus() '重新获取焦点TextBox5.Text = Year(Now)EndIfEndIfEndSubPrivateSub TextBox6_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox6.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenTextBox7.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox6_LostFocus(sender AsObject, e As EventArgs) Handles TextBox6.LostFocusDim a AsStringa = Val(TextBox6.Text) 'Val'转换为数值型If TextBox6.Text <>""ThenIf a > 12 Or a <= 0 ThenMsgBox("请输入正确的时间,1~12!", 48, "错误提示!")TextBox6.Focus() '重新获取焦点TextBox6.Text = Month(Now)EndIfEndIfEndSubPrivateSub TextBox7_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox7.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenTextBox8.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox7_LostFocus(sender AsObject, e As EventArgs) Handles TextBox7.LostFocusDim a AsStringDim thisDay AsInteger = DateAndTime.Day(Now)a = Val(TextBox7.Text) 'Val'转换为数值型If TextBox7.Text <>""ThenIf a > 31 Or a <= 0 ThenMsgBox("请输入正确的时间,1~31!", 48, "错误提示!")TextBox7.Focus() '重新获取焦点TextBox7.Text = thisDayEndIfEndIfEndSubPrivateSub TextBox8_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox8.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenTextBox9.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox8_LostFocus(sender AsObject, e As EventArgs) Handles TextBox8.LostFocusDim a AsStringDim thisDay AsInteger = DateAndTime.Day(Now)a = Val(TextBox8.Text) 'Val'转换为数值型If TextBox8.Text <>""ThenIf a > 23 Or a < 0 ThenMsgBox("请输入正确的时间,00~23!", 48, "错误提示!")TextBox8.Focus() '重新获取焦点TextBox8.Text = Hour(Now)EndIfEndIfEndSubPrivateSub TextBox9_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox9.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenButton7.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox9_LostFocus(sender AsObject, e As EventArgs) Handles TextBox9.LostFocusDim a AsStringDim thisDay AsInteger = DateAndTime.Day(Now)a = Val(TextBox9.Text) 'Val'转换为数值型If TextBox9.Text <>""ThenIf a > 59 Or a <= 0 ThenMsgBox("请输入正确的时间,00~59!", 48, "错误提示!")TextBox9.Focus() '重新获取焦点TextBox9.Text = Minute(Now)EndIfEndIfEndSubPrivateSub TextBox10_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox10.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenTextBox11.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox10_LostFocus(sender AsObject, e As EventArgs) Handles TextBox10.LostFocusDim a AsStringa = Val(TextBox10.Text) 'Val'转换为数值型If TextBox10.Text <>""ThenIf a > 9999 Or a < Year(Now) ThenMsgBox("请输入正确的时间,当前年份~9999年!", 48, "错误提示!")TextBox10.Focus() '重新获取焦点TextBox10.Text = Year(Now)EndIfEndIfEndSubPrivateSub TextBox11_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox11.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenTextBox12.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox11_LostFocus(sender AsObject, e As EventArgs) Handles TextBox11.LostFocusDim a AsStringa = Val(TextBox11.Text) 'Val'转换为数值型If TextBox11.Text <>""ThenIf a > 12 Or a <= 0 ThenMsgBox("请输入正确的时间,1~12!", 48, "错误提示!")TextBox11.Focus() '重新获取焦点TextBox11.Text = Month(Now)EndIfEndIfEndSubPrivateSub TextBox12_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox12.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenTextBox13.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox12_LostFocus(sender AsObject, e As EventArgs) Handles TextBox12.LostFocusDim a AsStringDim thisDay AsInteger = DateAndTime.Day(Now)a = Val(TextBox12.Text) 'Val'转换为数值型If TextBox12.Text <>""ThenIf a > 31 Or a <= 0 ThenMsgBox("请输入正确的时间,1~31!", 48, "错误提示!")TextBox12.Focus() '重新获取焦点TextBox12.Text = thisDayEndIfEndIfEndSubPrivateSub TextBox13_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox13.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing) If e.KeyChar = Chr(13) ThenTextBox14.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox13_LostFocus(sender AsObject, e As EventArgs) Handles TextBox13.LostFocusDim a AsStringDim thisDay AsInteger = DateAndTime.Day(Now)a = Val(TextBox13.Text) 'Val'转换为数值型If TextBox13.Text <>""ThenIf a > 23 Or a < 0 ThenMsgBox("请输入正确的时间,00~23!", 48, "错误提示!")TextBox13.Focus() '重新获取焦点TextBox13.Text = Hour(Now)EndIfEndIfEndSubPrivateSub TextBox14_KeyPress(sender AsObject, e As KeyPressEventArgs) Handles TextBox14.KeyPress Const xStr AsString = "0123456789"e.KeyChar = IIf(InStr(xStr & Chr(8) & Chr(13), e.KeyChar.ToString), e.KeyChar, Nothing)If e.KeyChar = Chr(13) ThenButton6.Focus()' Call Command1_Click 回车键事件,这里调用按键1内的单击事件EndIfEndSubPrivateSub TextBox14_LostFocus(sender AsObject, e As EventArgs) Handles TextBox14.LostFocus Dim a AsStringDim thisDay AsInteger = DateAndTime.Day(Now)a = Val(TextBox14.Text) 'Val'转换为数值型If TextBox14.Text <>""ThenIf a > 59 Or a <= 0 ThenMsgBox("请输入正确的时间,00~59!", 48, "错误提示!")TextBox14.Focus() '重新获取焦点TextBox14.Text = Minute(Now)EndIfEndIfEndSubEndClass。
利用VB实现动态时钟的算法分析
利用VB实现动态时钟的算法分析
动态时钟是指一个数字时钟能够自动更新时间,在计算
机程序中,可以使用VB来实现动态时钟。
具体实现的算法
如下:
1. 在VB中,需要创建一个窗体(Form)来展示动态时钟。
在窗体的Load事件中,可以初始化时钟的显示格式。
2. 在窗体的Timer事件中,需要更新时钟的时间。
可
以使用VB的内置函数Now()来获取当前时间。
同时,可以
使用Format函数将时间格式化为小时、分钟和秒。
3. 将格式化后的时间赋值给时钟控件的Text属性,以
更新显示的时间。
4. 为了使时钟能够动态更新,需要将Timer控件的Enabled属性设置为True。
同时,在窗体的Unload事件中,需要将Timer控件的Enabled属性设置为False,以避免程
序内存泄漏。
5. 可以通过调整Timer控件的Interval属性,来控制
时钟更新的频率。
通常,将Interval设置为1000(毫秒)即
可实现每秒钟更新一次。
6. 如果需要增加其他功能,如设置闹钟、倒计时等,
可以在时钟更新的方法中增加相应的代码实现。
综上,通过VB内置函数的使用以及时钟控件的更新,
就可以轻松地实现动态时钟的功能。
只需要根据实际需求,添加相应的代码实现即可。
vb秒表
Dim xs As Integer ‘小时Dim fz As Integer ‘分钟Dim mz As Integer ‘秒钟Dim i As IntegerDim ha As IntegerPrivate Sub Command1_Click()Command1.Enabled = FalseCommand2.Enabled = TrueCommand3.Enabled = TrueTimer1.Enabled = TrueEnd SubPrivate Sub Command2_Click()If Command2.Caption = "暂停" ThenCommand2.Caption = "继续"Timer1.Enabled = FalseElseCommand2.Caption = "暂停"Timer1.Enabled = TrueEnd IfCommand3.Enabled = TrueCommand1.Enabled = FalseEnd SubPrivate Sub Command3_Click()Text1.Text = "00:00:00:00" ’把text1.text表清零Command1.Enabled = TrueCommand2.Enabled = FalseCommand3.Enabled = FalseTimer1.Enabled = Falsei = 0xs = 0fz = 0mz = 0li = " "End SubPrivate Sub Command4_Click()Unload MeEnd SubPrivate Sub Form_Load()Timer1.Enabled = FalseText1.Text = "00:00:00:00"End SubPrivate Sub Timer1_Timer()i = i + Timer1.Interval '间隔If i = 1000 Then ‘这里是比秒还更小的单位毫秒mz = mz + 1 ’秒进1i = 0‘毫秒清零End IfIf mz = 60 Then ‘每60秒进一分fz = fz + 1 ’分种进一mz = 0 ’秒清零End IfIf fz = 60 Then '每60分钟就进一个小时xs = xs + 1fz = 0End Ifha = i / 10 ‘百毫秒间隔If xs < 10 Thenli = "0" & xs & liElseli = xs & liEnd IfIf fz < 10 Thenli = li & ":0" & fzElseli = li & ":" & fzEnd IfIf mz < 10 Thenli = li & ":0" & mzElseli = li & ":" & mzEnd IfIf ha < 10 Thenli = li & ":0" & haElseli = li & ":" & haEnd IfText1.Text = liEnd Sub这个用timer计时器来设计的秒表,timer注意一个Interval属性,表示时间的间隔。
(vb编程)制作秒表
(vb编程)制作秒表VERSION 5.00Begin VB.Form Form1Caption = "Form1"ClientHeight = 10275ClientLeft = 60ClientTop = 450ClientWidth = 13440LinkTopic = "Form1"ScaleHeight = 10275ScaleWidth = 13440StartUpPosition = 3 '窗口缺省Begin VB.Timer Timer1Left = 1440Top = 4560EndEndAttribute VB_Name = "Form1"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalseOption ExplicitDim sngCX As Single, sngCY As Single '重置坐标Dim blnBeginFlag As Boolean '是否初次Dim sngRClock As Single '时钟半径Dim intPreHr As Integer '前次小时Dim intPreMin As Integer '前次分Dim intPreX As Integer '前次秒Dim intPreY As Integer '前次分Dim sngHLen As Single '时针长Dim sngMLen As Single '分针长Dim sngSLen As Single '秒针长Const pi = 3.1415926 Sub DrawClock() '画表盘的子程序Dim intI As IntegerDim sngX As Single, sngY As SingleCircle (0, 0), sngRClock '时钟外圆For intI = 0 To 60 '时钟刻度sngX = (sngRClock - 50) * Sin(intI * pi / 30)sngY = (sngRClock - 50) * Cos(intI * pi / 30)If (intI Mod 5) = 0 Then '绘主刻度DrawWidth = 4PSet (sngX, sngY)Else '绘次刻度DrawWidth = 2PSet (sngX, sngY)End IfNext intIDrawWidth = 1'建立打印日期和星期的矩形框Line (0.3 * sngRClock, 100)-(0.9 * sngRClock, -100) _, , B Line (0.7 * sngRClock, 100)-(0.7 * sngRClock, -100)FillStyle = 0 '填充FillColor = vbBlack '黑色Circle (0, 0), 70 '画中心的小圆End SubPrivate Sub Form_Load()Timer1.Interval = 1000Form1.AutoRedraw = True '窗体改变后可重绘sngCX = ScaleWidth / 2sngCY = ScaleHeight / 2'使窗口中心为(0,0)Scale (-sngCX, sngCY)-(sngCX, -sngCY)blnBeginFlag = TruesngRClock = 1500 '时钟半径sngHLen = sngRClock * 0.6 '时针长sngMLen = sngRClock * 0.8 '分针长sngSLen = sngRClock * 0.9 '秒针长End SubPrivate Sub Timer1_Timer()Dim intX As Integer, intY As IntegerDim intHr As Integer, intMin As IntegerDim intSec As IntegerDim sngH As SingleDim vntBlack, vntGray'初次执行需绘表盘If blnBeginFlag = True ThenDrawClockEnd If'填写日期和星期CurrentX = 0.7 * sngRClockCurrentY = 80Print Day(Now) '日期CurrentX = 0.35 * sngRClockCurrentY = 80Print WeekdayName(Weekday(Now)) '星期intHr = Hour(Now)intHr = intHr Mod 12 '使小时在0-11之间intMin = Minute(Now) '取得分DrawWidth = 3 '时针和分针宽度vntBlack = vbBlack '黑色时针及分针If blnBeginFlag = True ThenDrawMode = 13 'CopyPen绘图模式sngH = intHr * 5 + intMin / 12intX = sngHLen * Sin(sngH * pi / 30)intY = sngHLen * Cos(sngH * pi / 30)intPreHr = intHrLine (0, 0)-(intX, intY), vntBlack '绘时针intX = sngMLen * Sin(intMin * pi / 30)intY = sngMLen * Cos(intMin * pi / 30)Line (0, 0)-(intX, intY), vntBlack '绘分针intPreMin = intMinEnd IfIf intPreMin <> intMin Then '如果分针改变DrawMode = 10 'NotXorPen绘图模式sngH = intPreHr * 5 + intPreMin / 12intX = sngHLen * Sin(sngH * pi / 30)intY = sngHLen * Cos(sngH * pi / 30)Line (0, 0)-(intX, intY), vntBlack '清除旧时针intX = sngMLen * Sin(intPreMin * pi / 30)intY = sngMLen * Cos(intPreMin * pi / 30)Line (0, 0)-(intX, intY), vntBlack '清除旧分针DrawMode = 13 'CopyPen绘图模式sngH = intHr * 5 + intMin / 12intX = sngHLen * Sin(sngH * pi / 30)intY = sngHLen * Cos(sngH * pi / 30)intPreHr = intHrLine (0, 0)-(intX, intY), vntBlack '绘新时针intX = sngMLen * Sin(intMin * pi / 30)intY = sngMLen * Cos(intMin * pi / 30)Line (0, 0)-(intX, intY), vntBlack '绘新分针intPreMin = intMinEnd IfintSec = Second(Now) '取得秒intX = sngSLen * Sin(intSec * pi / 30)intY = sngSLen * Cos(intSec * pi / 30)DrawWidth = 1DrawMode = 10 'NotXorPen绘图模式vntGray = QBColor(8) '灰色秒针Line (0, 0)-(intPreX, intPreY), vntGrayLine (0, 0)-(intX, intY), vntGray '绘新秒针intPreX = intXintPreY = intYblnBeginFlag = False '已经执行过该程序End Sub。
VB计时器源代码
Dim day, year, month, ddate, ttime As String '定义时间变量Dim alf(0 To 11) '定义数组Dim rr '定义变量Dim ss, hh, mmDim i As IntegerDim nHourLen, nMinLen, nSecLen As Integer '定义时针,分针,秒针长度变量Const Pi = 3.1415926 '定义pi常亮Dim nWidth, alfsec, alfmin, alfhour As IntegerOption ExplicitPrivate Sub Form_Load()Timer1.Interval = 1000 '设置计时器每隔一秒变换一次End SubPrivate Sub Timer1_Timer()rr = PicTime.Height / 2ddate = Format(Now, "mm:dd:yy") '格式化日期变量ttime = Format(Now, "hh:mm:ss") '格式化时间变量month = Left(ddate, 2) '读取月day = Mid(ddate, 4, 2) '读取年year = Right(ddate, 2) '读取日ss = Right(ttime, 2) '读取秒hh = Left(ttime, 2) '读取时针值mm = Mid(ttime, 4, 2) '读取分TxtYear.Text = "20" & year '读取年份TxtMth.Text = month '读取月份TxtDay.Text = day '读取几号LblTime.Caption = Time '显示当前事件值nWidth = PicTime.Width - 40 '设置表盘半径nHourLen = nWidth * 4 / 18 '设置时针半径nMinLen = nWidth * 6 / 18 '设置分针半径nSecLen = nWidth * 8 / 18 '设置秒针半径alfsec = ((ss - 15) / 30) * Pi '设置秒针每次转动的角度alfmin = ((mm + ss / 60 - 15) / 30) * Pi '设置分针每次转动的角度alfhour = ((hh + mm / 60 + ss / 3600 - 15) / 6) * Pi '设置时针每次转动的角度PicTime.RefreshFor i = 0 To 11 '利用循环开始绘制表盘上的刻度alf(i) = i * 30 * Pi / 180PicTime.DrawWidth = 1If i = 0 Or i = 3 Or i = 6 Or i = 9 ThenPicTime.DrawWidth = 3End IfPicTime.Line (rr + (rr - 100) * Cos(alf(i)), rr + (rr - 100) * Sin(alf(i)))-(rr + (rr - 300) * Cos(alf(i)), rr + (rr - 300) * Sin(alf(i))), RGB(25, 25, 25)Next iPicTime.DrawWidth = 3 '设置时针宽度PicTime.Line (rr, rr)-(rr + nHourLen * Cos(alfhour), rr + nHourLen * Sin(alfhour)) '绘制时针PicTime.DrawWidth = 2 '设置分针宽度PicTime.Line (rr, rr)-(rr + nMinLen * Cos(alfmin), rr + nMinLen * Sin(alfmin)) '绘制分针PicTime.DrawWidth = 1PicTime.Line (rr, rr)-(rr + nSecLen * Cos(alfsec), rr + nSecLen * Sin(alfsec))PicTime.DrawWidth = 5 '设置中心点大小PicTime.PSet (rr, rr), RGB(255, 0, 255) '绘制中心点End Sub。
vb倒计时源程序代码
Module Module1Sub Main()Dim a As DateDim h, m, s As IntegerDim n, i As LongDim x, z As LongDim y As LongConsole.WriteLine("本程序为倒计时程序,请分部输入需要倒数的小时数、分钟数、秒数")Console.WriteLine("请输入计时的小时数(大于等于0的整数):")h = Console.ReadLine()Console.WriteLine("请输入计时的总分钟数(大于等于0并且小于60的整数):")m = Console.ReadLine()Console.WriteLine("请输入计时的秒数(大于等于0的并且小于60的整数):")s = Console.ReadLine()Console.WriteLine("当前系统时间为:{0}", h)a = Now()Console.WriteLine("当前系统时间为:{0}", a)Console.WriteLine("")Console.WriteLine(" **************倒计时开始***************** ")Console.WriteLine(" **************倒计时开始***************** ")Console.WriteLine(" **************倒计时开始***************** ")Console.WriteLine(" **************倒计时开始***************** ")z = 0x = 60For i = 0 To h * 3600 + m * 60 + sFor n = 0 To 6000000For y = 0 To 25z = z + 1NextNextIf (h <= 0) And (m <= 0) And (s <= 0) Then Exit ForIf s > 0 Thens = s - 1Console.WriteLine(" 剩余:{0}小时{1}分{2}秒", h, m, s)If (h <= 0) And (m <= 0) And (s <= 0) ThenConsole.WriteLine("倒计时完毕,随意输入字符将退出程序:")Exit ForEnd IfEnd IfIf s = 0 ThenIf m > 0 Thenm = m - 1s = 59Console.WriteLine(" 剩余:{0}小时{1}分{2}秒", h, m, s)End IfIf m = 0 And h > 0 And s = 0 Thenh = h - 1m = 59s = 59Console.WriteLine(" 剩余:{0}小时{1}分{2}秒", h, m, s)If (h <= 0) And (m <= 0) And (s <= 0) ThenConsole.WriteLine("倒计时完毕,随意输入字符将退出程序:")Exit ForEnd IfEnd IfEnd IfNextConsole.Read()End Sub End Module。
用VB制作一个动态时钟(转载)
用VB制作一个动态时钟(转载)[原理]使用了LINE控件用来表示指针,根据粗细不同,分别为时、分、秒针,每个LINE都有2个点的坐标,一个点是原点,也就是表盘的最中心点,另一个点根据当前的时间计算从而决定。
然后通过TIMER控件,每隔1秒计算一次进行刷新,即可以实现。
[实现]一、新建立一个工程,在默认的窗体上添加以下三个LINE控件和一个TIMER控件,添加以下界面如下:二、在代码最上面建立全局变量,用于保存时钟原点:Dim x0 As SingleDim y0 As Single三、在窗体的FORM_LOAD事件中添加如下代码:Private Sub Form_Load()With Form1.Width = 3000.Height = 3100.BackColor = vbBlack.Caption = '动态时钟'End Withx0 = 1430y0 = 1290With Line1.X1 = x0.Y1 = y0End WithWith Line2.X1 = x0.Y1 = y0End WithWith Line3.X1 = x0.Y1 = y0End WithLine1.Visible = FalseLine2.Visible = FalseLine3.Visible = FalseLine1.BorderColor = vbRedLine2.BorderColor = vbRedLine3.BorderColor = vbRedEnd Sub四、在Timer的Timer事件中添加如下代码Private Sub Timer1_Timer()Text1.Text = DateLine1.Visible = TrueLine2.Visible = TrueLine3.Visible = TrueDim R0 As IntegerR0 = 1200R1 = 1000: R2 = 850: R3 = 600'画12个大圈子For i = 1 To 12X1 = x0 + R0 * Sin((i * 30) * 3.1415926 / 180)Y1 = y0 + R0 * Cos((i * 30) * 3.1415926 / 180)Circle (X1, Y1), 30, vbBlueNext i'画60个小圈子For i = 1 To 60X1 = x0 + R0 * Sin((i * 6) * 3.1415926 / 180)Y1 = y0 + R0 * Cos((i * 6) * 3.1415926 / 180)Circle (X1, Y1), 10, vbBlueNext i'秒针With Line1.X2 = x0 - R3 * Sin(-(Hour(Now) * 30 + Minute(Now) * 0.5) * 3.1415926 / 180).Y2 = y0 - R3 * Cos(-(Hour(Now) * 30 + Minute(Now) * 0.5) * 3.1415926 / 180)End With'分针With Line2.X2 = x0 - R2 * Sin(-(Minute(Now) * 6) * 3.1415926 / 180).Y2 = y0 - R2 * Cos(-(Minute(Now) * 6) * 3.1415926 / 180)End With'时针With Line3.X2 = x0 - R1 * Sin(-(Second(Now) * 6) * 3.1415926 / 180) .Y2 = y0 - R1 * Cos(-(Second(Now) * 6) * 3.1415926 / 180) End WithCircle (x0, y0), 20, vbWhiteEnd SubOK,至此大功告成,点击F5,就可以看到运行结果了。
基于VB6.0的钟表设计
目录摘要 (iii)第1章概述 (1)1.1 引言 (1)1.2 设计平台 (1)第2章开发工具简介 (2)2.1 VisualBasic6.0简介 (2)2.2 运用的控件和主要对象 (3)3.1主窗体 (4)Private Sub qiyong_Click() (5)3.2时钟窗体 (6)3.3屏幕保护窗体 (10)第4章结束语 (12)参考文献 (13)摘要Visual Basic(VB)是一种由微软公司开发的包含协助开发环境的事件驱动编程语言,从任何标准来说,VB都是世界上使用人数最多的语言。
它是一种面向对象的可视化程序设计语言,提供了集界面设计、编写代码和程序调试等于一体的集成开发环境。
VB拥有图形用户界面(GUI)和快速应用程序开发(RAD)系统,可以轻易的使用DAO、RDO、ADO连接数据库,或者轻松的创建ActiveX控件。
程序员可以轻松的使用VB提供的组件快速建立一个应用程序。
本文通过动画式时钟的设计与实现,,同时说明了在VB环境下开发应用程序的思路和步骤。
关键词:程序设计;时钟;Visual Basic6.0;界面第1章概述1.1 引言visual basic继承了basic语言易学易用的特点,特别适合于初学者学习windows系统编程。
随着21世纪信息社会的到来,计算机在人们的工作和生活中的深入,要求我们越来越多地与计算机打交道,为了用户更加方便的,更加有舒适的心情,设计出一个比较舒心的时钟显示。
1.2 设计平台VB全称Visual Basic,它是以Basic语言作为其基本语言的一种可视化编程工具。
它继承了basic语言易学易用的特点,特别适合于初学者学习windows系统编程。
随着21世纪信息社会的到来,计算机在人们的工作和生活中的深入,要求我们越来越多地与计算机打交道,为了使用户更加地使用舒心。
第2章开发工具简介2.1 VisualBasic6.0简介Visual Basic 是一种可视化的、面向对象和采用事件驱动方式的结构化高级程序设计语言,可用于开发 Windows 环境下的各类应用程序。
vb课程设计计时器
vb课程设计 计时器一、课程目标知识目标:1. 学生理解计时器的功能与作用,掌握计时器的基本概念。
2. 学生掌握Visual Basic(VB)中实现计时器功能的相关语法和编程方法。
3. 学生了解计时器在现实生活中的应用场景,并能结合实际需求设计计时器程序。
技能目标:1. 学生能够运用VB编程语言,独立编写具有计时功能的程序。
2. 学生通过动手实践,培养解决问题的能力和逻辑思维能力。
3. 学生能够对计时器程序进行调试和优化,提高程序的性能和稳定性。
情感态度价值观目标:1. 学生培养对编程的兴趣和热情,激发创新意识。
2. 学生通过合作学习,培养团队协作精神和沟通能力。
3. 学生在编程实践中,体验科技带来的乐趣,增强自信心和成就感。
课程性质:本课程为信息技术课程,旨在让学生掌握VB编程中计时器功能的应用。
学生特点:六年级学生具备一定的计算机操作能力和编程基础,对新鲜事物充满好奇心,喜欢动手实践。
教学要求:注重理论与实践相结合,引导学生主动探究,激发学生的学习兴趣,培养其编程能力和逻辑思维。
将课程目标分解为具体的学习成果,便于后续教学设计和评估。
二、教学内容1. 计时器原理介绍:计时器的工作原理、分类以及在计算机编程中的应用。
- 相关教材章节:第五章第三节“计时器与定时器”2. VB中实现计时器功能的相关语法和控件:- Timer控件的使用方法- Timer事件(如:Timer_Tick)的处理- 相关教材章节:第五章第四节“使用Timer控件”3. 设计与编写计时器程序:- 程序设计思路与流程图绘制- 编写计时器程序代码,实现倒计时、正计时等功能- 相关教材章节:第五章第五节“计时器程序设计”4. 计时器程序调试与优化:- 程序调试方法与技巧- 优化程序性能,提高计时精度- 相关教材章节:第五章第六节“程序调试与优化”5. 计时器应用实例分析:- 分析现实生活中的计时器应用案例- 结合实际需求设计具有特色的计时器程序- 相关教材章节:第五章实例分析“计时器应用实例”教学内容安排与进度:第一课时:计时器原理介绍,学习Timer控件的使用方法第二课时:编写计时器程序,实现基本计时功能第三课时:计时器程序调试与优化,分析应用实例第四课时:学生展示作品,总结反馈,巩固提高教学内容科学系统,注重理论与实践相结合,引导学生主动探究,培养其编程能力和逻辑思维。
VB编辑时钟控件+倒计时+闹钟+背景图片+背景音乐
Private Sub Check1_Click()WindowsMediaPlayer1.Controls.pauseEnd SubPrivate Sub Check2_Click()WindowsMediaPlayer1.Controls.playEnd SubPrivate Sub Check3_Click()WindowsMediaPlayer1.Controls.stopEnd SubPrivate Sub Command1_Click()Timer1.Interval = 1000Timer1.Enabled = TrueEnd SubPrivate Sub Command2_Click()Timer2.Enabled = TrueTimer2.Interval = 1000End SubPrivate Sub Form_Load()Picture1.Picture = LoadPicture(App.Path & "\images\" & "tp1.jpg") End SubPrivate Sub Timer1_Timer()Static X!, Y!, Z!, a%, b%, c%, q%, w%, E%Const n = 3.141592653Command1.Visible = FalsePicture1.ClsPicture1.Scale (-1000, 1000)-(1000, -1000)Picture1.Circle (0, 0), 900Dim l As DoubleDim r As DoubleFor i = 0 To 60l = (1 - Sin(i / 60 * 2 * 3.1415926)) * 900 - 900r = (1 - Cos(i / 60 * 2 * 3.1415926)) * 900 - 900Picture1.DrawWidth = 3Picture1.PSet (l, r)Picture1.DrawWidth = 4Next iPicture1.PSet (0, 0)Picture1.CurrentX = -100: Picture1.CurrentY = 850Picture1.FontSize = 25Picture1.Print "12"Picture1.CurrentX = -40: Picture1.CurrentY = -690Picture1.Print "6"Picture1.CurrentX = -850: Picture1.CurrentY = 80Picture1.Print "9"Picture1.CurrentX = 700: Picture1.CurrentY = 100Picture1.Print "3"Picture1.FontSize = 18Picture1.CurrentX = 360: Picture1.CurrentY = 740Picture1.Print "1"Picture1.CurrentX = 650: Picture1.CurrentY = 450Picture1.Print "2"Picture1.CurrentX = 630: Picture1.CurrentY = -320Picture1.Print "4"Picture1.CurrentX = 360: Picture1.CurrentY = -600Picture1.Print "5"Picture1.CurrentX = -430: Picture1.CurrentY = 740Picture1.Print "11"Picture1.CurrentX = -750: Picture1.CurrentY = 440Picture1.Print "10"Picture1.CurrentX = -700: Picture1.CurrentY = -360Picture1.Print "8"Picture1.CurrentX = -370: Picture1.CurrentY = -610Picture1.Print "7"Picture1.DrawWidth = 1X = Second(Time) * 2 * n / 60Y = Minute(Time) * 2 * n / 60Z = Hour(Time) * 2 * n / 12Picture1.Line (0, 0)-(800 * Sin(X), 800 * Cos(X))Picture1.Circle (700 * Sin(X), 700 * Cos(X)), 50Picture1.Line (0, 0)-(600 * Sin(Y), 600 * Cos(Y)), QBColor(5)Picture1.Line (0, 0)-(400 * Sin(Z), 400 * Cos(Z)), QBColor(12)h = Year(Now)i = Month(Now)j = Day(Now)k = Hour(Now)l = Minute(Now)m = Second(Now)Label10.Caption = h & "-" & i & "-" & jLabel11.Caption = k & ":" & l & ":" & mu = Weekday(Now)Select Case uCase 1Label8.Caption = "一"Case 2Label8.Caption = "二"Case 3Label8.Caption = "三"Case 4Label8.Caption = "四"Case 5Label8.Caption = "五"Case 6Label8.Caption = "六"Case 7Label8.Caption = "日"End SelectEnd SubPrivate Sub Timer2_Timer()Dim z1, z2, z3, z4, z5, z6, z7, z8, z9, z10%z1 = V al(Text1.Text)z2 = V al(Text2.Text)z3 = V al(Text3.Text)z4 = V al(Text4.Text)z5 = V al(Text5.Text)z6 = V al(Text6.Text)date1 = DateSerial(z1, z2, z3)date2 = DateSerial(Year(Now), Month(Now), Day(Now))z7 = DateDiff("d", date2, date1)time1 = TimeSerial(z4, z5, z6)time2 = TimeSerial(Hour(Now), Minute(Now), Second(Now)) z8 = DateDiff("h", time2, time1)z9 = DateDiff("n", time2, time1) - z8 * 60z10 = DateDiff("s", time2, time1) - z9 * 60 - z8 * 3600If z2 > 12 Or z3 > 31 Or z4 > 23 Or z5 > 59 Or z6 > 59 Then q = MsgBox("输入有误,请重新输入", , "出错")Print qEnd IfIf z10 < 0 Thenz9 = z9 - 1z10 = 60 + z10End IfIf z9 < 0 Thenz8 = z8 - 1z9 = 60 + z9End IfIf z8 < 0 Thenz7 = z7 - 1z8 = 24 + z8End IfLabel14.Caption = z7 & "天" & z8 & "小时" & z9 & "分" & z10 & "秒"If z7 = 0 And z8 = 0 And z9 = 0 And z10 = 0 ThenMsgBox "时间到", , "倒计时"WindowsMediaPlayer1.URL = App.Path & "\music\" & "dd.mp3" Timer2.Enabled = FalseEnd IfEnd SubPrivate Sub tp1_Click()Picture1.Picture = LoadPicture(App.Path & "\images\" & "tp6.jpg") End SubPrivate Sub tp2_Click()Picture1.Picture = LoadPicture(App.Path & "\images\" & "tp2.jpg") End SubPrivate Sub tp3_Click()Picture1.Picture = LoadPicture(App.Path & "\images\" & "tp3.jpg") End SubPrivate Sub tp4_Click()Picture1.Picture = LoadPicture(App.Path & "\images\" & "tp4.jpg") End SubPrivate Sub tp5_Click()Picture1.Picture = LoadPicture(App.Path & "\images\" & "tp5.jpg") End SubPrivate Sub yy1_Click()WindowsMediaPlayer1.URL = App.Path & "\music\" & "He's a Pirate.mp3" End SubPrivate Sub yy2_Click()WindowsMediaPlayer1.URL = App.Path & "\music\" & "apologize.mp3" End SubPrivate Sub yy3_Click()WindowsMediaPlayer1.URL = App.Path & "\music\" & "Heartbeats.mp3" End Sub。
VB编写漂亮的时钟
首先打开vb6.0,新建一个工程,在窗体中放一个imagebox控件然后在其中放一个timer控件再就是一个直线控件数组注意要把imagebox中的stretch属性改为true,直线控件数组是14个,要注意下面是我和程序代码,有一些注解的希望能对大家有帮助Private Sub Form_Load() '设置窗体和计时器参数Timer1.Interval = 100 '设置计时器事件间隔是1/10秒Width = 4000Height = 4000Left = Screen.Width \ 2 - 2000Top = (Screen.Height - Height) \ 2End SubPrivate Sub Form_Resize() '启动时和改变窗体时设置刻度和指针Dim i, angleFor i = 0 To 13Line1(i).Visible = TrueLine1(i).BorderWidth = 4If i = 0 Or i > 12 ThenLine1(i).BorderColor = RGB(0, 0, 128)ElseLine1(i).BorderColor = RGB(0, 128, 0)End IfNext iLine1(12).BorderColor = RGB(128, 256, 128)Line1(9).BorderColor = RGB(256, 128, 128)Line1(6).BorderColor = RGB(128, 0, 256)Line1(3).BorderColor = RGB(0, 0, 255)Line1(13).BorderColor = RGB(255, 0, 255)Line1(0).BorderWidth = 5 '时针的粗细Line1(14).BorderWidth = 2 '分针的粗细Line1(i).BorderColor = RGB(256, 0, 0)For i = 0 To 14Scale (-1, 1)-(1, -1) '画出表盘12个点和时,分.秒15条线angle = i * 2 * Atn(1) / 3Line1(i).X1 = 0.9 * Cos(angle)Line1(i).Y1 = 0.9 * Sin(angle)Line1(i).X2 = Cos(angle)Line1(i).Y2 = Sin(angle)Next iEnd SubPrivate Sub Timer1_Timer()Const hh = 0Const mh = 13Const sh = 14Dim angleStatic lsIf Second(Now) = ls Then Exit Sub '机器时钟停止程序退出ls = Second(Now)angle = 0.5236 * (15 - (Hour(Now) + Minute(Now) / 60)) '时钟转动设置Line1(hh).X1 = 0Line1(hh).Y1 = 0Line1(hh).X2 = 0.3 * Cos(angle)Line1(hh).Y2 = 0.3 * Sin(angle)angle = 0.1047 * (75 - (Minute(Now) + Second(Now) / 60)) '分钟转动设置Line1(mh).X1 = 0Line1(mh).Y1 = 0Line1(mh).X2 = 0.7 * Cos(angle)Line1(mh).Y2 = 0.7 * Sin(angle)angle = 0.5236 * (75 - Second(Now) / 5) '秒钟转动设置Line1(sh).X1 = 0Line1(sh).Y1 = 0Line1(sh).X2 = 0.8 * Cos(angle)Line1(sh).Y2 = 0.8 * Sin(angle)Form1.Caption = Str(Now()) '窗口显示日期和时间End Sub。
VB程序设计_计时器控件
VB程序设计_计时器控件VB程序设计中的计时器控件是一种用来进行计时操作的控件,它能够按照一定的时间间隔触发事件,从而实现定时执行特定的功能。
在VB 程序中,计时器控件是非常常用的一个控件,可以用于各种需要定时操作的场景,比如定时刷新、定时保存等。
要使用计时器控件,首先需要在VB的工具箱中找到计时器控件,然后将其拖放到窗体上。
默认情况下,计时器控件是不可见的,只是作为一个后台的计时器运行。
计时器控件有一些常用的属性和方法可以进行设置和操作,下面我们来介绍一下。
首先是最基本的属性:Interval。
这个属性表示计时器的时间间隔,单位是毫秒。
比如设置为1000,表示每隔一秒触发一次计时器的Tick事件。
可以通过代码直接设置该属性,也可以通过设计界面上的属性窗格进行设置。
其次是Enabled属性,表示是否启用计时器。
通过设置该属性为True或False,可以控制计时器的启用和停用。
需要注意的是,启用计时器之后,计时器才会按照设置的间隔时间触发Tick事件,停用计时器之后,计时器就不会再触发事件。
然后是Tick事件,这个事件是计时器控件的核心事件。
每当计时器达到指定的时间间隔时,就会触发一次Tick事件。
我们可以在这个事件中编写相应的代码来实现我们需要的功能。
比如可以在Tick事件中更新界面上的显示内容、进行定时保存等操作。
另外还有一些其他的属性和方法,例如Tag属性,用于存储一些额外的信息;Start方法,用于手动启动计时器;Stop方法,用于停止计时器。
这些属性和方法都可以根据实际需求进行使用。
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.TickLabel1.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")End Sub总结来说,计时器控件是VB程序设计中非常常用的一个控件,它可以实现定时操作,通过设置Interval属性和编写Tick事件中的代码,可以实现各种需要定时执行的功能。
VB报告秒表计时器
实验报告2:秒表计时器二、编写一个秒表操作程序,分别显示开始计时的时间、当前时间和开始于停止的时间间隔。
1、目的和要求(1)熟练掌握时间控件的使用。
(2)复习巩固不同类型的变量用法及自动类型转换。
2、内容和步骤(1)创建窗体窗体form1由三个命令按钮command1、command2、command3、三个标签label1、label2、label3、三个文本框text1 、text2、text3,Timer1组成。
(2)设计窗体界面及属性属性设置:对象名控件名属性名属性值功能command1 command1 Caption 开始计时器开始command1 Caption 停止计时器停止command1 Caption 退出结束程序label1 label1 Caption 开始提示label1 Caption 时间提示label1 Caption 间隔提示说明text1 text1 Text 空显示开始时间text1 Text 空显示当前时间text1 Text 空显示时间间隔Timer Timer1 Interval 1000 设置间隔为1秒Timer1 Enabled False 计时器起始状态(3)编写代码Dim a As Date, b As Date, c As DatePrivate Sub Command1_Click()Timer1.Enabled = Truea = TimeText1.Text = aEnd SubPrivate Sub Command2_Click()b = TimeText2.Text = bc = b - aText3.Text = cEnd SubPrivate Sub Command3_Click()Unload MeEnd Sub(4)保存工程,运行程序3、调试过程及运行结果比如最开始没有定义文本框内变量的date型,导致结果出错及解决过程和办法。
VB多功能计时器代码
frmTimer窗体控件:commandbutton * 2 picturebox * 1 pictureclip(注:图片剪切控件) * 1 timer * 3(注:此处只给出了控件类型,控件名祥见代码)frmTimer 的代码Option ExplicitPublic t As Integer, str As StringPublic h As Integer, m As Integer, s As IntegerPrivate Sub Show_LED(picTimer As PictureBox, str As String)'显示图片的通用过程Dim s As String * 1Dim pos As IntegerDim n As IntegerDim i As IntegerFor i = 1 To Len(str)s = Mid(str, i, 1)n = -1Select Case sCase "0" To "9"n = CInt(s)Case ":"n = 10Case "-"n = 11Case "."n = 12Case "a", "A"n = 13Case "p", "P"n = 14Case " "n = 15End SelectIf n <> -1 ThenpicTimer.PaintPicture PCTimer.GraphicCell(n), pos, 0pos = pos + 300End IfNextEnd SubPrivate Sub cmdRestart_Click() '秒表的回零h = 0m = 0s = 0str = ""cmdStart.Caption = "开始"Timer1.Enabled = False: str = "00:00:00"ClsCall Show_LED(picTimer, str)End SubPrivate Sub cmdStart_Click() '秒表的开始和暂停控制If cmdStart.Caption = "开始" Thenh = 0m = 0s = 0cmdStart.Caption = "暂停"Timer1.Enabled = TruecmdRestart.Enabled = FalseExit SubElseIf cmdStart.Caption = "暂停" ThencmdStart.Caption = "继续"Timer1.Enabled = FalsecmdRestart.Enabled = TrueExit SubElsecmdStart.Caption = "暂停"Timer1.Enabled = TruecmdRestart.Enabled = FalseExit SubEnd IfEnd SubPrivate Sub Form_Load() '窗体加载Call mnuClock_ClickEnd SubPrivate Sub mnuClock_Click() '时钟picTimer.ClsMe.ClsTimer1.Enabled = FalseTimer2.Enabled = False cmdStart.Visible = FalsecmdRestart.Visible = FalsefrmTimer.Caption = "时钟"Timer3.Enabled = TruepicTimer.Visible = TrueClsEnd SubPrivate Sub mnuCuntDwnClock_Click() '倒计时picTimer.ClsMe.ClsTimer1.Enabled = FalseTimer3.Enabled = FalsecmdStart.Visible = FalsecmdRestart.Visible = FalsefrmSet.Visible = TruefrmTimer.Caption = "倒计时钟" picTimer.Visible = TrueEnd SubPrivate Sub mnuExit_Click() '退出选项Unload frmTimer: Unload frmSetEnd SubPrivate Sub mnuStopwatch_Click() '秒表picTimer.ClsMe.ClsTimer1.Enabled = FalseTimer2.Enabled = FalseTimer3.Enabled = FalsecmdStart.Visible = TruecmdRestart.Visible = TruefrmTimer.Caption = "秒表"picTimer.Visible = TrueEnd SubPrivate Sub Timer1_Timer() '秒表计时过程str = ""s = s + 1If s = 100 Then '秒表数字显示的处理s = 0m = m + 1If m = 60 Thenm = 0h = h + 1If h = 24 Thenh = 0End IfEnd IfEnd IfIf h < 10 Then '数字显示格式的处理str = str & 0 & hElse: str = str & hEnd IfIf m < 10 Thenstr = str & ":" & 0 & mElse: str = str & ":" & mEnd IfIf s < 10 Thenstr = str & ":" & 0 & sElse: str = str & ":" & sEnd IfClsCall Show_LED(picTimer, str)End SubPrivate Sub Timer2_Timer() '倒计时计时过程picTimer.Clsstr = ""t = h * 3600 + m * 60 + sIf t >= 1 Thens = s - 1If s = -1 Thens = 59m = m - 1End IfIf m = -1 Thenm = 59h = h - 1End IfIf h < 10 Thenstr = str & "0" & hElsestr = str & ":" & hEnd IfIf m < 10 Thenstr = str & ":" & "0" & m Elsestr = str & ":" & mEnd IfIf s < 10 Thenstr = str & ":" & "0" & sElsestr = str & ":" & sEnd IfClsCall Show_LED(picTimer, str)ElsepicTimer.Visible = FalseClsfrmTimer.Print "时间到"Timer2.Enabled = FalseEnd IfEnd SubPrivate Sub Timer3_Timer() '时钟显示过程str = CStr(Now)Call Show_LED(picTimer, str)ClsEnd SubfrmTimer窗体控件:text * 3commandbuton * 1label * 3(注:控件名称参照代码)frmSet 的代码Option ExplicitPrivate Sub cmdCuntStart_Click()frmTimer.h = Val(txtInput(0))frmTimer.m = Val(txtInput(1))frmTimer.s = Val(txtInput(2))If frmTimer.h + frmTimer.m + frmTimer.s = 0 ThenCall MsgBox("输入错误,请重新输入", 48, "多功能计时器") ElsefrmTimer.Timer2.Enabled = TrueUnload frmSetEnd IfEnd Sub。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 10275
ClientLeft = 60
ClientTop = 450
ClientWidth = 13440
LinkTopic = "Form1"
ScaleHeight = 10275
ScaleWidth = 13440
StartUpPosition = 3 '窗口缺省
Begin VB.Timer Timer1
Left = 1440
Top = 4560
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim sngCX As Single, sngCY As Single '重置坐标
Dim blnBeginFlag As Boolean '是否初次Dim sngRClock As Single '时钟半径Dim intPreHr As Integer '前次小时Dim intPreMin As Integer '前次分
Dim intPreX As Integer '前次秒
Dim intPreY As Integer '前次分
Dim sngHLen As Single '时针长Dim sngMLen As Single '分针长Dim sngSLen As Single '秒针长Const pi = 3.1415926
Sub DrawClock() '画表盘的子程序Dim intI As Integer
Dim sngX As Single, sngY As Single
Circle (0, 0), sngRClock '时钟外圆
For intI = 0 To 60 '时钟刻度
sngX = (sngRClock - 50) * Sin(intI * pi / 30)
sngY = (sngRClock - 50) * Cos(intI * pi / 30)
If (intI Mod 5) = 0 Then '绘主刻度
DrawWidth = 4
PSet (sngX, sngY)
Else '绘次刻度
DrawWidth = 2
PSet (sngX, sngY)
End If
Next intI
DrawWidth = 1
'建立打印日期和星期的矩形框
Line (0.3 * sngRClock, 100)-(0.9 * sngRClock, -100) _
, , B Line (0.7 * sngRClock, 100)-(0.7 * sngRClock, -100)
FillStyle = 0 '填充
FillColor = vbBlack '黑色
Circle (0, 0), 70 '画中心的小圆
End Sub
Private Sub Form_Load()
Timer1.Interval = 1000
Form1.AutoRedraw = True '窗体改变后可重绘
sngCX = ScaleWidth / 2
sngCY = ScaleHeight / 2
'使窗口中心为(0,0)
Scale (-sngCX, sngCY)-(sngCX, -sngCY)
blnBeginFlag = True
sngRClock = 1500 '时钟半径
sngHLen = sngRClock * 0.6 '时针长
sngMLen = sngRClock * 0.8 '分针长
sngSLen = sngRClock * 0.9 '秒针长
End Sub
Private Sub Timer1_Timer()
Dim intX As Integer, intY As Integer
Dim intHr As Integer, intMin As Integer
Dim intSec As Integer
Dim sngH As Single
Dim vntBlack, vntGray
'初次执行需绘表盘
If blnBeginFlag = True Then
DrawClock
End If
'填写日期和星期
CurrentX = 0.7 * sngRClock
CurrentY = 80
Print Day(Now) '日期
CurrentX = 0.35 * sngRClock
CurrentY = 80
Print WeekdayName(Weekday(Now)) '星期
intHr = Hour(Now)
intHr = intHr Mod 12 '使小时在0-11之间intMin = Minute(Now) '取得分
DrawWidth = 3 '时针和分针宽度vntBlack = vbBlack '黑色时针及分针
If blnBeginFlag = True Then
DrawMode = 13 'CopyPen绘图模式
sngH = intHr * 5 + intMin / 12
intX = sngHLen * Sin(sngH * pi / 30)
intY = sngHLen * Cos(sngH * pi / 30)
intPreHr = intHr
Line (0, 0)-(intX, intY), vntBlack '绘时针
intX = sngMLen * Sin(intMin * pi / 30)
intY = sngMLen * Cos(intMin * pi / 30)
Line (0, 0)-(intX, intY), vntBlack '绘分针
intPreMin = intMin
End If
If intPreMin <> intMin Then '如果分针改变
DrawMode = 10 'NotXorPen绘图模式sngH = intPreHr * 5 + intPreMin / 12
intX = sngHLen * Sin(sngH * pi / 30)
intY = sngHLen * Cos(sngH * pi / 30)
Line (0, 0)-(intX, intY), vntBlack '清除旧时针
intX = sngMLen * Sin(intPreMin * pi / 30)
intY = sngMLen * Cos(intPreMin * pi / 30)
Line (0, 0)-(intX, intY), vntBlack '清除旧分针
DrawMode = 13 'CopyPen绘图模式
sngH = intHr * 5 + intMin / 12
intX = sngHLen * Sin(sngH * pi / 30)
intY = sngHLen * Cos(sngH * pi / 30)
intPreHr = intHr
Line (0, 0)-(intX, intY), vntBlack '绘新时针
intX = sngMLen * Sin(intMin * pi / 30)
intY = sngMLen * Cos(intMin * pi / 30)
Line (0, 0)-(intX, intY), vntBlack '绘新分针
intPreMin = intMin
End If
intSec = Second(Now) '取得秒intX = sngSLen * Sin(intSec * pi / 30)
intY = sngSLen * Cos(intSec * pi / 30)
DrawWidth = 1
DrawMode = 10 'NotXorPen绘图模式vntGray = QBColor(8) '灰色秒针
Line (0, 0)-(intPreX, intPreY), vntGray
Line (0, 0)-(intX, intY), vntGray '绘新秒针
intPreX = intX
intPreY = intY
blnBeginFlag = False '已经执行过该程序End Sub。