1 / 15

Windows Programming 제대로 배우기

Windows Programming 제대로 배우기. Chapter 4. Timer Event. 학습 목표. 타이머에 대한 이벤트를 컨트롤 하는 방법에 대해서 설명. 타이머. 윈도우에서는 일정시간을 설정하고 그 시간이 되면 타이머 메시지를 발생하여 이 타이머를 이용하여 여러 멀티 테스킹 및 일정시간에 반복되는 프로그래밍할수 있도록 되어 있음 타이머 메시지란 타이머란 일정시간이 경과 되면 알려주는 시계의 기능을 함

stian
Télécharger la présentation

Windows Programming 제대로 배우기

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Windows Programming제대로 배우기 Chapter 4. Timer Event

  2. 학습 목표 타이머에 대한 이벤트를 컨트롤 하는 방법에 대해서 설명

  3. 타이머 • 윈도우에서는 일정시간을 설정하고 그 시간이 되면 타이머 메시지를 발생하여 이 타이머를 이용하여 여러 멀티 테스킹 및 일정시간에 반복되는 프로그래밍할수 있도록 되어 있음 • 타이머 메시지란 • 타이머란 일정시간이 경과 되면 알려주는 시계의 기능을 함 • 예를 들어 "1분이 지나면 나에게 알려다오" 라고 했을 경우 타이머는 1분이 지난후에 "1분이 되었습니다" 라고 알려줌 • 윈도우에서 타이머는 타이머를 종료하기 이전까지 계속적으로 알려줌 • "1분이 지나면 나에게 알려다오"라고 설정하면 설정한 후에 1분이 경과되면 타이머 메시지를 발생하고 또 다시 1분이 경과 되면 타이머 메시지를 발생하고... 즉 특정 시간을 타이머로 설정하였다면 설정한후부터 그시간 간격으로 타이머 메시지를 발생 • 타이머를 설정하는 함수는 SetTimer

  4. Set 타이머 UINT SetTimer( HWND hWnd, // 윈도우 핸들 UINT nIDEvent, // 타이머 번호 UINT uElapse, // 설정된 시간 간격 TIMERPROC lpTimerFunc // 타이머 메시지가 발생되었을 때 실행되는 함수 ); (예) SetTimer(hwnd,1,5000,NULL);//5초짜리 1번 타이머 설정 SetTimer(hwnd,2,1000,NULL);//1초짜리 2번 타이머 설정

  5. Kill 타이머 타이머는 반드시 KillTimer로 destroy시켜주어야 한다. UINT KillTimer( HWND hWnd, // 윈도우 핸들 UINT nIDEvent, // 타이머 번호 );

  6. 타이머 구현 예 • 적당한 시간 뒤(예 10초 후) 프로그램이 종료되는 프로그램 구현

  7. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { char temp[80]; PAINTSTRUCT ps; HDC hdc; static int m_Count; switch(uMsg) { case WM_CREATE : m_Count = 10; SetTimer(hWnd, 1, 1000, NULL); break; case WM_PAINT : hdc = BeginPaint(hWnd, &ps); sprintf(temp, "Count Down: %2d", m_Count); TextOut(hdc, 300, 250, temp, strlen(temp)); EndPaint(hWnd, &ps); break; case WM_TIMER : m_Count--; InvalidateRect(hWnd, NULL, TRUE); if(m_Count == 0) DestroyWindow(hWnd); break; case WM_DESTROY : KillTimer(hWnd, 1); PostQuitMessage(0); break; } return DefWindowProc(hWnd, uMsg, wParam, lParam); }

  8. 2개 이상의 타이머 • 2개 이상의 타이머일때는 타이머 ID를 식별하여 사용 • SetTimer(hwnd,1,5000,NULL);//5초짜리 1번 타이머 설정 • SetTimer(hwnd,2,1000,NULL);//1초짜리 2번 타이머 설정 case WM_TIMER: if(wParam==1) { //1번 타이머로부터 메시지가 발생되었을 경우 } else if(wParam==2) { //2번 타이머로부터 메시지가 발생되었을 경우 } else if(wParam==3) { //3번 타이머로부터 메시지가 발생되었을 경우 } : break;

  9. 타이머 프로시저 예제 Extimer는 두 개의 타이머를 가동 1번 타이머는 0.5초마다 2번 타이머는 2초마다 가동 WM_PAINT메시지에서는 램덤 수를 이용하여 임의적인 색을 만들고 이색으로 박스를 그리고 또한 원을 그리도록 하였음 Timer.cpp

  10. 타이머 프로시저 SetTimer(hwnd,1,200,TimeProc); 위와 같이 한다면 0.2초후에는 TimerProc의 함수가 수행 이함수는 다음과 같은 형태로 만들어야 함 VOID CALLBACK TimeProc(HWND hwnd,UINT iMsg,UINT wParam,DWORD lParam) { //0.2초 후에 발생되는 내용을 이곳에 기록 }

  11. 프로시저를 이용한 타이머 메시지 예제 화면 (0,0)좌표에 현재 시간을 출력하면서 2초마다 사각형을 임의적인 색으로 바꾸어 출력하는 예제 현재 시간을 출력하는 부분은 프로시저 함수를 이용 VOID CALLBACK TimeProc(HWND hwnd,UINT iMsg,UINT wParam,DWORD lParam); SetTimer함수를 가동 //0.1초마다 TimerProc가 가동되도록 타이머 설정 SetTimer(hwnd,CLOCKTIMER,100,(TIMERPROC)TimeProc);

  12. 현재 시간을 출력하는 TimerProc는 다음과 같이 제작 //현재 시간을 화면에 출력하는 프로시저 VOID CALLBACK TimeProc(HWND hwnd,UINT iMsg,UINT wParam,DWORD lParam) { time_t curtime; struct tm cur; char curdate[80]; HDC hdc ; time(&curtime);//시스템 시간을 얻는다. cur=*localtime(&curtime);//시스템 시간을 로컬 타임존으로 바꾼다. wsprintf(curdate,"%02d-%02d- %02d:%02d:%02d:%02d",cur.tm_year,cur.tm_mon+1, cur.tm_mday,cur.tm_hour,cur.tm_min,cur.tm_sec); hdc=GetDC(hwnd); TextOut(hdc,0,0,curdate,strlen(curdate)); ReleaseDC(hwnd,hdc); } • time_t는 타이머 형 • time함수에 이 타이머 형의 포인터를 넘겨주면 시스템 시간을 얻을수 있음 • 이 타이머 형은 long형 • 이 형으로부터 연월일 시분의 형태를 tm구조체로 전환 (이때 사용하는 함수가 localtime)

  13. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { HDC hdc ; PAINTSTRUCT ps ; HBRUSH hBrush,oldBrush; HPEN hPen,oldPen; BYTE r,g,b; HRGN rgn; switch (iMsg) { case WM_CREATE : //0.1초마다 TimerProc가 가동되도록 타이머 설정 SetTimer(hwnd,CLOCKTIMER,100,(TIMERPROC)TimeProc); //WM_TIMER메시지에 의한 내용이 수행되도록 설정 SetTimer(hwnd,2,2000,NULL); return 0 ; Timer2.cpp

  14. case WM_TIMER: rgn=CreateRectRgn(20,20,300,300); InvalidateRgn(hwnd,rgn,FALSE); DeleteObject(rgn); break; case WM_DESTROY : KillTimer(hwnd,CLOCKTIMER); KillTimer(hwnd,2); PostQuitMessage (0) ; return 0 ; } return DefWindowProc (hwnd, iMsg, wParam, lParam) ; } case WM_PAINT : hdc = BeginPaint (hwnd, &ps) ; r=rand()%256; g=rand()%256; b=rand()%256; hBrush=CreateSolidBrush(RGB(r,g,b)); hPen=CreatePen(PS_SOLID,1,RGB(r,g,b)); oldBrush=(HBRUSH)SelectObject(hdc,hBrush); oldPen=(HPEN)SelectObject(hdc,hPen); Rectangle(hdc,20,20,300,300); SelectObject(hdc,oldBrush); SelectObject(hdc,oldPen); DeleteObject(hBrush); DeleteObject(hPen); EndPaint (hwnd, &ps) ; return 0 ;

  15. //현재 시간을 화면에 출력하는 프로시저 VOID CALLBACK TimeProc(HWND hwnd,UINT iMsg,UINT wParam,DWORD lParam) { time_t curtime; struct tm cur; char curdate[80]; HDC hdc ; time(&curtime);//시스템 시간을 얻는다. cur=*localtime(&curtime);//시스템 시간을 로컬 타임존으로 바꾼다. wsprintf(curdate,"%02d-%02d-%02d:%02d:%02d:%02d", cur.tm_year,cur.tm_mon+1, cur.tm_mday,cur.tm_hour,cur.tm_min,cur.tm_sec); hdc=GetDC(hwnd); TextOut(hdc,0,0,curdate,strlen(curdate)); ReleaseDC(hwnd,hdc); }

More Related