/*
WM_TIMER
SetTimer / KillTimer에 대해서 알아 보자.
*/
#include <windows.h>
void CALLBACK foo( HWND hwnd, // window Handle
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer id
DWORD dwTime) // current system time
{
MessageBox(hwnd, "foo", "foo", MB_OK);
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
// 윈도우가 생성될 때 발생 => 초기화 코드
case WM_CREATE:
// SetTimer(hwnd, 1,1000, foo); // 1000을 주기로 foo함수 호출->메세지 박스 1초단위로 호출
SetTimer(hwnd, 1, 1000, 0); //1000은 1초 , 주기로 WM_TIMER을 실행->WM_PAINT
return 0;
case WM_TIMER:
// timer ID값 확인..
if( wParam == 1)
{
InvalidateRect(hwnd, 0, TRUE); // 화면 무효화 1초 다위로 WM_PAINT실행
}
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
SYSTEMTIME st; GetLocalTime(&st); // 현재 시간 구하기
char buf[256];
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, 0, buf, 256);
RECT r;
GetClientRect(hwnd, &r); //시간의 뿌려줄 박스 생성
DrawText(hdc, buf, -1, &r, DT_SINGLELINE | DT_CENTER | DT_VCENTER);//화면에 시간 뿌려주기,
EndPaint(hwnd, &ps);
}
return 0;
case WM_DESTROY:
KillTimer(hwnd, 1); //종료시 settimer죽임.
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;
}
WM_TIMER
SetTimer / KillTimer에 대해서 알아 보자.
*/
#include <windows.h>
void CALLBACK foo( HWND hwnd, // window Handle
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer id
DWORD dwTime) // current system time
{
MessageBox(hwnd, "foo", "foo", MB_OK);
}
LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
// 윈도우가 생성될 때 발생 => 초기화 코드
case WM_CREATE:
// SetTimer(hwnd, 1,1000, foo); // 1000을 주기로 foo함수 호출->메세지 박스 1초단위로 호출
SetTimer(hwnd, 1, 1000, 0); //1000은 1초 , 주기로 WM_TIMER을 실행->WM_PAINT
return 0;
case WM_TIMER:
// timer ID값 확인..
if( wParam == 1)
{
InvalidateRect(hwnd, 0, TRUE); // 화면 무효화 1초 다위로 WM_PAINT실행
}
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
SYSTEMTIME st; GetLocalTime(&st); // 현재 시간 구하기
char buf[256];
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, 0, buf, 256);
RECT r;
GetClientRect(hwnd, &r); //시간의 뿌려줄 박스 생성
DrawText(hdc, buf, -1, &r, DT_SINGLELINE | DT_CENTER | DT_VCENTER);//화면에 시간 뿌려주기,
EndPaint(hwnd, &ps);
}
return 0;
case WM_DESTROY:
KillTimer(hwnd, 1); //종료시 settimer죽임.
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;
}