注册 登录
编程论坛 VB6论坛

谁给写一个中断定时50ms精确地程序

chen3bing 发布于 2019-01-06 15:19, 3170 次点击
谁给写一个中断定时50ms精确地程序,谢谢!
15 回复
#2
chen3bing2019-01-06 15:55
我不会用多媒体定时器,谁教教我
#3
chen3bing2019-01-06 16:07
用API方式定时举个例子
#4
wds12019-01-06 16:54
'延时调用函数
Public Declare Function timeGetTime Lib "winmm.dll" () As Long

call sleep(50)'相当于延时50ms


Public Sub sleep(seconds As Integer)
Dim temp As Variant
temp = timeGetTime
  While timeGetTime - seconds < temp
  DoEvents
  Wend
End Sub
#5
wds12019-01-06 22:43
类模块程序
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    Form1.Print Now()
    DoEvents
End Sub

窗体程序
Public ID1
Dim bz1 As Boolean

Private Sub Form_Load()
    bz1 = False
   Command1.Caption = "定时开始"
End Sub

Private Sub Command1_Click()
    If bz1 = False Then
        '每50ms调用一次函数
        ID1 = SetTimer(0, 0, 50, AddressOf TimerProc)
        bz1 = True
        Command1.Caption = "定时结束"
    Else
        KillTimer 0, ID1
        bz1 = False
        Command1.Caption = "定时开始"
    End If
End Sub

#6
chen3bing2019-01-07 08:40
回复 5楼 wds1
谢谢!类模块程序有错误
只有本站会员才能查看附件,请 登录
#7
chen3bing2019-01-07 09:00
只有本站会员才能查看附件,请 登录

只有16次,貌似定时不准。
#8
wds12019-01-07 11:23
用这个吧
mmTimer.ctl
#9
chen3bing2019-01-07 11:34
回复 8楼 wds1
谢谢,mmt出来了,可是我想放到窗体上,提示错误
只有本站会员才能查看附件,请 登录
#10
wds12019-01-07 11:40
只有本站会员才能查看附件,请 登录


Private Sub CmdStart_Click()
        mmTimer1.Init 50
        Timer1.Interval = 50
        Timer1.Enabled = True
End Sub

Private Sub CmdStop_Click()
        mmTimer1.Destroy
        Timer1.Enabled = False
End Sub


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        Static cnt As Long
        cnt = cnt + 1
        Text1.Text = cnt
End Sub


Private Sub mmTimer1_Timer()
        Static cnt As Long
        frmMain.Print Now()
        cnt = cnt + 1
        Text1.Text = cnt
End Sub

Private Sub Timer1_Timer()
        Static cnt As Long
        cnt = cnt + 1
        Text2.Text = cnt
End Sub
#11
wds12019-01-07 11:43
用这个吧
mmTimer.ctl
#12
chen3bing2019-01-07 11:44
回复 10楼 wds1
谢谢!你这个运行不了。
现在我的工程添加mmTimer时,变成了这样。
只有本站会员才能查看附件,请 登录
#13
wds12019-01-07 12:27
引用mmm,mmtCTL.ocx

#14
chen3bing2019-01-07 13:07
回复 13楼 wds1
找不到那个引用
#15
chen3bing2019-01-07 13:24
我点击浏览mmtctl.ocx
提示文件重复。
只有本站会员才能查看附件,请 登录
#16
chen3bing2019-01-07 14:06
这下时间比较准了,结贴
1