2개의 타이머를 사용하는 예제,
#include <windows.h>
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
HINSTANCE g_hInst;
LPCTSTR lpszClass = TEXT("TwoTimer");
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow )
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst = hInstance;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hbrBackground = (HBRUSH)GetStockObject( COLOR_WINDOW+1 );
WndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
WndClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
WndClass.hInstance = hInstance;
WndClass.lpfnWndProc = WndProc;
WndClass.lpszClassName = lpszClass;
WndClass.lpszMenuName = NULL;
WndClass.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass( &WndClass );
hWnd = CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, (HMENU)NULL, hInstance, NULL );
ShowWindow( hWnd, nCmdShow );
while( GetMessage( &Message, NULL, 0, 0 ) )
{
TranslateMessage( &Message );
DispatchMessage( &Message );
}
return (int)Message.wParam;
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam )
{
HDC hdc;
PAINTSTRUCT ps;
SYSTEMTIME st;
static TCHAR sTime[128];
static RECT rt = {100, 100, 400, 120}; // 특정 영역만 PAINT하기 위한 RECT
switch( iMessage )
{
case WM_CREATE:
SetTimer( hWnd, 1, 1000, NULL ); // 1초 간격 ID = 1
SetTimer( hWnd, 2, 5000, NULL ); // 5초 간격 ID = 2
SendMessage( hWnd, WM_TIMER, 1, 0 );
return 0;
case WM_TIMER:
switch( wParam )// wParam에서 타이머의 ID에 따라서 각각 다른 일을 시킨다.
{
case 1: // 매 초마다 변경된 시간을 출력
GetLocalTime( &st );
wsprintf( sTime, TEXT("지금 시간은 %d:%d:%d입니다."),
st.wHour, st.wMinute, st.wSecond);
InvalidateRect( hWnd, &rt, TRUE );
break;
case 2:
MessageBeep(0); // 5초에 한번씩 띵 소리
break;
}
return 0;
case WM_DESTROY:
KillTimer( hWnd, 1 ); // 타이머가 2개라서, Kill도 2개
KillTimer( hWnd, 2 );
PostQuitMessage(0);
return 0;
case WM_PAINT:
hdc = BeginPaint( hWnd, &ps );
TextOut( hdc, 100, 100, sTime, lstrlen(sTime));
EndPaint( hWnd, &ps );
return 0;
}
return( DefWindowProc( hWnd, iMessage, wParam, lParam));
}
// 실행 화면

매 초마다 시간을 갱신하고, 5초에 한번씩 띵 소리가 나게한다.
'프로그래밍 > API' 카테고리의 다른 글
API-무한의 작업을 위한 타이머(2) (0) | 2010.04.21 |
---|---|
API-무한의 작업을 위한 타이머(1) (0) | 2010.04.21 |
API-타이머 (0) | 2010.04.21 |
API-마우스입력 (0) | 2010.04.21 |
API-Key입력(2) (0) | 2010.04.21 |