Home

Visual C++ MFC Examples: Time Sheet

Introduction

A time sheet is an object that allows employees to enter the time they have worked for a day, a week or two weeks. Although it is usually done on paper, you can create an application or dialog box that would make this task easier to both the employees and the employers. In this exercise, we will create such a dialog-based application.

To create our time sheet, we will use the Time Picker of the Date Time Picker control. This convenient control make it possible for a user to select the time instead of typing it. This reduces the likelihood of mistakes.

Practical LearningPractical Learning: Using the Time Picker

  1. Using the MFC Application wizard, create a new Dialog-Based application named TimeSheet11
  2. Set the Dialog Title to Employees Time Sheet
     
  3. Click Finish
  4. Design the dialog box as follows:
     
    Control ID Caption Other Properties
    GroupBox   Employee Identification  
    Static Text      
    Edit Control IDC_EMPLNAME     
    GroupBox   Shifts  
    Static Text      
    Static Text   Time In  
    Static Text   Time Out  
    Static Text   Hours  
    Static Text   Monday:  
    Date Time Picker IDC_MONDAYIN   Format: Time
    Date Time Picker IDC_MONDAYOUT   Format: Time
    Edit Control IDC_MONDAY   Align Text: Right
    Static Text   Tuesday:  
    Date Time Picker IDC_TUESDAYIN     
    Date Time Picker IDC_TUESDAYOUT     
    Edit Control IDC_TUESDAY    Align Text: Right
    Static Text   Wednesday:  
    Date Time Picker IDC_WEDNESDAYIN   Format: Time
    Date Time Picker IDC_WEDNESDAYOUT   Format: Time
    Edit Control IDC_WEDNESDAY   Align Text: Right
    Static Text   Thursday:  
    Date Time Picker IDC_THURSDAYIN   Format: Time
    Date Time Picker IDC_THURSDAYOUT    Format: Time
    Edit Control IDC_THURSDAY    Align Text: Right
    Static Text   Friday:  
    Date Time Picker IDC_FRIDAYIN   Format: Time
    Date Time Picker IDC_FRIDAYOUT   Format: Time
    Edit Control IDC_FRIDAY   Align Text: Right
    Static Text   Saturday:  
    Date Time Picker IDC_SATURDAYIN   Format: Time
    Date Time Picker IDC_SATURDAYOUT   Format: Time
    Edit Control IDC_SATURDAY   Align Text: Right
    Static Text   Sunday:  
    Date Time Picker IDC_SUNDAYIN   Format: Time
    Date Time Picker IDC_SUNDAYOUT   Format: Time
    Edit Control IDC_SUNDAY   Align Text: Right
    Static Text   Total Hours:  
    Edit Control IDC_TOTALHOURS    Align Text: Right
  5. Right-click each control on the dialog and click Add Variable
  6. Create the variables as follows:
     
    ID Control Variable Value Variable
    IDC_EMPLNAME   m_EmployeeName
    IDC_MONDAYIN m_MondayIn  
    IDC_MONDAYOUT m_MondayOut  
    IDC_MONDAY   m_Monday
    IDC_TUEDAYIN m_TuesdayIn  
    IDC_TUESDAYOUT m_TuesdayOut  
    IDC_TUESDAY   m_Tuesday
    IDC_WEDNESDAYIN m_WednesdayIn  
    IDC_WEDNESDAYOUT m_WednesdayOut  
    IDC_WEDNESDAY   m_Wednesday
    IDC_THURSDAYIN m_ThursdayIn  
    IDC_THURSDAYOUT m_ThursdayOut  
    IDC_THURSDAY   m_Thursday
    IDC_FRIDAYIN m_FridayIn  
    IDC_FRIDAYOUT m_FridayOut  
    IDC_FRIDAY   m_Friday
    IDC_SATURDAYIN m_SaturdayIn  
    IDC_SATURDAYOUT m_SaturdayOut  
    IDC_SATURDAY   m_Saturday
    IDC_SUNDAYIN m_SundayIn  
    IDC_SUNDAYOUT m_SundayOut  
    IDC_SUNDAY   m_Sunday
    IDC_TOTALHOURS   m_TotalHours 
  7. Access the OnInitDialog() event of the CtimeSheet1Dlg class and change it as follows:
      
    BOOL CTimeSheet1Dlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	// Set the icon for this dialog.  The framework does this automatically
    	//  when the application's main window is not a dialog
    	SetIcon(m_hIcon, TRUE);			// Set big icon
    	SetIcon(m_hIcon, FALSE);		// Set small icon
    
    	// TODO: Add extra initialization here
    	CTime TimeToSet = CTime::GetCurrentTime();
    
    	this->m_MondayIn.SetTime(&TimeToSet);
    	this->m_MondayOut.SetTime(&TimeToSet);
    	this->m_TuesdayIn.SetTime(&TimeToSet);
    	this->m_TuesdayOut.SetTime(&TimeToSet);
    	this->m_WednesdayIn.SetTime(&TimeToSet);
    	this->m_WednesdayOut.SetTime(&TimeToSet);
    	this->m_ThursdayIn.SetTime(&TimeToSet);
    	this->m_ThursdayOut.SetTime(&TimeToSet);
    	this->m_FridayIn.SetTime(&TimeToSet);
    	this->m_FridayOut.SetTime(&TimeToSet);
    	this->m_SaturdayIn.SetTime(&TimeToSet);
    	this->m_SaturdayOut.SetTime(&TimeToSet);
    	this->m_SundayIn.SetTime(&TimeToSet);
    	this->m_SundayOut.SetTime(&TimeToSet);
    
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
  8. To create a method that can calculate the total hours, in Class View, expand TimeSheet1 and CTimeSheet1Dlg. Right-click CTimeSheet1Dlg -> Add -> Add Functionů
  9. Set the Return Type to void
    Set the Function Name to CalculateWeeklyHours
    Set the Access level to private
     
  10. Click Finish
  11. Implement the function as follows:
     
    void CTimeSheet1Dlg::CalculateWeeklyHours(void)
    {
    	double monday, tuesday, wednesday, thursday,
    		   friday, saturday, sunday, totalHours;
    
    	monday     = atof(this->m_Monday);
    	tuesday    = atof(this->m_Tuesday);
    	wednesday  = atof(this->m_Wednesday);
    	thursday   = atof(this->m_Thursday);
    	friday     = atof(this->m_Friday);
    	saturday   = atof(this->m_Saturday);
    	sunday     = atof(this->m_Sunday);
    
    	totalHours = monday + tuesday + wednesday + thursday +
    		         friday + saturday + sunday;
    	this->m_TotalHours.Format("%.2f", totalHours);
    }
  12. Display the dialog box. On the dialog box, double-click the left Time Picker control of Monday just under the Time In label
  13. Implement the function as follows:
     
    void CTimeSheet1Dlg::OnDtnDatetimechangeMondayin(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    
    	UpdateData();
    
    	CTime timeIn, timeOut;
    
    	this->m_MondayIn.GetTime(timeIn);
    	this->m_MondayOut.GetTime(timeOut);
    
    	// Make sure the time in doesn't occur AFTER the time out
    	 if( timeIn > timeOut )
    		 this->m_MondayIn.SetTime(&timeOut);
    
    	 // Calculate the time difference between both ends of the shift
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 // Calculate the number of hours in the elapsed time
    	 nbrHours = minutes / 60;
    
    	 // Convert the time difference to a decimal value
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 
    	 // Format the number to display appropriately
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 // Display the result in the corresponding text mox
    	 this->m_Monday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
  14. Display the dialog box. Double-click the right Date Time Picker control of Monday just under the Time Out label
  15. Return to the dialog box and double-click each of the Date Time Pickers from left to right and from top to bottom
  16. Implement the events as follows:
     
    // TimeSheet1Dlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "TimeSheet1.h"
    #include "TimeSheet1Dlg.h"
    #include ".\timesheet1dlg.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    // CTimeSheet1Dlg dialog
    
    
    
    CTimeSheet1Dlg::CTimeSheet1Dlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CTimeSheet1Dlg::IDD, pParent)
    	, m_EmployeeName(_T(""))
    	, m_Monday(_T("0.00"))
    	, m_Tuesday(_T("0.00"))
    	, m_Wednesday(_T("0.00"))
    	, m_Thursday(_T("0.00"))
    	, m_Friday(_T("0.00"))
    	, m_Saturday(_T("0.00"))
    	, m_Sunday(_T("0.00"))
    	, m_TotalHours(_T("0.00"))
    {
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    void CTimeSheet1Dlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	DDX_Text(pDX, IDC_EMPLNAME, m_EmployeeName);
    	DDX_Control(pDX, IDC_MONDAYIN, m_MondayIn);
    	DDX_Control(pDX, IDC_MONDAYOUT, m_MondayOut);
    	DDX_Text(pDX, IDC_MONDAY, m_Monday);
    	DDX_Control(pDX, IDC_TUESDAYIN, m_TuesdayIn);
    	DDX_Control(pDX, IDC_TUESDAYOUT, m_TuesdayOut);
    	DDX_Text(pDX, IDC_TUESDAY, m_Tuesday);
    	DDX_Control(pDX, IDC_WEDNESDAYIN, m_WednesdayIn);
    	DDX_Control(pDX, IDC_WEDNESDAYOUT, m_WednesdayOut);
    	DDX_Text(pDX, IDC_WEDNESDAY, m_Wednesday);
    	DDX_Control(pDX, IDC_THURSDAYIN, m_ThursdayIn);
    	DDX_Control(pDX, IDC_THURSDAYOUT, m_ThursdayOut);
    	DDX_Text(pDX, IDC_THURSDAY, m_Thursday);
    	DDX_Control(pDX, IDC_FRIDAYIN, m_FridayIn);
    	DDX_Control(pDX, IDC_FRIDAYOUT, m_FridayOut);
    	DDX_Text(pDX, IDC_FRIDAY, m_Friday);
    	DDX_Control(pDX, IDC_SATURDAYIN, m_SaturdayIn);
    	DDX_Control(pDX, IDC_SATURDAYOUT, m_SaturdayOut);
    	DDX_Text(pDX, IDC_SATURDAY, m_Saturday);
    	DDX_Control(pDX, IDC_SUNDAYIN, m_SundayIn);
    	DDX_Control(pDX, IDC_SUNDAYOUT, m_SundayOut);
    	DDX_Text(pDX, IDC_SUNDAY, m_Sunday);
    	DDX_Text(pDX, IDC_TOTALHOURS, m_TotalHours);
    }
    
    BEGIN_MESSAGE_MAP(CTimeSheet1Dlg, CDialog)
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	//}}AFX_MSG_MAP
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_MONDAYIN, OnDtnDatetimechangeMondayin)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_MONDAYOUT, OnDtnDatetimechangeMondayout)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_TUESDAYIN, OnDtnDatetimechangeTuesdayin)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_TUESDAYOUT, OnDtnDatetimechangeTuesdayout)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_WEDNESDAYIN, OnDtnDatetimechangeWednesdayin)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_WEDNESDAYOUT, OnDtnDatetimechangeWednesdayout)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_THURSDAYIN, OnDtnDatetimechangeThursdayin)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_THURSDAYOUT, OnDtnDatetimechangeThursdayout)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_FRIDAYIN, OnDtnDatetimechangeFridayin)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_FRIDAYOUT, OnDtnDatetimechangeFridayout)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_SATURDAYIN, OnDtnDatetimechangeSaturdayin)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_SATURDAYOUT, OnDtnDatetimechangeSaturdayout)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_SUNDAYIN, OnDtnDatetimechangeSundayin)
    	ON_NOTIFY(DTN_DATETIMECHANGE, IDC_SUNDAYOUT, OnDtnDatetimechangeSundayout)
    END_MESSAGE_MAP()
    
    
    // CTimeSheet1Dlg message handlers
    
    BOOL CTimeSheet1Dlg::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    
    	// Set the icon for this dialog.  The framework does this automatically
    	//  when the application's main window is not a dialog
    	SetIcon(m_hIcon, TRUE);			// Set big icon
    	SetIcon(m_hIcon, FALSE);		// Set small icon
    
    	// TODO: Add extra initialization here
    	CTime TimeToSet = CTime::GetCurrentTime();
    
    	this->m_MondayIn.SetTime(&TimeToSet);
    	this->m_MondayOut.SetTime(&TimeToSet);
    	this->m_TuesdayIn.SetTime(&TimeToSet);
    	this->m_TuesdayOut.SetTime(&TimeToSet);
    	this->m_WednesdayIn.SetTime(&TimeToSet);
    	this->m_WednesdayOut.SetTime(&TimeToSet);
    	this->m_ThursdayIn.SetTime(&TimeToSet);
    	this->m_ThursdayOut.SetTime(&TimeToSet);
    	this->m_FridayIn.SetTime(&TimeToSet);
    	this->m_FridayOut.SetTime(&TimeToSet);
    	this->m_SaturdayIn.SetTime(&TimeToSet);
    	this->m_SaturdayOut.SetTime(&TimeToSet);
    	this->m_SundayIn.SetTime(&TimeToSet);
    	this->m_SundayOut.SetTime(&TimeToSet);
    
    	return TRUE;  // return TRUE  unless you set the focus to a control
    }
    
    // If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.
    
    void CTimeSheet1Dlg::OnPaint() 
    {
    	if (IsIconic())
    	{
    		CPaintDC dc(this); // device context for painting
    
    		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
    
    		// Center icon in client rectangle
    		int cxIcon = GetSystemMetrics(SM_CXICON);
    		int cyIcon = GetSystemMetrics(SM_CYICON);
    		CRect rect;
    		GetClientRect(&rect);
    		int x = (rect.Width() - cxIcon + 1) / 2;
    		int y = (rect.Height() - cyIcon + 1) / 2;
    
    		// Draw the icon
    		dc.DrawIcon(x, y, m_hIcon);
    	}
    	else
    	{
    		CDialog::OnPaint();
    	}
    }
    
    // The system calls this function to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CTimeSheet1Dlg::OnQueryDragIcon()
    {
    	return static_cast<HCURSOR>(m_hIcon);
    }
    
    // This function gathers the time value of each day and calculates the total time for the week
    void CTimeSheet1Dlg::CalculateWeeklyHours(void)
    {
    	double monday, tuesday, wednesday, thursday,
    		   friday, saturday, sunday, totalHours;
    
    	monday     = atof(this->m_Monday);
    	tuesday    = atof(this->m_Tuesday);
    	wednesday  = atof(this->m_Wednesday);
    	thursday   = atof(this->m_Thursday);
    	friday     = atof(this->m_Friday);
    	saturday   = atof(this->m_Saturday);
    	sunday     = atof(this->m_Sunday);
    
    	totalHours = monday + tuesday + wednesday + thursday +
    		         friday + saturday + sunday;
    	this->m_TotalHours.Format("%.2f", totalHours);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeMondayin(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    
    	UpdateData();
    
    	CTime timeIn, timeOut;
    
    	this->m_MondayIn.GetTime(timeIn);
    	this->m_MondayOut.GetTime(timeOut);
    
    	// Make sure the time in doesn't occur AFTER the time out
    	 if( timeIn > timeOut )
    		 this->m_MondayIn.SetTime(&timeOut);
    
    	 // Calculate the time difference between both ends of the shift
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 // Calculate the number of hours in the elapsed time
    	 nbrHours = minutes / 60;
    
    	 // Convert the time difference to a decimal value
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 
    	 // Format the number to display appropriately
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 // Display the result in the corresponding text mox
    	 this->m_Monday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeMondayout(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    	
    	CTime timeIn, timeOut;
    
    	this->m_MondayIn.GetTime(timeIn);
    	this->m_MondayOut.GetTime(timeOut);
    
    	 if( timeOut < timeIn )
    		 this->m_MondayOut.SetTime(&timeIn);
    		 
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 this->m_Monday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeTuesdayin(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    
    	UpdateData();
    
    	CTime timeIn, timeOut;
    
    	this->m_TuesdayIn.GetTime(timeIn);
    	this->m_TuesdayOut.GetTime(timeOut);
    
    	// Make sure the time in doesn't occur AFTER the time out
    	 if( timeIn > timeOut )
    		 this->m_TuesdayIn.SetTime(&timeOut);
    
    	 // Calculate the time difference between both ends of the shift
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 // Calculate the number of hours in the elapsed time
    	 nbrHours = minutes / 60;
    
    	 // Convert the time difference to a decimal value
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 
    	 // Format the number to display appropriately
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 // Display the result in the corresponding text mox
    	 this->m_Tuesday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeTuesdayout(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    	
    	CTime timeIn, timeOut;
    
    	this->m_TuesdayIn.GetTime(timeIn);
    	this->m_TuesdayOut.GetTime(timeOut);
    
    	 if( timeOut < timeIn )
    		 this->m_TuesdayOut.SetTime(&timeIn);
    		 
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 this->m_Tuesday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeWednesdayin(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    
    	UpdateData();
    
    	CTime timeIn, timeOut;
    
    	this->m_WednesdayIn.GetTime(timeIn);
    	this->m_WednesdayOut.GetTime(timeOut);
    
    	// Make sure the time in doesn't occur AFTER the time out
    	 if( timeIn > timeOut )
    		 this->m_WednesdayIn.SetTime(&timeOut);
    
    	 // Calculate the time difference between both ends of the shift
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 // Calculate the number of hours in the elapsed time
    	 nbrHours = minutes / 60;
    
    	 // Convert the time difference to a decimal value
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 
    	 // Format the number to display appropriately
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 // Display the result in the corresponding text mox
    	 this->m_Wednesday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeWednesdayout(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    	
    	CTime timeIn, timeOut;
    
    	this->m_WednesdayIn.GetTime(timeIn);
    	this->m_WednesdayOut.GetTime(timeOut);
    
    	 if( timeOut < timeIn )
    		 this->m_WednesdayOut.SetTime(&timeIn);
    		 
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 this->m_Wednesday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeThursdayin(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    
    	UpdateData();
    
    	CTime timeIn, timeOut;
    
    	this->m_ThursdayIn.GetTime(timeIn);
    	this->m_ThursdayOut.GetTime(timeOut);
    
    	// Make sure the time in doesn't occur AFTER the time out
    	 if( timeIn > timeOut )
    		 this->m_ThursdayIn.SetTime(&timeOut);
    
    	 // Calculate the time difference between both ends of the shift
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 // Calculate the number of hours in the elapsed time
    	 nbrHours = minutes / 60;
    
    	 // Convert the time difference to a decimal value
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 
    	 // Format the number to display appropriately
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 // Display the result in the corresponding text mox
    	 this->m_Thursday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeThursdayout(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    	
    	CTime timeIn, timeOut;
    
    	this->m_ThursdayIn.GetTime(timeIn);
    	this->m_ThursdayOut.GetTime(timeOut);
    
    	 if( timeOut < timeIn )
    		 this->m_ThursdayOut.SetTime(&timeIn);
    		 
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 this->m_Thursday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeFridayin(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    
    	UpdateData();
    
    	CTime timeIn, timeOut;
    
    	this->m_FridayIn.GetTime(timeIn);
    	this->m_FridayOut.GetTime(timeOut);
    
    	// Make sure the time in doesn't occur AFTER the time out
    	 if( timeIn > timeOut )
    		 this->m_FridayIn.SetTime(&timeOut);
    
    	 // Calculate the time difference between both ends of the shift
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 // Calculate the number of hours in the elapsed time
    	 nbrHours = minutes / 60;
    
    	 // Convert the time difference to a decimal value
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 
    	 // Format the number to display appropriately
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 // Display the result in the corresponding text mox
    	 this->m_Friday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeFridayout(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    	
    	CTime timeIn, timeOut;
    
    	this->m_FridayIn.GetTime(timeIn);
    	this->m_FridayOut.GetTime(timeOut);
    
    	 if( timeOut < timeIn )
    		 this->m_FridayOut.SetTime(&timeIn);
    		 
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 this->m_Friday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeSaturdayin(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    
    	UpdateData();
    
    	CTime timeIn, timeOut;
    
    	this->m_SaturdayIn.GetTime(timeIn);
    	this->m_SaturdayOut.GetTime(timeOut);
    
    	// Make sure the time in doesn't occur AFTER the time out
    	 if( timeIn > timeOut )
    		 this->m_SaturdayIn.SetTime(&timeOut);
    
    	 // Calculate the time difference between both ends of the shift
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 // Calculate the number of hours in the elapsed time
    	 nbrHours = minutes / 60;
    
    	 // Convert the time difference to a decimal value
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 
    	 // Format the number to display appropriately
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 // Display the result in the corresponding text mox
    	 this->m_Saturday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeSaturdayout(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    	
    	CTime timeIn, timeOut;
    
    	this->m_MondayIn.GetTime(timeIn);
    	this->m_MondayOut.GetTime(timeOut);
    
    	 if( timeOut < timeIn )
    		 this->m_MondayOut.SetTime(&timeIn);
    		 
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 nbrHours = minutes / 60;
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 this->m_Saturday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeSundayin(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    
    	UpdateData();
    
    	CTime timeIn, timeOut;
    
    	this->m_SundayIn.GetTime(timeIn);
    	this->m_SundayOut.GetTime(timeOut);
    
    	// Make sure the time in doesn't occur AFTER the time out
    	 if( timeIn > timeOut )
    		 this->m_SundayIn.SetTime(&timeOut);
    
    	 // Calculate the time difference between both ends of the shift
    	 CTimeSpan diff = timeOut - timeIn;
    			 
    	 int nbrHours;
    	 double nbrMinutes;
    	 CString strTotalTime;
    
    	 // Calculate the number of minutes in the elapsed time
    	 int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	 // Calculate the number of hours in the elapsed time
    	 nbrHours = minutes / 60;
    
    	 // Convert the time difference to a decimal value
    	 nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    	 
    	 // Format the number to display appropriately
    	 char strHours[6];
    	 itoa(nbrHours, strHours, 10);
    	 int dec, sign;
    	 CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	 strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	 // Display the result in the corresponding text mox
    	 this->m_Sunday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
    
    void CTimeSheet1Dlg::OnDtnDatetimechangeSundayout(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
    	// TODO: Add your control notification handler code here
    	*pResult = 0;
    	
    	CTime timeIn, timeOut;
    
    	this->m_SundayIn.GetTime(timeIn);
    	this->m_SundayOut.GetTime(timeOut);
    
    	if( timeOut < timeIn )
    		this->m_SundayOut.SetTime(&timeIn);
    
    	CTimeSpan diff = timeOut - timeIn;
    
    	int nbrHours;
    	double nbrMinutes;
    	CString strTotalTime;
    
    	int minutes = diff.GetMinutes() + (diff.GetHours() * 60);
    	nbrHours = minutes / 60;
    	nbrMinutes = (minutes - (nbrHours * 60)) * 5 / 3;
    
    	char strHours[6];
    	itoa(nbrHours, strHours, 10);
    	int dec, sign;
    	CString strMinutes = fcvt(nbrMinutes, 2, &dec, &sign);
    
    	strTotalTime = CString(strHours) + CString(".") + CString(strMinutes);
    
    	this->m_Sunday = strTotalTime;
    	CalculateWeeklyHours();
    
    	UpdateData(FALSE);
    }
  17. Execute the application to test it
 

Home Copyright © 2004-2009 FunctionX, Inc.