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 define custom events when errors occur in a program. The event in question can be a beep, some other notification or the termination of a thread. MFC Events, like the example code given below, are appropriate for mission-critical Visual C++ applications.

Things You'll Need

  • Microsoft Visual Studio
Show More

Instructions

    • 1

      Give the following declaration a global scope. If you are using a dedicated header file for all your declarations include the statement below at the top of all declarations in that header file:

      int CODERSOURCEMSG;

    • 2

      Declare the prototype of the handler function that wraps around the event in the appropriate module. Use the following signature:

      afx_msg void onCODERSOURCEMSG();

    • 3

      Write the handler function for the event:

      void onCODERSOURCEMSG()

      {

      // Insert the code of the event here

      }

    • 4

      Create a Message Map. To find out how message maps are created in the Microsoft Visual Studio, Visual C++, read eHow article "How to Create Message Maps in MFC". Add the following macro in the message map:

      ON_REGISTERED_MESSAGE(CODERSOURCEMSG, onCODERSOURCEMSG)

    • 5

      Insert the following line in all the critical, error-prone places in your source code where you want each of the MFC events raised:

      PostMessage(CODERSOURCEMSG);

Related Searches:

Resources

Comments

You May Also Like

Related Ads

Featured