How to Make an MFC Alarm Application

If you want an alarm application so that you can remember to perform a particular task at certain time, think about using Visual C++. Its alarm subsystem "alarm.h" is designed to handle alarms based on the operating system clock. The MFC alarm_proc function pointer can be wrapped inside member functions in a CAlarm class. Then a CSchedule class can manage CAlarm objects to make your application functional.

Things You'll Need

  • An IDE such as Visual Studio .NET 2005
Show More

Instructions

    • 1

      Understand the alarm_proc function pointer that's at the heart of the program. It accepts as parameters the execution time of the alarm and an alarm ID or alarm handle value. Here is its prototype:

      typedef void (*alarm_proc)(int time, int alarm_id);

    • 2

      Put alarm_proc into a class, CAlarm, to give it instructions. The member functions of CAlarm will represent simple tasks such as enable, disable, setting a time and more. Here's a suggestion on how you could implement CAlarm:

      class CAlarm

      {

      public:

      typedef void (*alarm_proc)();

      //creates an alarm

      CAlarm(const CTime& time, alarm_proc fire): mTime(time), mEnabled(true) {

      mAlarm_proc = fire;

      }

      ~CAlarm(){}

      void setTime(const CTime& time) {

      mTime = time;

      }

      const CTime& getTime() const {

      return mTime;

      }

      bool isEnabled() const {

      return mEnabled;

      }

      void fire() const {

      mAlarm_proc();

      }

      void disable() {

      mEnabled = false;

      }

      void enable() {

      mEnabled = true;

      }

      protected:

      bool mEnabled;

      CTime mTime;

      mutable alarm_proc mAlarm_proc;

      };

    • 3

      Design a CScheduler class for manipulating CAlarm objects. At minimum, it should do what a typical stop watch does. That is, it should start a loop, stop a loop and reset, but also add an alarm, remove an alarm and so on. Following is a suggestion for a CScheduler class. Only the public member functions (class interfaces) are shown. Implementing each member function is left as an exercise for the programmer:

      class CScheduler

      {

      public:

      CScheduler();

      ~CScheduler();

      void AddAlarm(int handle, const CTime& time, CAlarm::alarm_proc fire);

      void RemoveAlarm(int handle);

      const CAlarm* Alarm(int handle) const;

      CAlarm* Alarm(int handle);

      void StartLoop();

      void StopLoop();

      bool IsRunning() const;

      void Reset();

      private:

      //

      protected:

      //

      };

    • 4

      Use an MFC CMap class to store the CAlarm objects in the CScheduler class. A CMap array is excellent for fast indexing and add/delete operations. Put the CMap variable in the "protected" access identifier of the CScheduler class. Here's what it looks like:

      class CScheduler {

      public:

      //

      protected:

      CMap mAlarms;

      };

    • 5

      Create a function that continually iterates through the CMap objects (CAlarms) and, if one needs to be executed, executes it. Make it a friend and put it in "protected" in CScheduler, under mAlarms. Its simplified implementation can be as follows:

      int Iterate( CSchedule* pSchedule ){

      POSITION pos;

      int ID;

      CAlarm* pAlarm;

      CTime time;

      while(pSchedule->isRunning()){

      if(pSchedule->mAlarms.IsEmpty())

      continue;

      time = CTime::GetCurrentTime();

      pos = pSchedule->mAlarms.GetStartPosition();

      while(pos!=NULL) {

      pSchedule->mAlarms.GetNextAssoc(pos, ID, pAlarm);

      if(pAlarm->getTime() <= time) {

      if(pAlarm->isEnabled())

      pAlarm->fire();

      pAlarm->setTime(pAlarm->getTime());

      delete pAlarm;

      }

      }

      }

      return 0;

      }

    • 6

      Use the CScheduler class in an MFC application. It won't run elsewhere. Add the CScheduler and CAlarm cpp and h files to your project. Make an instance of the CSchedule class in your application. And, of course, don't forget to call Iterate().

Related Searches:

Resources

Comments

You May Also Like

  • How to Troubleshoot an MFC 210C

    The Brother MFC 210C allows users to complete a number of tasks, including sending and receiving fax transmissions, making copies, printing digital...

  • About Talking Alarm Clocks

    Talking alarm clocks allow users to interact with them vocally. This means they announce the time and in some cases take verbal...

  • How to Make a MFC Paint Brush Application

    This article will walk you through the steps to write a simple paint brush application using the Microsoft Foundation Class (MFC) library....

  • How to Make MFC Command Buttons

    "CButton" is the MFC class that allows programmers to manipulate command buttons. Command buttons are ubiquitous in programs with a graphical user...

  • Prometh VC Side Effects

    Various side effects are commonly associated with Prometh VC. These side effects are not usually considered to be cause for alarm as...

  • How to Create Message Maps in MFC

    A Windows operating system is message driven environment. An event like a mouse click or a printer signal sends a message to...

  • How to Store a List of Objects in MFC

    Every programming language and library includes an array as part of its toolset. Arrays are useful because they allow convenient and efficient...

  • How to Make MFC Interface Threads

    A thread is a semi-independent program segment that resides within a program's execution memory space. The Microsoft Foundation Class (MFC) Library offers...

  • How to Make Files in MFC

    CFile is the base class for all MFC file classes. It is used as a tool for handling disk files. The CFile...

  • How to Write an MFC API for Workstation Shutdown

    In order to shutdown a workstation using an API you need to make a shutdown timer in MFC. This API using MFC...

  • How to Create MFC Events

    The MFC Library provides an evolution of the Exception Handling Model of plain C++. In Visual C++ the user is able to...

  • How to Uninstall Car Alarms

    Uninstalling a car alarm, whether for removal, reinstallation, or repair, can be a delicate process. Though it may take some know-how and...

  • How to Make Timers in MFC and Visual C++

    A timer in Visual C++ is the term used to describe an arbitrary lapse of time. Programmers are the only people to...

  • What Is Mfc71.Dll?

    The MFC71.dll file stores programming subroutines for use with applications created with Microsoft's Visual Studio 2003 development program. Applications created with ...

  • How to Change an Alarm Code for Home Security

    Home alarm systems are a popular security measure used by many homeowners. From time to time it may be necessary to change...

  • iTunes Alarm Troubleshooting

    Apple iTunes Alarm Clock uses songs in an iTunes library to awaken you as opposed to the bells or beeps of classic...

  • How to Monitor Home Security Systems With an iPhone

    Apple's iPhone is more than just a phone and MP3 player. It also provides users with a constant connection by using both...

  • How to Make a Simple Alarm

    This is about the simplest electronic alarm that you can make. When someone enters the room the door opens, an iron plate...

Related Ads

Featured