본문 바로가기
IT-개발,DB

[개발/MFC] WM_TIMER 예제

by SB리치퍼슨 2012. 5. 2.

[MFC] WM_TIMER 예제


// Message Map
BEGIN_MESSAGE_MAP(CDlgDlg, CDialog)
    ...
    ON_WM_TIMER()
END_MESSAGE_MAP()

...

// Timer ID constants.
const UINT ID_TIMER_ONE  = 0x1001;
const UINT ID_TIMER_TWO = 0x1000;

// Start the timers.
void CDlgDlg::StartTimer()
{
    // Set timer for Minutes.
    SetTimer( ID_TIMER_ONE, 60 * 1000, 0 );

    // Set timer for Seconds.
    SetTimer( ID_TIMER_TWO, 1000, 0 );
}

// Stop the timers.
void CDlgDlg::StopTimer()
{
    // Stop both timers.
    KillTimer( ID_TIMER_ONE  );
    KillTimer( ID_TIMER_TWO );
}

// Timer Handler.
void CDlgDlg::OnTimer( UINT nIDEvent )
{
    // Per minute timer ticked.
    if( nIDEvent == ID_TIMER_ONE )
    {
        // Do your minute based tasks here.
    }

    // Per minute timer ticked.
    if( nIDEvent == ID_TIMER_TWO )
    {
        // Do your seconds based tasks here.
    }
}

 

위의 방법외에도 다른 방법이 있다.

UINT SetTimer(
   UINT nIDEvent,
   UINT 
nElapse,
   void ( CALLBACK* 
lpfnTimer )(HWND, UINT, UINT, DWORD) = NULL 
) throw();

void CALLBACK TimerProc(HWND hWnd, UINT nIDEvent, UINT nElapse, DWORD dwTime)
{
    CWnd *pWnd = AfxGetMainWnd();

    CTestProgDlg *dlg = (CTestProgDlg *)pWnd;

    dlg->ShowWindow(SW_SHOW);
}

반응형

댓글