Table of contents
1.
Introduction 
2.
Active Template Library (ATL)
3.
Message handler functions of ATL
3.1.
Message handler
3.1.1.
Syntax
3.1.2.
Parameters
3.1.3.
Example
3.2.
CommandHandler
3.2.1.
Syntax
3.2.2.
Parameters
3.2.3.
Example
3.3.
Notify handler
3.3.1.
Syntax
3.3.2.
Parameters
3.3.3.
Example
4.
Frequently Asked Questions
4.1.
What is the result of successful message processing?
4.2.
What is the parameter of Notify handler in the NOTIFY_HANDLER macro?
4.3.
What happens to bHandler in Message handler?
4.4.
Why do we use classes?
5.
Conclusion
Last Updated: Oct 28, 2024

Message handler functions in ATL

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Hello, ninjas. Have you heard of the Active Template Library of Microsoft? Are you intrigued to know more about it? Well, here we go!

Message handler functions in ATL

In this blog, we will discuss the message handler functions of ATL in C++. C++ is a high-level language developed by extending features of already existing C language. Active Template Library(ATL) is a template library that makes programming in C++ simpler and more efficient.

Active Template Library (ATL)

Microsoft developed ATL to simplify C++ programming. ATL is a set of C++ class templates aimed at simplifying the programming of COM objects. The component Object Model (COM) is an independent system that can create Binary interactive software.

ATL can be very fruitful with complete knowledge of COM.

Today we will discuss the message handler function of the ATL.

Message handler functions of ATL

In the active template library (ATL), the message handler function is three types:

Message handler

In the message map, the second parameter of the MESSAGE_HANDLER macro defines the MessageHandler function.

Syntax

LRESULT MessageHandler(
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam,
    BOOL& bHandled);
You can also try this code with Online C++ Compiler
Run Code

Parameters

Parameters Description
uMsg Specifies the message
wParam More message-specific knowledge.
lParam More message-specific knowledge.
bHandler Before calling MessageHandler, the message map sets bHandled to TRUE. The message should be marked as needing more processing if the MessageHandler does not fully handle it by setting bHandled to FALSE.

Example

class MyBaseWindow: public CWindowImpl<CMyBaseWindow>
{
public:
   BEGIN_MSG_MAP(MyBaseWindow)
      MESSAGE_HANDLER(WM_CREATE, OnCreate)
   END_MSG_MAP()

   // When a MyBaseWindow object will receive a WM_CREATE message, the message
   // will be directed to MyBaseWindow::OnCreate for accurate processing.
   LRESULT OnCreate(UINT /*nMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, 
      BOOL& /*bHandled*/)
   {
      return 0;   
   }
};
You can also try this code with Online C++ Compiler
Run Code

CommandHandler

In the message map, the third parameter of the COMMAND_HANDLER macro defines the CommandHandler function.

Syntax

LRESULT CommandHandler(
    WORD wNotifyCode,
    WORD wID,
    HWND hWndCtl,
    BOOL& bHandled);
You can also try this code with Online C++ Compiler
Run Code

Parameters

Parameters Description
wNotifyCode Specifies the message
wID Identifies the menu item, control, or accelerator.
hWndCtl Steer to window control.
bHandler Before calling CommandHandler, the message map sets bHandled to TRUE. CommandHandler should set bHandled to FALSE if it cannot fully handle the message.

Example

class ATL_NO_VTABLE CPolyProp :
   public CComObjectRootEx<CComSingleThreadModel>,
   public CComCoClass<CPolyProp, &CLSID_PolyProp>,
   public IPropertyPageImpl<CPolyProp>,
   public CDialogImpl<CPolyProp>
{
public:
BEGIN_COM_MAP(CPolyProp)
   COM_INTERFACE_ENTRY(IPropertyPage)
END_COM_MAP()

BEGIN_MSG_MAP(CPolyProp)
   COMMAND_HANDLER(IDC_SIDES, EN_CHANGE, OnEnChangeSides)
   CHAIN_MSG_MAP(IPropertyPageImpl<CPolyProp>)
END_MSG_MAP()

   // When a CPolyProp object will receive a WM_COMMAND message identified 
   // by IDC_SIDES and EN_CHANGE, the message will be directed to 
   // CPolyProp::OnEnChangeSides for accurate processing.
   LRESULT OnEnChangeSides(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, 
      BOOL& /*bHandled*/);
You can also try this code with Online C++ Compiler
Run Code

Notify handler

In message map, the third parameter of the NOTIFY_HANDLER macro defines the NotifyHandler function.

Syntax

LRESULT NotifyHandler(
    int idCtrl,
    LPNMHDR pnmh,
    BOOL& bHandled);
You can also try this code with Online C++ Compiler
Run Code

Parameters

Parameters Description
idCtrl Identifies the control of sending the message.
pnmh The location of an NMHDR structure that has the notification code and further details. This parameter links to a more prominent structure with the NMHDR structure as its first component for some notification messages.
bHandler Before using NotifyHandler, the message map changes bHandled to TRUE. NotifyHandler should set bHandled to FALSE to indicate that the message requires more processing if it does not fully handle the notification.

Example

class MyDialog: public CDialogImpl<MyDialog>
{
public:
   enum { IDD = IDD_MYDLG };

   BEGIN_MSG_MAP(CMyDialog2)
      NOTIFY_HANDLER(IDC_TREE1, NM_CLICK, OnNMClickTree1)
   END_MSG_MAP()

public:
   // When a MyDialog object will receive a WM_NOTIFY message 
   // identified by IDC_TREE1 and NM_CLICK, the message will be 
   // directed to MyDialog::OnNMClickTree1 for accurate processing
   LRESULT OnNMClickTree1(int /*idCtrl*/, LPNMHDR pNMHDR, BOOL& /*bHandled*/);
};
You can also try this code with Online C++ Compiler
Run Code

Frequently Asked Questions

What is the result of successful message processing?

The result of successful message processing is 0.

What is the parameter of Notify handler in the NOTIFY_HANDLER macro?

In the message map, the third parameter of the NOTIFY_HANDLER macro defines the NotifyHandler function.

What happens to bHandler in Message handler?

Before calling CommandHandler, the message map sets bHandler to TRUE. To signal that the message requires more processing, CommandHandler should set bHandled to FALSE if it cannot fully handle the news.

Why do we use classes?

We use classes to implement and create particular objects having multiple data types. We also use classes for encryption and encapsulation.

Conclusion

We hope this blog successfully helped you understand the message handler function of ATL.

Refer to the Basics of C++ with Data StructureDBMS, and Operating System by Coding Ninjas, and keep practising on our platform Coding Ninjas Studio. You can check out the mock test series on code studio.

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and AlgorithmsCompetitive ProgrammingAptitude, and many more! Refer to the interview bundle if you want to prepare for placement interviews. Check out interview experiences to understand various companies' interview questions.

Give your career an edge over others by considering our premium courses!

Happy Learning!

Thankyou image
Live masterclass