1/28/09

Microsoft Visual c++ Timer Example

In This Tutorial microsoft visual c++ Timer example we use left mouse buttons to trigger the motion of a rectangle and kill the motion of a rectangle m_rect_time when left button up










=

note the photos here are protected by copyright rules


DOWNLOADS

The downloadds contain the whole project plus the Exe in the debug folder
To start rect motion presss left mouse button and stop it press right mouse button click


and start again using the left mouse the time was chosen is 300 milli seconds



To start the tutorial



1- Start new single document application and let us name it TimeEX and deselect printing and print proview



2- In your derived view class add the following public data member m_rect_time ;

3- In your view class initilise it as follows :m_rect_time( 20,20,50,50) .

4- Add windows messege handlers for WM_TIMER , WM_RBUTTONDOWN, WM_LBUTTOMDOWN .

5- Edit the OnTimer function as shown in red
// timer action


void CTimerEXView::OnTimer(UINT nIDEvent) {

if(m_rect_time.right> 250 ) m_rect_time.OffsetRect( -250,0) ;



// rectangle motion


m_rect_time.OffsetRect( 50,0) ;

InvalidateRect(NULL,TRUE);





CView::OnTimer(nIDEvent);


}



6-Edit on left button handler as shown here in red

SetTimer(1, 300,NULL) ; // 3 00 millisecond

// trigger the timer by setting the time
3 00 millisecond 1 mearns first timer NULL timers window


7- Edit the on right buttom down as folows



KillTimer(1);

// we have only one timer


8-Edit the ondRaw as follows





pDC->TextOut(70 ,70,"Press the left mouse button to start moving the rectangle using timer and right button to stop it") ;

pDC->SelectStockObject(BLACK_BRUSH );

pDC->Rectangle(m_rect_time) ;



9- Build the application and start stoping the rectangle as what I did here is the figure below






of course you can use bigger font for the text and can change tiime interval by change the value in settimer second paprameter

Press CTrl F5 abd build it


to download the whole project click here
I hope that I guided how to create simple timer tutorial

Hints To practice
if you want multiple timers then is what you have to do is to trigger multiple timer by the function settimer by changing the first and second parameter
for example if you have second timer SetTimer( 2,400, NULL)
second one is set to 400 and add handler to killtimer by killTimer(2) ;
and so on also try different values in the second parameters in settimer ( ) and see the effects


Use callBack functions instead of the WM_TIMER you used and try to control the clock by increasing and decreasing time intervals using menus
for lessons ans extra tutorials see the rest of the blog

for notes and and comments leave me a post below

4 comments:

Kirsten said...

Great for those who want to learn programming.

Vinay Rai said...

I hope it will be useful for wannabe programmers.

Uminya Rama said...

I know nothing about what you're talking about. But, I'm sure that you MUST BE A SMART person. Salut!

Check My blog, kay.

Permanent Tourist said...

Nice post. Do you have any on how to open and read files?

Post a Comment

leave me messege