Just use the SetTimer function after you Create the window , and
just handle the WM_TIMER message inside the message loop.

That's it !

1,934 Views
// timertesting.cpp 
//

#include "stdafx.h"
#include <windows.h>
#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// The title bar text

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);


#define ID_5SECONDS 101

UINT TimmerID = 0;

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// initialize the gobal variables that holds
	// title and the window name
	strcpy( szTitle , "Timer Alert" );
	strcpy( szWindowClass , "Window Class");
		
	// Initialize global strings
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}



	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(NULL , IDI_ASTERISK);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL ;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(NULL , IDI_ERROR);

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }


	// set up our timer
   TimmerID = SetTimer( hWnd , ID_5SECONDS , 5 *1000 , NULL);



   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}


//
// The windows Message Handler
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;

	switch (message) 
	{

		case WM_TIMER:
			MessageBox( NULL , "5 Second Alert !" , "Timer Alert"  , MB_OK);
			break ;

		case WM_DESTROY:
			PostQuitMessage(0);
			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}
About the Author

well I love technology and want to implement new and make the human life easy.ppl say I am kind thanks and I love to hear that.In programming I am not a expert , but love expertise advice , I want to listen and debug and read than code.My #1 programming language is C++. and I love VLSI and FPGA electronic subjects too.Currently I am a 3rd year student of BIT externaland I just completing my degree in informationtechnology. My favourate linux distro is slax. (there is an old story if you know RH you know RH ,if you know slax you know linux) and I love programming with the xlib and still learning it.In this semester we have a compiler theory , AI as subjects. I love those subjects. and Open,and want to think and implement something new. and my idea about the technology is Technologyshould be easily accessible , low cost , and reliable and should make the human life easy.and I living in the bambalapitiya city north colombo sri lanka.where is a best place to learn electronics. :S

can u send me a program code for changing background colour...........

Hi
can I use setTimer in console project ?

You could, but you will have to do a lot of other things too, such as implement a message pump so that Windows messages can be read and processed. Not really worth the effort.

An easier way is to just create another thread, then in an infinite loop put it to sleep for the time you need. When it wakes up have the thread do the processing you want the SetTime() function to do.

For console you can check this one:

http://www.programmingtunes.com/creating-timer-c/