WM_TIMER 메시지
윈도우는 타이머를 한 번 설정하면 종료하기 전까지 주기적으로 타이머 메시지를 발생 시킨다. 타이머를 시작하는 함수는 SetTimer이고 타이머를 종료하는 함수는 KillTimer이다.
SetTimer() 함수 |
UINT_PTR SetTimer ( HWND hWnd, // 윈도우 핸들 UINT_PTR nIDEvent, // 타이머 ID로 정수값 UINT uElapse, // 시간 간격으로 단위는 밀리초(1000은 1초) TIMERPROC lpTimerFunc // 타이머가 보내는 WM_TIMER 메시지를 받을 함수 이름 ); |
KillTimer() 함수 |
BOOL KillTimer { HWND hWnd, UINT_PTR nIDEvent // 종료할 ID 입력 ); |
오른쪽 방향키를 누르면 자동으로 이동하기
#include <windows.h> #include <TCHAR.H> LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { HWND hwnd; MSG msg; WNDCLASS WndClass; WndClass.style = CS_HREDRAW | CS_VREDRAW; WndClass.lpfnWndProc = WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = hInstance; WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WndClass.lpszMenuName = NULL; WndClass.lpszClassName = _T("Window Class Name"); RegisterClass(&WndClass); hwnd = CreateWindow(_T("Window Class Name"), _T("Window Title Name"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; }
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static int x, y; static RECT rectView;
switch (iMsg) { case WM_CREATE: GetClientRect(hwnd, &rectView); x = 20; y = 20; break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); Ellipse(hdc, x-20, y-20, x+20, y+20); EndPaint(hwnd, &ps); break; case WM_KEYDOWN: if (wParam == VK_RIGHT) break; case WM_TIMER: x += 40; if (x + 20 > rectView.right) InvalidateRgn(hwnd, NULL, TRUE); break; case WM_DESTROY: KillTimer(hwnd, 1); PostQuitMessage(0); break; } return(DefWindowProc(hwnd, iMsg, wParam, lParam)); } |
실행 결과
'프로그램언어 > C++' 카테고리의 다른 글
API 그리기 모드(Draw Mode) (0) | 2020.09.23 |
---|---|
API 마우스 (0) | 2020.09.22 |
API 키도드 입력로 도형제어 (0) | 2020.09.21 |
파일 입출력 (0) | 2020.08.24 |
템플릿 (0) | 2020.08.20 |