Timer wont work

This is a discussion on Timer wont work within the Windows Programming forums, part of the Platform Specific Boards category; Whats wrong: Code: #include <windows.h> #include "Resource.h" #define ID_TIMER 1 void OnInitDialog(HWND hDlg); BOOL CALLBACK APP_DlgProc(HWND, UINT, WPARAM, LPARAM); int ...

  1. #1
    yatta!
    Guest

    Timer wont work

    Whats wrong:
    Code:
    #include <windows.h> 
    #include "Resource.h" 
    
    #define ID_TIMER 1
    
    void OnInitDialog(HWND hDlg);
    
    BOOL CALLBACK APP_DlgProc(HWND, UINT, WPARAM, LPARAM);
    
    int APIENTRY WinMain( 
    					 HINSTANCE hInstance, HINSTANCE hPrevious, LPTSTR lpsz, int iCmd) 
    { 
    	// Run main dialog  
    	BOOL b = DialogBox(hInstance, "DLG_MAIN", NULL, APP_DlgProc); 
    	
    	return b; 
    } 
    
    BOOL CALLBACK APP_DlgProc(HWND hDlg, UINT uiMsg, WPARAM wParam, LPARAM lParam) 
    { 	
    	int timer = 60;
    	char timer_str[10];
    	
    	switch(uiMsg) 
    	{ 
    	case WM_INITDIALOG: 
    		OnInitDialog(hDlg); 
    		
    		break; 
    		
    	case WM_COMMAND: 
    		
    		switch(wParam) 
    		{ 
    		case IDCANCEL: 
    			EndDialog(hDlg, FALSE); 
    			return FALSE;
    			
    		case IDC_START:
    			SetTimer(hDlg, ID_TIMER, 1000, NULL);
    			break;
    			
    		case IDC_STOP:
    			KillTimer(hDlg, ID_TIMER);
    			break;
    			
    		case ID_TIMER:
    			itoa(timer, timer_str, 10);
    			
    			if(timer == 0)
    			{
    				KillTimer(hDlg, ID_TIMER);
    			}
    			else
    			{
    				timer--;
    			}
    			
    			SetDlgItemText(hDlg, IDC_SECONDS, timer_str);
    			
    			break;
    			
    		} 
    		break; 
    	} 
    	
    	return FALSE;
    } 
    
    void OnInitDialog(HWND hDlg) 
    {
    	
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Code:
    BOOL CALLBACK APP_DlgProc(HWND hDlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
    {
    	static int timer = 60;
    	char timer_str[10];
    
    	switch(uiMsg){
    	case WM_INITDIALOG:
    		OnInitDialog(hDlg);
    	break;
    	case WM_CLOSE:
    		EndDialog(hDlg, FALSE);
    	break;
    
    	case WM_COMMAND:
    		switch(LOWORD(wParam)){
    		case IDC_START:
    			SetTimer(hDlg, ID_TIMER, 1000, NULL);
    			break;
    		case IDC_STOP:
    			KillTimer(hDlg, ID_TIMER);
    			break;
    		}
    		break;	
    
    	case WM_TIMER:
    		if(wParam != ID_TIMER) break;
    		itoa(timer, timer_str, 10);
    
    		if(timer == 0){
    			KillTimer(hDlg, ID_TIMER);
    		}
    		else{
    			timer--;
    		}
    		SetDlgItemText(hDlg, IDC_SECONDS, timer_str);
    		break;
    	}
    
    return FALSE;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 12:48 PM
  4. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM
  5. Kitchen Timer sugestions for improvements
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 12-08-2005, 10:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21