TTimer 쓰다가
API 만으로 타이머를 쓰고 싶을때.
예제는 1초마다 제목줄에 현재 시간을 표시하고
2초마다 띵띵거립니다.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
const int m_Timer1 = 1; // 타이머 구분 ID
const int m_Timer2 = 2;
int CALLBACK TimerProc1() // 1 초마다 시간 표시
{
Form1->Caption = Now();
return 0;
}
int CALLBACK TimerProc2() // 2 초마다 띵...
{
Beep();
return 0;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
SetTimer(Handle, m_Timer1, 1000, TimerProc1); // 1 초 타이머.
SetTimer(Handle, m_Timer2, 2000, TimerProc2); // 2 초 타이머.
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
KillTimer(Handle, m_Timer1);
KillTimer(Handle, m_Timer2);
}
//---------------------------------------------------------------------------
만일 CALLBACK 함수를 TForm1 의 멤버함수에서 처리하고 싶으면
TForm1에서 WM_TIMER 메시지를 잡아서 처리해주면 됩니다.
이때 타이머 ID를 가지고 어떤 타이머 이벤트인지 구분하면 됩니다.
호출은 아래처럼 해야겠죠.
SetTimer(Handle, m_Timer1, 1000, NULL); // 1 초 타이머.
|