Click here to Skip to main content
13,344,508 members (48,513 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 27 May 2005

Good bye, BEGIN_MSG_MAP!

, 19 Jul 2005
Rate this:
Please Sign up or sign in to vote.
A replacement for BEGIN_MSG_MAP macros, using the Boost.MPL library.


Four yeas ago, I made a program. Everything WTL originally had was useless except Win32 thin wrappers. As CUpdateUI was the one of them, I made the replacement by macros. Later, I read the book, C++ Template Metaprogramming, and I was inspired by the sample code, the finite state machine. That could remove the macros and the result was named ketchup, intended to replace BEGIN_MSG_MAP of ATL/WTL.

In time, the experience of making biscuit gave me the way of avoiding compile-time crashes. Now that ketchup is the type-safe synonym of BEGIN_MSG_MAP to help WTL catch up the modern programming.


Ketchup is a message map generator implemented using class templates. The templates allow us to write type-safe BEGIN_MSG_MAP.

A simple BEGIN_MSG_MAP macro snippet:


is approximated using Ketchup's facilities as seen in this code snippet:

  message_handler<WM_CREATE, &_::OnCreate>,
  command_id_handler<ID_APP_EXIT, &_::OnFileExit>,
  command_id_handler<ID_FILE_NEW, &_::OnFileNew>,
  command_id_handler<ID_APP_ABOUT, &_::OnAppAbout>,
  chain_msg_map< WTL::CFrameWindowImpl<CMainFrame> >


Microsoft Visual C++ .NET version 7.1, WTL 7.5 and Boost C++ libraries (no build required). I did test the demo in Visual C++ .NET standard edition with Visual C++ toolkit 2003. Ketchup itself is a header-only template library.

Quick Start

  1. Include a header and derive from ketchup::message_processor<CMainFrame>:
    #include "ketchup/ketchup.hpp"
    class CMainFrame : 
      public WTL::CFrameWindowImpl<CMainFrame>,
      public WTL::CUpdateUI<CMainFrame>,
      public WTL::CMessageFilter, public WTL::CIdleHandler,
      public ketchup::message_processor<CMainFrame>
  2. Define message handlers:
      LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, 
                     LPARAM /*lParam*/, BOOL& /*bHandled*/)
        // create command bar window
        HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, 
                        NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
        // attach menu
        // ...
      // ...
  3. Let CMainFrame a message map:
        message_handler<WM_CREATE, &_::OnCreate>,
        command_id_handler<ID_APP_EXIT, &_::OnFileExit>,
        command_id_handler<ID_FILE_NEW, &_::OnFileNew>,
        command_id_handler<ID_VIEW_TOOLBAR, &_::OnViewToolBar>,
        command_id_handler<ID_VIEW_STATUS_BAR, &_::OnViewStatusBar>,
        command_id_handler<ID_APP_ABOUT, &_::OnAppAbout>,
        chain_msg_map< WTL::CUpdateUI<CMainFrame> >,
        chain_msg_map< WTL::CFrameWindowImpl<CMainFrame> >
  4. Finally, override CMessageMap::ProcessWindowMessage as BEGIN_MSG_MAP insidiously does, using process_window_message provided by ketchup::message_processor<CMainFrame>:
      virtual BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg,
        WPARAM wParam, LPARAM lParam, LRESULT& lResult, DWORD dwMsgMapID = 0)
        return process_window_message(hWnd, uMsg, wParam, 
                            lParam, lResult, dwMsgMapID);

Bear in mind that declarations of message handlers should be placed before the entry and C++ Standard doesn't allow you to abbreviate the syntax of member function pointers.

Points of interest

The last point is the performance. The program size seems not to be a problem. VC++ 7.1 generates the same size program as the original one, because Ketchup's message map is almost same as BEGIN_MSG_MAP. But VC++ 7.1 can't inline message handlers, unlike BEGIN_MSG_MAP. Could this be a problem of the speed?

Well, I did not intend to emulate the appearance of BEGIN_MSG_MAP. It is not just a syntax sugar but the coincidence as a result of naming consistency with ATL/WTL. It was an amazing discovery for me.

By the way, Ketchup must be the first application using Boost.Xpressive for the implementation.



  • 23rd May, 2005 - version 0.910 (Initial release).
  • 27th May, 2005 - version 0.940.
  • 30th May, 2005 - version 0.950 (class_trace and debugging_entry added).
  • 12th Jun, 2005 - version 0.951 (debugging_entry removed; empty_entry and debug_entry added).
  • 20th Jul, 2005 - version 0.9992.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Japan Japan
I am worried about my poor English...

You may also be interested in...

Comments and Discussions

AnswerI don't get it Pin
auge__29-Jan-13 23:48
memberauge__29-Jan-13 23:48 
General"&_::",too ugly Pin
ZhangLi16-May-08 22:23
memberZhangLi16-May-08 22:23 
GeneralFor the newest version of ketchup message map library, click here! Pin
mb2sync6-Jul-05 20:15
membermb2sync6-Jul-05 20:15 
GeneralI really like your idea! Pin
peterxiau13-Jun-05 15:02
memberpeterxiau13-Jun-05 15:02 
GeneralRe: I really like your idea! Pin
mb2sync14-Jun-05 7:09
membermb2sync14-Jun-05 7:09 
GeneralThoughts Pin
peterchen11-Jun-05 14:50
memberpeterchen11-Jun-05 14:50 
GeneralRe: Thoughts Pin
mb2sync11-Jun-05 15:55
membermb2sync11-Jun-05 15:55 
GeneralThoughts Pin
Jörgen Sigvardsson28-May-05 11:29
memberJörgen Sigvardsson28-May-05 11:29 
GeneralRe: Thoughts Pin
mb2sync28-May-05 12:49
membermb2sync28-May-05 12:49 
GeneralRe: Thoughts Pin
Jörgen Sigvardsson28-May-05 12:54
memberJörgen Sigvardsson28-May-05 12:54 
GeneralRe: Thoughts Pin
mb2sync28-May-05 18:06
membermb2sync28-May-05 18:06 
Anonymous28-May-05 0:44
sussAnonymous28-May-05 0:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 20 Jul 2005
Article Copyright 2005 by mb2sync
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid