프로그래밍/API 프로그래밍
Timer 응용
CokeBell
2008. 11. 5. 18:05
/*간단한 타이머 응용*/
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
static int nx = 1, ny = 1, nxStop = 1;
static int nxx = 203, nxxStop = 1;
char szCho[30];
RECT rc = { 0, 0, 300, 70 };
switch( msg )
{
case WM_KEYDOWN:
if( LOWORD(wParam ) == VK_RETURN) //Enter_Key 입력시 타이머 생성하여 작동을 보여줌
{
SetTimer(hwnd, 1, 10, NULL);
SetTimer(hwnd, 2, 10, NULL);
}
if( LOWORD(wParam ) == VK_SPACE) //Space_Key 입력시 타이머를 죽여 정지상태를 보여줌
{
KillTimer(hwnd, 1);
KillTimer(hwnd, 2);
}
return 0;
case WM_TIMER:
hdc = GetDC(hwnd);
InvalidateRect(hwnd, &rc, TRUE);
UpdateWindow(hwnd);
switch(wParam)
{
case 1:
if( nxStop == 1 ) nx++; //처음 왼쪽에서 오른쪽으로 한칸씩 옴겨 간다.
if( nx == 203 ) nxStop = 2; //203이 될때 까지 203이 되면 nxStop가 2가 되면서
if( nxStop == 2 ) nx--; //이 식이 성립되어 이제는 1이될때까지 오른쪽에 왼쪽으로 이동
if( nx == 1) nxStop = 1;
break;
case 2:
if( nxxStop == 1 ) nxx--;
if( nxx ==1 ) nxxStop = 2;
if( nxxStop == 2) nxx++;
if( nxx == 203) nxxStop = 1;
break;
}
TextOut(hdc, nx, ny, "텍스트 이동", 11);
sprintf(szCho, "x좌표 : %d, y좌표 : %d", nxx, ny);
TextOut(hdc, nxx, 40, szCho, strlen(szCho));
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
// 1. 윈도우 클래스 만들기
WNDCLASS wc;
wc.cbWndExtra = 0;
wc.cbClsExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc; // DefWindowProc;
wc.lpszClassName = "First";
wc.lpszMenuName = 0;
wc.style = 0;
// 2. 등록(레지스트리에)
RegisterClass(&wc);
// 3. 윈도우 창 만들기
HWND hwnd = CreateWindowEx( 0, // WS_EX_TOPMOST
"first", // 클래스 명
"Hello", // 캡션바 내용
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT , 0, CW_USEDEFAULT, 0, // 초기 위치
0, 0, // 부모 윈도우 핸들, 메뉴 핸들
hInstance, // WinMain의 1번째 파라미터 (exe 주소)
0); // 생성 인자
// 4. 윈도우 보여주기
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
// 5. Message
MSG msg;
while( GetMessage( &msg, 0, 0, 0 ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
static int nx = 1, ny = 1, nxStop = 1;
static int nxx = 203, nxxStop = 1;
char szCho[30];
RECT rc = { 0, 0, 300, 70 };
switch( msg )
{
case WM_KEYDOWN:
if( LOWORD(wParam ) == VK_RETURN) //Enter_Key 입력시 타이머 생성하여 작동을 보여줌
{
SetTimer(hwnd, 1, 10, NULL);
SetTimer(hwnd, 2, 10, NULL);
}
if( LOWORD(wParam ) == VK_SPACE) //Space_Key 입력시 타이머를 죽여 정지상태를 보여줌
{
KillTimer(hwnd, 1);
KillTimer(hwnd, 2);
}
return 0;
case WM_TIMER:
hdc = GetDC(hwnd);
InvalidateRect(hwnd, &rc, TRUE);
UpdateWindow(hwnd);
switch(wParam)
{
case 1:
if( nxStop == 1 ) nx++; //처음 왼쪽에서 오른쪽으로 한칸씩 옴겨 간다.
if( nx == 203 ) nxStop = 2; //203이 될때 까지 203이 되면 nxStop가 2가 되면서
if( nxStop == 2 ) nx--; //이 식이 성립되어 이제는 1이될때까지 오른쪽에 왼쪽으로 이동
if( nx == 1) nxStop = 1;
break;
case 2:
if( nxxStop == 1 ) nxx--;
if( nxx ==1 ) nxxStop = 2;
if( nxxStop == 2) nxx++;
if( nxx == 203) nxxStop = 1;
break;
}
TextOut(hdc, nx, ny, "텍스트 이동", 11);
sprintf(szCho, "x좌표 : %d, y좌표 : %d", nxx, ny);
TextOut(hdc, nxx, 40, szCho, strlen(szCho));
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
// 1. 윈도우 클래스 만들기
WNDCLASS wc;
wc.cbWndExtra = 0;
wc.cbClsExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc; // DefWindowProc;
wc.lpszClassName = "First";
wc.lpszMenuName = 0;
wc.style = 0;
// 2. 등록(레지스트리에)
RegisterClass(&wc);
// 3. 윈도우 창 만들기
HWND hwnd = CreateWindowEx( 0, // WS_EX_TOPMOST
"first", // 클래스 명
"Hello", // 캡션바 내용
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT , 0, CW_USEDEFAULT, 0, // 초기 위치
0, 0, // 부모 윈도우 핸들, 메뉴 핸들
hInstance, // WinMain의 1번째 파라미터 (exe 주소)
0); // 생성 인자
// 4. 윈도우 보여주기
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
// 5. Message
MSG msg;
while( GetMessage( &msg, 0, 0, 0 ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}