MFC Sample Applications: Wake Me Up

 

Introduction

Many of us use a small radio to wake us up in the morning. before going to bed, we set a specific radio station that would play when a certain time occurs. In this application, we will create such an application that starts playing the computer CD at the specified time.

To make this application work, you must make sure you launch it before going to bed (of course, an alternative is to create a tray or Service application that can start on its own but we will not get into that this time), exactly like you must remember to set up your clock radio before going to bed.

Prerequisites:

  • Timer
  • MCI (we haven't had time to cover MCI enough but you don't need all its details to perform this exercise)

Practical Learning: Creating the Application

 
  1. Start Microsoft Visual C++
  2. Create a new MFC Application named WakeMeUp1
  3. Create the project as a Dialog Based and remove the About Box
  4. Delete the TODO line and the Cancel button. We will leave the OK button so the user can close the application by pressing Enter (you know in the morning, you should be able to stop the application by pressing something, especially if you are not in the mood to leave the bed)
  5. Set the dialog's Border Style to Thin
  6. Position the OK button to the top-left side of the dialog box and change its Caption to Snooze
  7. Add another button to the dialog box and set its properties as follows:
    Caption: Set 
    ID: IDC_SET
     
  8. In the Resource View, right-click the name of the project and click Resource Symbols...
  9. In the Resource Symbols dialog box, click New and create a new resource symbol named IDC_CHECKTIME
     
  10. Click Close
  11. In the header file of the dialog box, declare two integer variables named DialogWidth and DialogHeight in a private section of the header file of the dialog:
     
    private:
    	int DialogWidth ;
    	int DialogHeight;
    };
  12. When the application is running, we will draw its screen black so that if it is next to the bed, its colors should not disturb our sleep.
    Display the dialog box and double-click the Set button
  13. Implement its event as follows:
     
    void CWakeMeUp1Dlg::OnBnClickedSet()
    {
    	// TODO: Add your control notification handler code here
    	// Get the screen dimensions
    	DialogWidth  = GetSystemMetrics(SM_CXSCREEN);
    	DialogHeight = GetSystemMetrics(SM_CYSCREEN);
    
    	// When sets the clock CD, remove the title bar and the borders
    	LONG ptrStyles = GetWindowLong(this->m_hWnd, GWL_STYLE);
    	ptrStyles &= ~WS_TILEDWINDOW;
    	SetWindowLong(this->m_hWnd, GWL_STYLE, ptrStyles);
    
    	// Occupy the whole screen
    	SetWindowPos(&wndTopMost, 0, 0, DialogWidth, DialogHeight, SWP_SHOWWINDOW);
    
    	// This will represent the dimensions of the whole screen
    	CRect rctClient;
    	// Get a reference to the device context
    	CClientDC dc(this);
    	// Create a black brush
    	CBrush BlackBrush(RGB(0, 0, 0));
    	// Select the black brush
    	CBrush *pOldBrush = dc.SelectObject(&BlackBrush);
    
    	// Get the dimension of the current dialog box
    	GetClientRect(&rctClient);
    	
    	// Paint the dialog box in black
    	dc.Rectangle(rctClient);
    	// Restore the original brush
    	dc.SelectObject(pOldBrush);
    
    	// Start the timer control
    	SetTimer(IDC_CHECKTIME, 400, 0);
    
    	// We don't need to see the cursor
    	ShowCursor(FALSE);
    }
  14. On the main menu of Visual C++, click Project -> Add Existing Item... (MSVC 6: Project -> Add To Project -> Files)
  15. Locate the Lib folder of your MSVC installation:
    MSVC .NET = C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Lib
    or
    MSVC 6 = C:\Program Files\Microsoft Visual Studio\VC98\Lib
  16. Select WinMM.lib
     
  17. Click Open
  18. Open the StdAfx.h header file and add the following line:
     
    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently,
    // but are changed infrequently
    
    #pragma once
    
    #ifndef VC_EXTRALEAN
    #define VC_EXTRALEAN		// Exclude rarely-used stuff from Windows headers
    #endif
    
    // Modify the following defines if you have to target a platform prior to the ones specified below.
    // Refer to MSDN for the latest info on corresponding values for different platforms.
    #ifndef WINVER				// Allow use of features specific to Windows 95 and Windows NT 4 or later.
    #define WINVER 0x0400		// Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
    #endif
    
    #ifndef _WIN32_WINNT		// Allow use of features specific to Windows NT 4 or later.
    #define _WIN32_WINNT 0x0400		// Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
    #endif						
    
    #ifndef _WIN32_WINDOWS		// Allow use of features specific to Windows 98 or later.
    #define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
    #endif
    
    #ifndef _WIN32_IE			// Allow use of features specific to IE 4.0 or later.
    #define _WIN32_IE 0x0400	// Change this to the appropriate value to target IE 5.0 or later.
    #endif
    
    #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS	// some CString constructors will be explicit
    
    // turns off MFC's hiding of some common and often safely ignored warning messages
    #define _AFX_ALL_WARNINGS
    
    #include <afxwin.h>         // MFC core and standard components
    #include <afxext.h>         // MFC extensions
    #include <afxdisp.h>        // MFC Automation classes
    
    #include <afxdtctl.h>		// MFC support for Internet Explorer 4 Common Controls
    #ifndef _AFX_NO_AFXCMN_SUPPORT
    #include <afxcmn.h>			// MFC support for Windows Common Controls
    #endif // _AFX_NO_AFXCMN_SUPPORT
    
    #include <Mmsystem.h>
    
  19. Generate the WM_TIMER message for the dialog to get its OnTimer() event
  20. To test the application, double-click the clock on the taskbar of your computer and get the time it displays (write it down)
  21. Implement the event as follows:
     
    void CWakeMeUp1Dlg::OnTimer(UINT nIDEvent)
    {
    	// TODO: Add your message handler code here and/or call default
        CTime tmeCurrent = CTime::GetCurrentTime();
    
        if( tmeCurrent == CTime(tmeCurrent.GetYear(), tmeCurrent.GetMonth(), tmeCurrent.GetDay(), HOUR, MINUTE, 0, 0) )
    	{
    		char strSender[] = "play cdaudio";
    		mciSendString(strSender, NULL, 0, NULL);
    	}
    
    	CDialog::OnTimer(nIDEvent);
    }
  22. In the above code, replace HOUR with the same hour you wrote down
  23. Add 4 to the number of minutes you wrote down and replace MINUTE in the above code with it
  24. Return to the dialog box and Generate its WM_DESTROY message
  25. Implement its OnDestroy event as follows:
     
    void CWakeMeUp1Dlg::OnDestroy()
    {
    	CDialog::OnDestroy();
    
    	// TODO: Add your message handler code here
    	// Stop the CD
    	char strSender[] = "stop cdaudio";
    	mciSendString(strSender, NULL, 0, NULL);
    
    	// Stop the timer
    	KillTimer(IDC_CHECKTIME);
    }
  26. Although we are stopping the CD, remember that, as it may have happened to you in the morning, the user can let the CD continue playing. We are simply allowing him or her to stop the mess, I mean the music
    Put a music CD in your CD drive or make sure there is one
  27. Execute the application to test it
  28. Click the Set button and wait for the time you had set to occur
  29. After hearing the CD playing, press Esc to stop it
  30. To improve the application, add a Static Control to the dialog box and set its Caption to Wake me up at
  31. Add a Date Time Picker to the dialog box and set its properties as follows:
    ID: IDC_TIMETOWAKEUP
    Format: Time
     
  32. Add a Control Member variable for the Date Timer Picker control and name it m_TimeToWakeUp
     
  33. In the header file of the dialog box, declare a CTime variable named tmeSet:
     
    // WakeMeUp1Dlg.h : header file
    //
    
    #pragma once
    #include "atltime.h"
    #include "afxdtctl.h"
    
    
    // CWakeMeUp1Dlg dialog
    class CWakeMeUp1Dlg : public CDialog
    {
    // Construction
    public:
    	CWakeMeUp1Dlg(CWnd* pParent = NULL);	// standard constructor
    
    // Dialog Data
    	enum { IDD = IDD_WAKEMEUP1_DIALOG };
    
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
    
    
    // Implementation
    protected:
    	HICON m_hIcon;
    
    	// Generated message map functions
    	virtual BOOL OnInitDialog();
    	afx_msg void OnPaint();
    	afx_msg HCURSOR OnQueryDragIcon();
    	DECLARE_MESSAGE_MAP()
    
    private:
    	int DialogWidth ;
    	int DialogHeight;
    public:
    	afx_msg void OnBnClickedSet();
    	afx_msg void OnTimer(UINT nIDEvent);
    	afx_msg void OnDestroy();
    	CDateTimeCtrl m_TimeToWakeUp;
    	CTime tmeSet;
    };
  34. Initialize the tmeSet variable when the user clicks the Set button as follows:
     
    void CWakeMeUp1Dlg::OnBnClickedSet()
    {
    	// TODO: Add your control notification handler code here
    	// Get the time that the user had set and store it
    	this->m_TimeToWakeUp.GetTime(tmeSet);
    
    	// Get the screen dimensions
    	DialogWidth  = GetSystemMetrics(SM_CXSCREEN);
    	DialogHeight = GetSystemMetrics(SM_CYSCREEN);
    
    	// When sets the clock CD, remove the title bar and the borders
    	LONG ptrStyles = GetWindowLong(this->m_hWnd, GWL_STYLE);
    	ptrStyles &= ~WS_TILEDWINDOW;
    	SetWindowLong(this->m_hWnd, GWL_STYLE, ptrStyles);
    
    	// Occupy the whole screen
    	SetWindowPos(&wndTopMost, 0, 0, DialogWidth, DialogHeight, SWP_SHOWWINDOW);
    
    	// This will represent the dimensions of the whole screen
    	CRect rctClient;
    	// Get a reference to the device context
    	CClientDC dc(this);
    	// Create a black brush
    	CBrush BlackBrush(RGB(0, 0, 0));
    	// Select the black brush
    	CBrush *pOldBrush = dc.SelectObject(&BlackBrush);
    
    	// Get the dimension of the current dialog box
    	GetClientRect(&rctClient);
    	
    	// Paint the dialog box in black
    	dc.Rectangle(rctClient);
    	// Restore the original brush
    	dc.SelectObject(pOldBrush);
    
    	// Start the timer control
    	SetTimer(IDC_CHECKTIME, 400, 0);
    
    	// We don't need to see the cursor
    	ShowCursor(FALSE);
    }
  35. Change the OnTimer event as follows:
     
    void CWakeMeUp1Dlg::OnTimer(UINT nIDEvent)
    {
    	// TODO: Add your message handler code here and/or call default
    	CTime curTime = CTime::GetCurrentTime();
    
        	if( curTime == tmeSet )
    	{
    		char strSender[] = "play cdaudio";
    		mciSendString(strSender, NULL, 0, NULL);
    	}
    
    	CDialog::OnTimer(nIDEvent);
    }
  36. Test the application
 

Home Copyright © 2005 FunctionX, Inc.