vb如何在自定义控件添加循环事件
想在自己做的趋势图控件上精确显示时间,用Timer精度太低,想用个循环调用QueryPerformanceFrequency来做,这个代码应该写在哪个事件里面呢,或者能否...
想在自己做的趋势图控件上精确显示时间,用Timer精度太低,想用个循环调用QueryPerformanceFrequency来做,这个代码应该写在哪个事件里面呢,或者能否自定义能循环执行的事件?
展开
1个回答
展开全部
可以啊,用api添加计时器
Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
在窗口的load事件里添加代码,
SetTimer Me.hWnd, 0, 100, AddressOf TimerProc ‘timerproc就是你要执行的过程,100是每100毫秒执行一次。
至于你说的时间精度不准,应该是用api取出毫秒级的时间,用now当然不准了。
Private Type uSystemTime
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As uSystemTime)
'*********************************************************************************************
'名称 Fun_GetLocalTime
'作者 MornWoo20071016
'功能 获取系统的时间,精确到毫秒。
'参数 无
'返回 返回字符串数据: iFormat = 0,"YYYY-MM-DD HH:MM:SS"+" " + "毫秒数";iFormat=1,yyyymmddhhmmss毫秒数
' iFormat = 2,"YYYY-MM-DD HH:MM:SS"
'*********************************************************************************************
Function Fun_GetLocalTime(Optional ByVal iFormat = 0) As String
Dim uSysTime As uSystemTime
Dim sA
GetSystemTime uSysTime
If CInt(uSysTime.wHour + 8) < 24 Then
Select Case iFormat
Case 0
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTime.wHour + 8), "00") & ":" & Format(CStr(uSysTime.wMinute), "00") & ":" & _
Format(CStr(uSysTime.wSecond), "00") & "." & CStr(uSysTime.wMilliseconds)
Case 1
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyymmdd") & _
Format(CStr(uSysTime.wHour + 8), "00") & Format(CStr(uSysTime.wMinute), "00") & _
Format(CStr(uSysTime.wSecond), "00") & CStr(uSysTime.wMilliseconds)
Case 2
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTime.wHour + 8), "00") & ":" & Format(CStr(uSysTime.wMinute), "00") & ":" & _
Format(CStr(uSysTime.wSecond), "00")
End Select
Else
Select Case iFormat
Case 0
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTime.wMinute), "00") & _
":" & Format(CStr(uSysTime.wSecond), "00") & "." & CStr(uSysTime.wMilliseconds)
Case 1
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyymmdd") & _
Format(CStr(uSysTime.wMinute), "00") & _
Format(CStr(uSysTime.wSecond), "00") & CStr(uSysTime.wMilliseconds)
Case 2
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTime.wMinute), "00") & _
":" & Format(CStr(uSysTime.wSecond), "00")
End Select
End If
Fun_GetLocalTime = sA
End Function
Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
在窗口的load事件里添加代码,
SetTimer Me.hWnd, 0, 100, AddressOf TimerProc ‘timerproc就是你要执行的过程,100是每100毫秒执行一次。
至于你说的时间精度不准,应该是用api取出毫秒级的时间,用now当然不准了。
Private Type uSystemTime
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As uSystemTime)
'*********************************************************************************************
'名称 Fun_GetLocalTime
'作者 MornWoo20071016
'功能 获取系统的时间,精确到毫秒。
'参数 无
'返回 返回字符串数据: iFormat = 0,"YYYY-MM-DD HH:MM:SS"+" " + "毫秒数";iFormat=1,yyyymmddhhmmss毫秒数
' iFormat = 2,"YYYY-MM-DD HH:MM:SS"
'*********************************************************************************************
Function Fun_GetLocalTime(Optional ByVal iFormat = 0) As String
Dim uSysTime As uSystemTime
Dim sA
GetSystemTime uSysTime
If CInt(uSysTime.wHour + 8) < 24 Then
Select Case iFormat
Case 0
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTime.wHour + 8), "00") & ":" & Format(CStr(uSysTime.wMinute), "00") & ":" & _
Format(CStr(uSysTime.wSecond), "00") & "." & CStr(uSysTime.wMilliseconds)
Case 1
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyymmdd") & _
Format(CStr(uSysTime.wHour + 8), "00") & Format(CStr(uSysTime.wMinute), "00") & _
Format(CStr(uSysTime.wSecond), "00") & CStr(uSysTime.wMilliseconds)
Case 2
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTime.wHour + 8), "00") & ":" & Format(CStr(uSysTime.wMinute), "00") & ":" & _
Format(CStr(uSysTime.wSecond), "00")
End Select
Else
Select Case iFormat
Case 0
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTime.wMinute), "00") & _
":" & Format(CStr(uSysTime.wSecond), "00") & "." & CStr(uSysTime.wMilliseconds)
Case 1
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyymmdd") & _
Format(CStr(uSysTime.wMinute), "00") & _
Format(CStr(uSysTime.wSecond), "00") & CStr(uSysTime.wMilliseconds)
Case 2
sA = Format(CStr(uSysTime.wYear) & "-" & CStr(uSysTime.wMonth) & "-" & CStr(uSysTime.wDay), "yyyy-mm-dd") & _
" " & Format(CStr(uSysTime.wMinute), "00") & _
":" & Format(CStr(uSysTime.wSecond), "00")
End Select
End If
Fun_GetLocalTime = sA
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询