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
Creating Dual Timers
1 Create buttons in the toolbar as shown in the figure below
Make like them
Then assign the following ids for the buttons
IDC_GO_LEFT , IDC_BAN_HORIZONTAL_MOVES ,IDC_ACTIVATE_VERTICAL_MOVES AND IDC_FREESE_UPDOWN
2- In the view class add the following member variables
BOOL m_b_halt_leftright,m_Bol_suspended_UpDown_motion
;
CRect m_animated_sqaure ;
Make them public
The Boolean variables are used to enable moving and the square is the animated object
3-Go to the constructor of the class and edit it as follows
CDualtimersView::CDualtimersView():m_animated_sqaure(20,20,50,50)
{
m_b_halt_leftright = TRUE ;
m_Bol_suspended_UpDown_motion = TRUE ;
}
// so at first the square is stopped
Now go on Draw virtual function and edit as follows
void CDualtimersView::OnDraw(CDC* pDC)
{
pDC->TextOut(40,10," Press Toolbars bottons");
pDC->SelectStockObject(BLACK_BRUSH );
pDC->Rectangle(m_animated_sqaure) ;
}
// The ondraw vertual function with dimentions 30 pixels by 30 pixels draw black square
Add windows message handler for the WM_timer and edit as
void CDualtimersView::OnTimer(UINT IDEvent)
{
switch(IDEvent)
{
case 1:
// first timer
if(m_animated_sqaure.right> 250 )
m_animated_sqaure.OffsetRect( -250,0) ;
m_animated_sqaure.OffsetRect( 20,0) ;
InvalidateRect(NULL,TRUE);
break ;
case 2:
// second timer and the action to shift
if(m_animated_sqaure.top <10)
m_animated_sqaure.OffsetRect(0,200);
m_animated_sqaure.OffsetRect( 0,-10) ;
InvalidateRect(NULL,TRUE);
break;
default:
CView::OnTimer(IDEvent);
}
}
Now add windows mesege handler for the Tolbar buttons
We start with the first button
void CDualtimersView::OnGoLeft()
{
// see if the button not pressed
if(m_b_halt_leftright) {
SetTimer( 1,500,NULL) ;
// half a second
m_b_halt_leftright =FALSE ;
}
}
and add Update windows messge handler for the same button
void CDualtimersView::OnUpdateGoLeft(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_b_halt_leftright);
}
Do the same for the whole buttons as you can see here
void CDualtimersView::OnBanHorizontalMoves()
{
if(!m_b_halt_leftright) {
KillTimer(1);
m_b_halt_leftright =TRUE ;
}
}
void CDualtimersView::OnUpdateBanHorizontalMoves(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_b_halt_leftright);
}
void CDualtimersView::OnActivateVerticalStep()
{
if(m_Bol_suspended_UpDown_motion) {
SetTimer( 2,200,NULL) ;
m_Bol_suspended_UpDown_motion =FALSE ;
}
}
void CDualtimersView::OnUpdateActivateVerticalStep(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_Bol_suspended_UpDown_motion);
}
void CDualtimersView::OnFreezeUpdown()
{
if(!m_Bol_suspended_UpDown_motion) {
KillTimer(2);
m_Bol_suspended_UpDown_motion =TRUE ;
}
}
void CDualtimersView::OnUpdateFreezeUpdown(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_Bol_suspended_UpDown_motion);
}
As an activity Add discription to the code i did
Note use better method for naming
and also see the toolbar example for more on toolbars and use menu items to start and stop the motion and add keyboard accelerators
Click here to download both examples Download link
Please leave your feedback below
Great for those who want to learn programming.
ReplyDeleteI hope it will be useful for wannabe programmers.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteNice post. Do you have any on how to open and read files?
ReplyDeletemoncler, moncler outlet, links of london, bottes ugg, moncler, moncler, doudoune canada goose, sac louis vuitton pas cher, juicy couture outlet, canada goose, moncler, pandora charms, pandora charms, replica watches, louis vuitton, pandora jewelry, lancel, canada goose, louis vuitton, ugg boots uk, swarovski, louis vuitton, canada goose uk, wedding dresses, moncler, ugg,uggs,uggs canada, hollister, supra shoes, juicy couture outlet, toms shoes, canada goose outlet, ugg pas cher, montre pas cher, canada goose, moncler, karen millen, swarovski crystal, louis vuitton, pandora jewelry, ugg,ugg australia,ugg italia, marc jacobs, thomas sabo, coach outlet, moncler, canada goose, canada goose outlet
ReplyDeleteoakley sunglasses, polo ralph lauren outlet, tiffany jewelry, nike roshe run, louboutin pas cher, tiffany and co, kate spade outlet, oakley sunglasses, oakley sunglasses, nike free, louboutin outlet, prada outlet, christian louboutin outlet, tory burch outlet, ray ban sunglasses, jordan shoes, louis vuitton, longchamp outlet, ralph lauren pas cher, louis vuitton outlet, ugg boots, longchamp pas cher, ray ban sunglasses, oakley sunglasses, replica watches, polo ralph lauren outlet, uggs on sale, air max, louis vuitton outlet, michael kors, louis vuitton, prada handbags, longchamp, nike free, chanel handbags, sac longchamp, burberry, louis vuitton, nike air max, gucci outlet, nike outlet, louboutin, ugg boots, nike air max, replica watches, air jordan pas cher, ray ban sunglasses, louboutin shoes, longchamp outlet, cheap oakley sunglasses
ReplyDeletejimmy choo shoes, converse outlet, north face outlet, insanity workout, soccer jerseys, wedding dresses, nike air max, ralph lauren, mcm handbags, oakley, hollister, lululemon, mont blanc, valentino shoes, vans shoes, vans, abercrombie and fitch, nike huarache, new balance, hollister, bottega veneta, nike trainers, birkin bag, celine handbags, ghd, soccer shoes, asics running shoes, instyler, timberland boots, beats by dre, nfl jerseys, converse, reebok shoes, nike roshe, hollister, ferragamo shoes, p90x workout, gucci, louboutin, north face outlet, iphone cases, babyliss, chi flat iron, baseball bats, longchamp, ray ban, nike air max, herve leger, mac cosmetics
ReplyDelete