Do you think IIT Guwahati certified course can help you in your career?
No
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!
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.
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
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
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
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.