问题描述

winmm控件在vb.NET中的使用方法通过下述方法给以简单说明,下例中建立一个窗口,通过按钮可以观察从读取窗口后时间(ms)

案例

winmm动态链接库时间控制方法
右击右上角windowsApplication,加入winmm动态链接库。代码如下

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        expThread = New Thread(AddressOf expThreadRun2)‘1号标记点
        expThread.Start()

    End Sub
#Region "Winmm"
    Public clock As Long = 0  'ms为单位计时
    Public ExpPeriod As Integer   '实验周期,即多少秒进行一次实验

    '多媒体计时器
    Public Delegate Sub TimerEventHandler(uID As Integer, uMsg As Integer, _dwUser As Integer, dw1 As Integer, dw2 As Integer)
    Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Integer, ByVal uResolution As Integer, ByVal lpFunction As TimerEventHandler, ByVal dwUser As Integer, ByVal uFlags As Integer) As Integer
    Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Integer) As Integer
    Public MytimerHandler As TimerEventHandler '时间句柄
#End Region
    Public Sub ClockProc()
        If clock = 10000 Then
            clock = 0
            MessageBox.Show("完成一轮")
        End If
        clock = clock + 5
    End Sub
    Public TimeID1 As Long
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        MytimerHandler = AddressOf ClockProc
        TimeID1 = timeSetEvent(5, 1, MytimerHandler, 1, 1) '打开计时器
        clock = 0

    End Sub
    Public Sub expThreadRun2()
        While (1)
            expThreadRun()
        End While
    End Sub
    Public Sub expThreadRun()


        If clock = 6000 Then
            timeKillEvent(TimeID1) '关闭计时器
        Else
            Me.Invoke(New expThreadEunDelegate(AddressOf textCstr))


        End If

    End Sub
    Public expThread As Thread
    Private Sub textCstr()
        TextBox1.Text = Str(clock)
    End Sub
    Public Delegate Sub expThreadEunDelegate()


End Class

实验效果

若是在1号标记点将AddressOf expThreadRun2改为AddressOf expThreadRun,实验中只会在按button时出现数字,当时间到10000ms时出现messagebox。当使用上图代码时框中代码随时间改变,且到6000ms时如代码所示停止计时器,框体显示5995.
winmm动态链接库时间控制方法

知识点

while(1)
end while
重复过程中内容。

相关文章:

  • 2021-11-18
  • 2021-08-30
  • 2021-10-04
  • 2021-08-05
  • 2022-12-23
  • 2022-02-11
  • 2021-12-02
猜你喜欢
  • 2021-07-24
  • 2022-12-23
  • 2021-09-11
  • 2021-08-20
  • 2021-06-22
  • 2022-12-23
  • 2021-07-06
相关资源
相似解决方案