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
visual c++ samples and examples blog contains tutorials on Toolbar ,statusbar, timers ,hover buttons,inflaterect, intersectrect,tooltips, dll , These applications and tutorials contains codes working programs plus explanation in steps doing these application The lessons include code ,descriptions plus images to illustrate the steps to create the application and download Each one of the modules fully explained functions to help you how to make the project
1/26/09
Microsoft visual c++ toolbar example
Microsoft visual c++ toolbar example use toolbar buttons to move movable rectangle m_rect_movable in four directions up, down , left and right and when intersected with a fixed rectangle m_rect_fixed it draws new rectangle with red - hatched pattern and also it the movable rectangle become blue hatched when it approaches the border rectangle m_rectBorder .
This program is similar to a previous program that used menu items to control the position of a movable rectangleThere is 2 things to check the first one is to check the intersection between the movable rectangle and the fixed rectangle so that draw a hatched pattern in the intersection rectangle I could not use IntersectRect function because it takes LPCRECT as parameters and the other thing to check was if the movable rectangle is close to the border rectangle ,you can GetClientRect function to get the window's client area instead of the border rectangle
To build the application
1- Chose new and build single doc application and name it like TOOLBAR_MOVE_EX
2- In the resource view go to toolbar and delete some of the default buttoms and add 4 buttoms as shown in the fig below and assign the following IDs to them ID_SHIFT_LEFT ,ID_SHIFT_RIGHT ,ID_MOVE_UP and ID_MOVE_DOWN sorry for the unpretty buttoms !!
3-Add the following data members to your derived CView class ( 3 rectangles and 5 boolean variables)
CRect m_rectBorder , m_rect_movable, m_rect_fixed ; // border ,movable and fixed rectangles respectively
BOOL m_bLeftBorder ,m_bRightBorder,m_bTopBorder ,m_bButtomBorder ,m_bIntersect;
// border and intersection indicators
4- In the view class initilize your data members as follows
CTOOLBAR_MOVE_EXView::CTOOLBAR_MOVE_EXView():m_rectBorder(10 ,10,340,340),m_rect_fixed(100,100,200,200) ,m_rect_movable(40,30,90,130)
{
m_bLeftBorder =m_bRightBorder =m_bTopBorder =m_bButtomBorder =m_bIntersect==FALSE ;
}
5- In your view add windows messege handlers for command and update command as follows
void CTOOLBAR_MOVE_EXView::OnShiftLeft()
{
if(m_rect_movable.left <=(m_rectBorder.left +40)) m_bLeftBorder = TRUE ; m_rect_movable.OffsetRect(-10,0); m_bRightBorder = FALSE ; if (m_rect_fixed.PtInRect(m_rect_movable.TopLeft()) | m_rect_fixed.PtInRect(m_rect_movable.BottomRight())| m_rect_fixed.PtInRect(CPoint(m_rect_movable.right,m_rect_movable.top))|m_rect_fixed.PtInRect(CPoint( m_rect_movable.left,m_rect_movable.bottom))) { m_bIntersect =TRUE ; } InvalidateRect(NULL,TRUE); } void CTOOLBAR_MOVE_EXView::OnUpdateShiftLeft(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bLeftBorder);
}
void CTOOLBAR_MOVE_EXView::OnShiftRight()
{
if(m_rect_movable.right >= ( m_rectBorder.right -30 ))
m_bRightBorder = TRUE ;
m_rect_movable.OffsetRect(10,0);
m_bLeftBorder = FALSE ;
if (m_rect_fixed.PtInRect(m_rect_movable.TopLeft()) | m_rect_fixed.PtInRect(m_rect_movable.BottomRight())| m_rect_fixed.PtInRect(CPoint(m_rect_movable.right,m_rect_movable.top))|m_rect_fixed.PtInRect(CPoint( m_rect_movable.left,m_rect_movable.bottom))) {
m_bIntersect =TRUE ;
}
InvalidateRect(NULL,TRUE);
}
void CTOOLBAR_MOVE_EXView::OnUpdateShiftRight(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_bRightBorder);
}
void CTOOLBAR_MOVE_EXView::OnMoveDown()
{
if( m_rect_movable.bottom >= ( m_rectBorder.bottom -20))
m_bButtomBorder = TRUE ;
m_rect_movable.OffsetRect(0,10) ;
m_bTopBorder = FALSE ;
if (m_rect_fixed.PtInRect(m_rect_movable.TopLeft()) | m_rect_fixed.PtInRect(m_rect_movable.BottomRight())| m_rect_fixed.PtInRect(CPoint(m_rect_movable.right,m_rect_movable.top))|m_rect_fixed.PtInRect(CPoint( m_rect_movable.left,m_rect_movable.bottom))) {
m_bIntersect =TRUE ;
}
InvalidateRect(NULL,TRUE);
}
void CTOOLBAR_MOVE_EXView::OnUpdateMoveDown(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!m_bButtomBorder);
}
void CTOOLBAR_MOVE_EXView::OnMoveUp()
{
if(m_rect_movable.top <= (m_rectBorder.top + 20)) m_bTopBorder =TRUE ; m_rect_movable.OffsetRect(0,-10) ; m_bButtomBorder = FALSE ; if (m_rect_fixed.PtInRect(m_rect_movable.TopLeft()) | m_rect_fixed.PtInRect(m_rect_movable.BottomRight())| m_rect_fixed.PtInRect(CPoint(m_rect_movable.right,m_rect_movable.top))|m_rect_fixed.PtInRect(CPoint( m_rect_movable.left,m_rect_movable.bottom))) { m_bIntersect =TRUE ; } InvalidateRect(NULL,TRUE); } void CTOOLBAR_MOVE_EXView::OnUpdateMoveUp(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bTopBorder);
}
6- Edit the OnDraw member function of your generated view
pDC->Rectangle(m_rectBorder) ; // white hallow
pDC->SelectStockObject(LTGRAY_BRUSH); // outer rectangle
pDC->Rectangle(m_rect_fixed); // fixed rectangle
CBrush bluebrush(HS_DIAGCROSS, RGB(0, 0, 250)); // blue brush for border condition when moveable rect on border
pDC->SelectStockObject(BLACK_BRUSH);
if(m_bLeftBorder |m_bRightBorder |m_bTopBorder |m_bButtomBorder)
pDC->SelectObject(bluebrush) ; // if in nearby the border rectangle
pDC->Rectangle(m_rect_movable);
if(m_bIntersect) {
CRect rc_intersection ;// use red hatch pattern
rc_intersection.IntersectRect(m_rect_movable ,m_rect_fixed);
CBrush brushHatch(HS_DIAGCROSS, RGB(255, 90, 0));
pDC->SelectObject(brushHatch) ;
pDC->Rectangle(rc_intersection);
}
7- try to move the movable rectangle around when you start the application you will start with a window like this
1/25/09
c++ conditional return example
In this c++ condition return example we made a function the accept both positive and negative numbers and produce the output of the square rout
if the number is negative the output would the square rout of absolute value otherwise the output will the square rout
at first the the program check the number if it is positive or negative
This is simple c++ conditional return example that uses if statement to check the value of the number entered
as seen in the red code below
#include
#include
using namespace std ;
double sqr( double x) {
if( x<0)
return sqrt(-x) ;
return sqrt(x) ;
}
int main() {
cout<< "Enter the number you to get square rout positive or negative "<<>
double x , result ;
cin>> x;
result = sqr (x);
cout<<"The resuly is square rout of the number you entered " <<>
return 0 ;
}
if the number is negative the output would the square rout of absolute value otherwise the output will the square rout
at first the the program check the number if it is positive or negative
This is simple c++ conditional return example that uses if statement to check the value of the number entered
as seen in the red code below
#include
#include
using namespace std ;
double sqr( double x) {
if( x<0)
return sqrt(-x) ;
return sqrt(x) ;
}
int main() {
cout<< "Enter the number you to get square rout positive or negative "<<>
double x , result ;
cin>> x;
result = sqr (x);
cout<<"The resuly is square rout of the number you entered " <<>
return 0 ;
}
1/23/09
Visual c++ Menu Example
download
In this Visual c++ menu example we Top level Move with 4 pop_up items Up ,Down ,Left and Right to move a rectangle rcRect inside the borders of a bigger rectangle rcBorder .
working example
To create the program
1- Start new Single document MFC exe application and deselect printing and print view options
2-In the resource View to IDR_MAINFRAME menu and make add the following items as shown in Fig 1
Downloadshttp://www.4shared.com/file/88832447/4d0b1fea/Menu_Move_Rect.html
3- In your derived view class add the following member functions and make them public
BOOL m_bLeft,m_bRight ,m_bDOWN , m_bUp ; // these are the left right top bottom borders indicators
CRect rcRect ,rcBorder ; // small rectangle and the bigger rectangle rcBorder
4-In your derived view class initilise the date as shown
CMove_RectView::CMove_RectView():rcRect(100,100,50,50),rcBorder( 20 ,20 ,300,300)
{
m_bLeft =m_bRight = m_bDOWN =m_bUp =FALSE ;
}
5-Add messege handlers to your menuitem IDs
void CMove_RectView::OnMoveUp() { rcRect.OffsetRect(0 ,-20); m_bDOWN =FALSE ; if((rcRect.top -70) <= rcBorder.top) m_bUp =TRUE ; InvalidateRect(NULL ,TRUE ); } void CMove_RectView::OnUpdateMoveUp(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bUp); } void CMove_RectView::OnMoveDown() { rcRect.OffsetRect(0, 20); m_bUp =FALSE ; if((rcRect.bottom) >= (rcBorder.bottom -70) ) m_bDOWN =TRUE ; InvalidateRect(NULL ,TRUE ); } void CMove_RectView::OnUpdateMoveDown(CCmdUI* pCmdUI) { pCmdUI->Enable(! m_bDOWN) ; } void CMove_RectView::OnMoveLeft() { if( (rcRect.left) <=rcBorder.left + 70) m_bLeft = TRUE ; rcRect.OffsetRect(-10 , 0); m_bRight = FALSE ; InvalidateRect(NULL ,TRUE ); } void CMove_RectView::OnUpdateMoveLeft(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bLeft); } void CMove_RectView::OnMoveRight() { rcRect.OffsetRect(20 ,0); m_bLeft = FALSE ; InvalidateRect(NULL ,TRUE ); if((rcBorder.right)<= rcRect.right) m_bRight=TRUE ; } void CMove_RectView::OnUpdateMoveRight(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bRight); }
6- Finally edit the OnDraw function in your view class as follows
void CMove_RectView::OnDraw(CDC* pDC)
{
pDC->Rectangle(rcBorder) ;
if( m_bLeft |m_bRight |m_bDOWN |m_bUp)
pDC->SelectStockObject(DKGRAY_BRUSH ) ;
// if the small rect near the big rect
pDC->Rectangle(rcRect);
}
7- Build the application and see how it works
8- Try to move the small Rect with the menu options UP DOWN LEFT AND RIGHT And when move near the borders of the big rectangle you get something like in the fig 2
to download the whole project clich here
and the exe file click here
things to add are keyboad accelerators to do
In this Visual c++ menu example we Top level Move with 4 pop_up items Up ,Down ,Left and Right to move a rectangle rcRect inside the borders of a bigger rectangle rcBorder .
working example
To create the program
1- Start new Single document MFC exe application and deselect printing and print view options
2-In the resource View to IDR_MAINFRAME menu and make add the following items as shown in Fig 1
Downloadshttp://www.4shared.com/file/88832447/4d0b1fea/Menu_Move_Rect.html
3- In your derived view class add the following member functions and make them public
BOOL m_bLeft,m_bRight ,m_bDOWN , m_bUp ; // these are the left right top bottom borders indicators
CRect rcRect ,rcBorder ; // small rectangle and the bigger rectangle rcBorder
4-In your derived view class initilise the date as shown
CMove_RectView::CMove_RectView():rcRect(100,100,50,50),rcBorder( 20 ,20 ,300,300)
{
m_bLeft =m_bRight = m_bDOWN =m_bUp =FALSE ;
}
5-Add messege handlers to your menuitem IDs
void CMove_RectView::OnMoveUp() { rcRect.OffsetRect(0 ,-20); m_bDOWN =FALSE ; if((rcRect.top -70) <= rcBorder.top) m_bUp =TRUE ; InvalidateRect(NULL ,TRUE ); } void CMove_RectView::OnUpdateMoveUp(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bUp); } void CMove_RectView::OnMoveDown() { rcRect.OffsetRect(0, 20); m_bUp =FALSE ; if((rcRect.bottom) >= (rcBorder.bottom -70) ) m_bDOWN =TRUE ; InvalidateRect(NULL ,TRUE ); } void CMove_RectView::OnUpdateMoveDown(CCmdUI* pCmdUI) { pCmdUI->Enable(! m_bDOWN) ; } void CMove_RectView::OnMoveLeft() { if( (rcRect.left) <=rcBorder.left + 70) m_bLeft = TRUE ; rcRect.OffsetRect(-10 , 0); m_bRight = FALSE ; InvalidateRect(NULL ,TRUE ); } void CMove_RectView::OnUpdateMoveLeft(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bLeft); } void CMove_RectView::OnMoveRight() { rcRect.OffsetRect(20 ,0); m_bLeft = FALSE ; InvalidateRect(NULL ,TRUE ); if((rcBorder.right)<= rcRect.right) m_bRight=TRUE ; } void CMove_RectView::OnUpdateMoveRight(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bRight); }
6- Finally edit the OnDraw function in your view class as follows
void CMove_RectView::OnDraw(CDC* pDC)
{
pDC->Rectangle(rcBorder) ;
if( m_bLeft |m_bRight |m_bDOWN |m_bUp)
pDC->SelectStockObject(DKGRAY_BRUSH ) ;
// if the small rect near the big rect
pDC->Rectangle(rcRect);
}
7- Build the application and see how it works
8- Try to move the small Rect with the menu options UP DOWN LEFT AND RIGHT And when move near the borders of the big rectangle you get something like in the fig 2
to download the whole project clich here
and the exe file click here
things to add are keyboad accelerators to do
1/18/09
C++ overloaded function example
C++ overloaded function example in this tuturial we used the c++ property of overloading to use single function for different purposes square rout , addition , multiplication and devision in one single operation
we include cmath library in order to use math functions
The code below marked in red demonstrate how made this sample program
#include
#include
using namespace std ;
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a){
return sqrt( a);}
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a , double b){
return a *b ;}
double SQUAREROOT_MULTIPLY_SUM_DEVISION (double a ,double b, double c){
return a + b + c ;}
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a ,double b, double c , double d){
return a /( b +c +d) ; }
C++ overloaded function example In this tutorial we used the property of overloading to use single function for 4 different purposes Square rout addition multiplication and division
int main (){
cout << " this program used single function to perform square rout , multiplication , addition and devisition in single operation " <<>
double w = SQUAREROOT_MULTIPLY_SUM_DEVISION(4);
cout<< " square root of 4 ="<<>
double x = SQUAREROOT_MULTIPLY_SUM_DEVISION( 12 ,14);
cout << " the sum of 13 and 14 is "<<>
double y = SQUAREROOT_MULTIPLY_SUM_DEVISION( 2 ,3,4);
cout <<" 2 *3*4 = "<<>
double z = SQUAREROOT_MULTIPLY_SUM_DEVISION ( 100, 2,3,5);
cout << " 100 / ( 2 + 3 +5) "<<>
return 0 ;
}
we include cmath library in order to use math functions
The code below marked in red demonstrate how made this sample program
#include
#include
using namespace std ;
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a){
return sqrt( a);}
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a , double b){
return a *b ;}
double SQUAREROOT_MULTIPLY_SUM_DEVISION (double a ,double b, double c){
return a + b + c ;}
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a ,double b, double c , double d){
return a /( b +c +d) ; }
C++ overloaded function example In this tutorial we used the property of overloading to use single function for 4 different purposes Square rout addition multiplication and division
int main (){
cout << " this program used single function to perform square rout , multiplication , addition and devisition in single operation " <<>
double w = SQUAREROOT_MULTIPLY_SUM_DEVISION(4);
cout<< " square root of 4 ="<<>
double x = SQUAREROOT_MULTIPLY_SUM_DEVISION( 12 ,14);
cout << " the sum of 13 and 14 is "<<>
double y = SQUAREROOT_MULTIPLY_SUM_DEVISION( 2 ,3,4);
cout <<" 2 *3*4 = "<<>
double z = SQUAREROOT_MULTIPLY_SUM_DEVISION ( 100, 2,3,5);
cout << " 100 / ( 2 + 3 +5) "<<>
return 0 ;
}
C++ overloaded function example
C++ overloaded function example in this tuturial we used the c++ property of overloading to use single function for different purposes square rout , addition , multiplication and devision in one single operation
we include cmath library in order to use math functions
The code below marked in red demonstrate how made this sample program
#include
#include
using namespace std ;
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a){
return sqrt( a);}
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a , double b){
return a *b ;}
double SQUAREROOT_MULTIPLY_SUM_DEVISION (double a ,double b, double c){
return a + b + c ;}
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a ,double b, double c , double d){
return a /( b +c +d) ; }
C++ overloaded function example In this tutorial we used the property of overloading to use single function for 4 different purposes Square rout addition multiplication and division
int main (){
cout << " this program used single function to perform square rout , multiplication , addition and devisition in single operation " <<>
double w = SQUAREROOT_MULTIPLY_SUM_DEVISION(4);
cout<< " square root of 4 ="<<>
double x = SQUAREROOT_MULTIPLY_SUM_DEVISION( 12 ,14);
cout << " the sum of 13 and 14 is "<<>
double y = SQUAREROOT_MULTIPLY_SUM_DEVISION( 2 ,3,4);
cout <<" 2 *3*4 = "<<>
double z = SQUAREROOT_MULTIPLY_SUM_DEVISION ( 100, 2,3,5);
cout << " 100 / ( 2 + 3 +5) "<<>
return 0 ;
}
we include cmath library in order to use math functions
The code below marked in red demonstrate how made this sample program
#include
#include
using namespace std ;
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a){
return sqrt( a);}
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a , double b){
return a *b ;}
double SQUAREROOT_MULTIPLY_SUM_DEVISION (double a ,double b, double c){
return a + b + c ;}
double SQUAREROOT_MULTIPLY_SUM_DEVISION( double a ,double b, double c , double d){
return a /( b +c +d) ; }
C++ overloaded function example In this tutorial we used the property of overloading to use single function for 4 different purposes Square rout addition multiplication and division
int main (){
cout << " this program used single function to perform square rout , multiplication , addition and devisition in single operation " <<>
double w = SQUAREROOT_MULTIPLY_SUM_DEVISION(4);
cout<< " square root of 4 ="<<>
double x = SQUAREROOT_MULTIPLY_SUM_DEVISION( 12 ,14);
cout << " the sum of 13 and 14 is "<<>
double y = SQUAREROOT_MULTIPLY_SUM_DEVISION( 2 ,3,4);
cout <<" 2 *3*4 = "<<>
double z = SQUAREROOT_MULTIPLY_SUM_DEVISION ( 100, 2,3,5);
cout << " 100 / ( 2 + 3 +5) "<<>
return 0 ;
}
Labels:
c++,
example,
function,
library,
overloaded
1/17/09
MFC visual c++ CstringArray Example
In this MFC visual c++ CstringArray example we used CstringArray class in order to display time on status bar bans we used it to display seconds , minutes and hours
1 - Build MFC application single document and chose not to select printing and printing preview
2 - Add resource element in the View menu chose resource simbols and New and name it ID_TIMEARRAY
3-Add handlers in the CmainFrame for ID_VIEW_STATUS_BAR and update handler for it,
4-Replace the previously created indicators array with 3 ID_SEPARATOR to hold second minutes and hours respectively and make m_wndStatusBar data member public instead of private .
5- Replace if (!m_wndStatusBar.Create(this) with this, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM, ID_TIMEARRAY) ;so as not to use the default status bar .
6-
CTime currenttime = GetCurrentTime();
int sec , min , hours ;
hours = currenttime.GetHour();
min = currenttime.GetMinute();
sec = currenttime.GetSecond();
CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus = &pFrame->m_wndStatusBar;
CStringArray timearray ;
timearray.SetSize(3);
CString strsec ,strmin ,strhours ;
strsec.Format("%d ",sec);
strmin.Format("%d " , min);
strhours.Format("%d " ,hours );
timearray[0] = strsec ;
timearray[1] = strmin ;
timearray[2] = strhours ;
pStatus->SetPaneText(0, timearray[0]);
pStatus->SetPaneText(1 ,timearray[1] );
pStatus->SetPaneText(2 ,timearray[2]);
to download the whole project clock here
to download the Exe file click here
1 - Build MFC application single document and chose not to select printing and printing preview
2 - Add resource element in the View menu chose resource simbols and New and name it ID_TIMEARRAY
3-Add handlers in the CmainFrame for ID_VIEW_STATUS_BAR and update handler for it,
4-Replace the previously created indicators array with 3 ID_SEPARATOR to hold second minutes and hours respectively and make m_wndStatusBar data member public instead of private .
5- Replace if (!m_wndStatusBar.Create(this) with this, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM, ID_TIMEARRAY) ;so as not to use the default status bar .
6-
void CMainFrame::OnViewStatusBar()8- ADd the following code to OnLbuttonDown handler in your view class
{
m_wndStatusBar.ShowWindow((m_wndStatusBar.GetStyle() &
WS_VISIBLE) == 0);
RecalcLayout();
}
void CMainFrame::OnUpdateViewStatusBar(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck((m_wndStatusBar.GetStyle() & WS_VISIBLE) != 0);
} in mainframe.cpp
7- Add the following function OnDraw to your View class pDC->TextOut(30, 30 ,Press the left key to see your computer current time on the status bar ");
and add #include "MainFrm.h" near the top of your view class
CTime currenttime = GetCurrentTime();
int sec , min , hours ;
hours = currenttime.GetHour();
min = currenttime.GetMinute();
sec = currenttime.GetSecond();
CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus = &pFrame->m_wndStatusBar;
CStringArray timearray ;
timearray.SetSize(3);
CString strsec ,strmin ,strhours ;
strsec.Format("%d ",sec);
strmin.Format("%d " , min);
strhours.Format("%d " ,hours );
timearray[0] = strsec ;
timearray[1] = strmin ;
timearray[2] = strhours ;
pStatus->SetPaneText(0, timearray[0]);
pStatus->SetPaneText(1 ,timearray[1] );
pStatus->SetPaneText(2 ,timearray[2]);
to download the whole project clock here
to download the Exe file click here
visual c++ Status bar example
1- Make single document MFC application and deselect printing and printview options
2- In the view menu chose resource and then add New resource and then add ID_TIME_STATUSBAR and accept the defaul value (101) .
3- Add message handler and update message handler in the CMainFrame class for both ID_VIEW_STATUS_BAR .
4- Replace the previously created indicators array with single ID_SEPARATOR and make sure that the CMainFrame data member m_wndStatusBar is public instead of protected so that it can be accessed from the View class .
5- Replace if (!m_wndStatusBar.Create(this) with this, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM, ID_TIME_STATUSBAR) ;
6- Add the following code to ur CMainFrame.cpp
8- Edit OnDraw messege handler as follows
CTime currenttime = GetCurrentTime();
int sec , min , hours ;
hours = currenttime.GetHour();
min = currenttime.GetMinute();
sec = currenttime.GetSecond();
CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus = &pFrame->m_wndStatusBar;
CString strtime ;
strtime.Format( "%d %d %d ", hours,min , sec );
pStatus->SetPaneText(0, strtime );
Finally include #include "MainFrm.h" near the top with other include statements
to download the exe file click here
and the whole project here
this contains sum errors
2- In the view menu chose resource and then add New resource and then add ID_TIME_STATUSBAR and accept the defaul value (101) .
3- Add message handler and update message handler in the CMainFrame class for both ID_VIEW_STATUS_BAR .
4- Replace the previously created indicators array with single ID_SEPARATOR and make sure that the CMainFrame data member m_wndStatusBar is public instead of protected so that it can be accessed from the View class .
5- Replace if (!m_wndStatusBar.Create(this) with this, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM, ID_TIME_STATUSBAR) ;
6- Add the following code to ur CMainFrame.cpp
void CMainFrame::OnViewStatusBar()7- Add the following function OnDraw to your View class pDC->TextOut(30, 30 ,Press the left key to see your computer current time on the status bar ");
{
m_wndStatusBar.ShowWindow((m_wndStatusBar.GetStyle() &
WS_VISIBLE) == 0);
RecalcLayout();
}
void CMainFrame::OnUpdateViewStatusBar(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck((m_wndStatusBar.GetStyle() & WS_VISIBLE) != 0);
}
8- Edit OnDraw messege handler as follows
CTime currenttime = GetCurrentTime();
int sec , min , hours ;
hours = currenttime.GetHour();
min = currenttime.GetMinute();
sec = currenttime.GetSecond();
CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus = &pFrame->m_wndStatusBar;
CString strtime ;
strtime.Format( "%d %d %d ", hours,min , sec );
pStatus->SetPaneText(0, strtime );
Finally include #include "MainFrm.h" near the top with other include statements
to download the exe file click here
and the whole project here
this contains sum errors
Subscribe to:
Posts (Atom)