CAFE

vb6 api

SetTimer/KillTimer/타이머 api

작성자올란도|작성시간09.02.12|조회수1,473 목록 댓글 0

API Declarations 
Code: 

Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, _

 ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long 
Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long 



Code: 

Public Timer as long 
Timer = SetTimer(Form.hWnd, 0, 85, AddressOf TimerProc) 

'In a module  
Public Sub TimerProc(ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) 

'Whatever you want the timer to do! <-니 하고픈거... 이말임 

End Sub 

'In your form_unload event  
Call KillTimer(Form.hWnd, TimerProc) 



'-------------------- 
1. 모듈에 다음과 같은 API 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
Public Declare Function KillTimer Lib "user32" _
              (ByVal hwnd As Long, _
               ByVal nIDEvent As Long) As Long

Public iCounter As Integer

Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, _
                     ByVal idEvent As Long, ByVal dwTime As Long)
    iCounter = iCounter + 1
    Form1.Text1.Text = CStr(iCounter)
End Sub

2. 폼에 하나의 Command Button과 TextBox 를 추가하여 다음과 같이 코딩을 하세요

Dim lngTimerID As Long
Dim BlnTimer As Boolean

Private Sub Form_Load()
    BlnTimer = False
    Command1.Caption = "Timer 시작"
End Sub
         
Private Sub Command1_Click()
    If BlnTimer = False Then
        ' 1000 msec = 1 초 입니다. 
        lngTimerID = SetTimer(0, 0, 1000, AddressOf TimerProc)
        If lngTimerID = 0 Then
            MsgBox "Timer 를 발생시키지 못했습니다. 프로그램을 종료합니다."
            Exit Sub
        End If
        BlnTimer = True
        Command1.Caption = "Timer 종료"
    Else
        lngTimerID = KillTimer(0, lngTimerID)
        If lngTimerID = 0 Then
            MsgBox "Can not kill Timer"
        End If
        BlnTimer = False
        Command1.Caption = "Timer 시작"
    End If
End Sub

출처 : Tong - whoman님의 비주얼베이직통

'-------------------- 

위와는 약간 다르지만 핸들값을 요구하지 않습니다. 옵션이 많구요 ㅋㅋ



Public Const TIME_ONESHOT = 0 'Event occurs once, after uDelay milliseconds.  
Public Const TIME_PERIODIC = 1 'Event occurs every uDelay milliseconds.  
Public Const TIME_CALLBACK_EVENT_PULSE = &H20 'When the timer expires, Windows calls thePulseEvent function 

to pulse the event pointed to by the lpTimeProc parameter. The dwUser parameter is ignored.  
Public Const TIME_CALLBACK_EVENT_SET = &H10 'When the timer expires, Windows calls theSetEvent function to 

set the event pointed to by the lpTimeProc parameter. The dwUser parameter is ignored.  
Public Const TIME_CALLBACK_FUNCTION = &H0 'When the timer expires, Windows calls the function pointed to 

by the lpTimeProc parameter. This is the default.  



Public Declare Function timeSetEvent Lib "winmm.dll" Alias "timeSetEvent" (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" Alias "timeKillEvent" (ByVal uID As Long) As Long


마찬가지로 



aTimer = timeSetEvent(시간, 정밀도, Addressof 타이머프로시저, 0=필요없는듯;;, TIME_PERIODIC Or TIME_CALLBACK_FUNCTION)



식으로 선언하시면 됩니다. 약간 더 선택권이 늘어났죠?

역시 이것도 서브정의를 합니다.



Sub TimerProc(ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long)



그리고 언로드시 timeKillEvent aTimer를 불러주고요.







프로그래밍 소스 창고 http://cafe.daum.net/0pds  게임소스, 비주얼 베이직, 비주얼 씨
맨땅에 해딩 하는 프로그래머가 없기를 희망합니다. 여러 프로그래밍 소스를 모아 놓아.....비주얼베이직소스, 비주얼씨소스,Visual Basic Source, Visual C++ Source 실시간 차트 소스 ... 공학용 계산기 소스 ... 상당한 양의 소스 코드가 공개되어 있고 체계적으로 정리가... Visucal Basic.Net 







프로그래밍 소스 창고 http://cafe.daum.net/0pds  게임소스, 비주얼 베이직, 비주얼 씨
맨땅에 해딩 하는 프로그래머가 없기를 희망합니다. 여러 프로그래밍 소스를 모아 놓아.....비주얼베이직소스, 비주얼씨소스,Visual Basic Source, Visual C++ Source 실시간 차트 소스 ... 공학용 계산기 소스 ... 상당한 양의 소스 코드가 공개되어 있고 체계적으로 정리가... Visucal Basic.Net 


다음검색
현재 게시글 추가 기능 열기

댓글

댓글 리스트
맨위로

카페 검색

카페 검색어 입력폼