当前位置: 网学 > 编程文档 > VB > 正文

VB高精度计时器编程的讨论

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/16
下载{$ArticleTitle}原创论文样式
Frequency lim
     
    ''将频率除以1000就的出时钟1毫秒震动的次数
    lMSFreq = (lim.highpart * 2 ^ 16) \ 1000 + lim.lowpart \ 1000
    Timer1.Interval = 10
    Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
    TimerCount = TimerCount - 0.01
    Text3.Text = Format$(TimerCount, "00.00")
    If TimerCount <= 0 Then
Timer1.Enabled = False
End If
End Sub
在Project中加入一个Module,然后在其中加入以下代码:
Option Explicit

Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type

Public Declare Function QueryPerformanceCounter Lib "kernel32" _
(lpPerformanceCount As LARGE_INTEGER) As Long
Public Declare Function QueryPerformanceFrequency Lib "kernel32" _
(lpFrequency As LARGE_INTEGER) As Long
Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal _
uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, _
ByVal uFlags As Long) As Long
Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
Public Declare Function GetTickCount Lib "kernel32" () As Long

Public lMSFreq As Long
Public TimerCount As Single
Public lmmCount As Single
Public lTimeID As Long
Public actTime1 As Long
Public actTime2 As Long
Public iCountStart As Single

Dim iCount As Single

''timeSetEvent的回调函数
Sub TimeProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, _
ByVal dw1 As Long, ByVal dw2 As Long)

Form1.Text2.Text = Format$(lmmCount, "00.00")
lmmCount = lmmCount - 0.01
If lmmCount <= 0 Then
iCountStart = 60
lmmCount = 60
TimerCount = 60
EndCount
End If
End Sub
Sub EndCount()
iCount = iCountStart
iCountStart = 0
timeKillEvent lTimeID
actTime2 = GetTickCount - actTime1
With Form1
.Command1.Enabled = True
.Command2.Enabled = False
.Timer1.Enabled = False

.Text1 = "计数器记时" + Format$((60 - iCount), "00.00") + " " _
+ "实际经过时间" + Format$((actTime2 / 1000), "00.00")
.Text2 = "计数器记时" + Format$((60 - lmmCount), "00.00") + " " _
+ "实际经过时间" + Format$((actTime2 / 1000), "00.00")
.Text3 = "计数器记时" + Format$((60 - TimerCount), "00.00") + " " _
+ "实际经过时间" + Format$((actTime2 / 1000), "00.00")
End With
End Sub


运行程序,点击“开始倒记时”按钮开始倒记时,可以看到两种API记时器工作基本正常,文本框中的倒记时显示流畅,而Timer控件的时间显示相比之下却不堪重负,十分缓慢。按“停止记时”按钮就可以停止倒记时,由图1可以看到,两种API记时器的累计误差在2‰以下,考虑到系统原因和处理记时显示的时间,这个误差基本是可以接受的,而且经过作者的多次检测,误差都在3‰以下。而Timer控件的误差简直是无法接受的。

在运行程序时作者还发现一个问题,如果在倒记时时拖动窗口,文本框中的显示都会停止,而当停止窗口拖放后,多媒体记时
  • 下一篇资讯: 用VB开发托盘程序
  • 网学推荐

    免费论文

    原创论文

    浏览:
    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
    湘ICP备09003080号