Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 488037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:39:58+00:00 2026-05-13T01:39:58+00:00

How could I create similar structure to handle Win32 Messages like it is in

  • 0

How could I create similar structure to handle Win32 Messages like it is in MFC?

In MFC;

BEGIN_MESSAGE_MAP(CSkinCtrlTestDlg, CDialog)
    //{{AFX_MSG_MAP(CSkinCtrlTestDlg)
    ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
    ON_BN_CLICKED(IDC_DEFAULTSKIN, OnChangeSkin)
    ON_WM_DRAWITEM()
    ON_WM_MEASUREITEM()
    ON_WM_COMPAREITEM()
    ON_BN_CLICKED(IDC_CHECK3, OnCheck3)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_MESSAGE_MAP macro handles this behaviour. What to do for pure Win32?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:39:58+00:00Added an answer on May 13, 2026 at 1:39 am

    Here is a breif summary of the code I use to do this in the Zeus programmer’s editor:

    Step 1: Define a couple of message structure to hold the Windows message details:

    typedef struct
    {
      MSG     msg;
      LRESULT lResult;
    } xMessage;
    
    struct xWM_COMMAND
    {
      HWND hwnd;
      UINT Msg;
      WORD ItemID;
      WORD NotifyCode;
      HWND Ctl;
      LRESULT lResult;
    };
    
    //-- unpack a message buffer
    #define MSG_UNPACK(var, id, msg) x##id *var = (x##id *)(msg);
    

    Step 2: Define a base window class with a few special methods:

    class xWindow
    {
    protected:
      //-- windows callback function
      static LRESULT CALLBACK wndProc(HWND hwnd, UINT msg, 
                                      WPARAM wParam, 
                                      LPARAM lParam);
    
      //-- a message dispatch method
      void dispatch(HWND hwnd, UINT uMessageID, WPARAM wParam, 
                    LPARAM lParam, LRESULT &Result);
    
      //-- method for command message dispatching
      virtual void dispatchToCmdMap(xMessage *pMessage);
    
      //-- method for windows message dispatching
      virtual void dispatchToMsgMap(xMessage *pMessage);
    };
    

    Step 3: Define a few macros to do the dispatching of the Windows messages:

    #define BEGIN_MSG_MAP                          \
       protected:                                  \
       virtual void dispatchToMsgMap(xMessage *msg)\
       {                                           \
         if (msg->msg.message == WM_NULL)          \
         {                                         \
           return;                                 \
         }
    
    #define MSG_HANDLER(meth, wm_msg)              \
         else if (msg->msg.message == wm_msg)      \
         {                                         \
           this->meth(msg);                        \
           return;                                 \
         }
    
    #define END_MSG_MAP(base)                      \
         else if (msg->msg.message == WM_COMMAND)  \
         {                                         \                       
           this->dispatchToCmdMap(msg);            \                       
           return;                                 \                       
         }                                         \                       
         else if (msg->msg.message == WM_NOTIFY)   \                       
         {                                         \                       
           this->dispatchToNotifyMap(msg);         \                       
           return;                                 \                       
         }                                         \                       
                                                   \                       
         base::dispatchToMsgMap(msg);              \                       
       };
    
    #define BEGIN_CMD_MAP                          \
       virtual void dispatchToCmdMap(xMessage *msg)\
       {                                           \                              
         MSG_UNPACK(Cmd, WM_COMMAND, msg);         \                              
                                                   \                              
         if (Cmd->ItemID == 0)                     \                              
         {                                         \                              
            /* not allowed */                      \                              
         }                                                                        
    
    #define CMD_HANDLER(meth, cmd_id)              \
         else if (Cmd->ItemID == cmd_id)           \
         {                                         \                                
           this->meth(Cmd->ItemID);                \                                
         }                                                                          
    
    #define END_CMD_MAP(base)                      \
         else                                      \                              
         {                                         \                              
           base::dispatchToCmdMap(msg);        \                              
         }                                         \                              
       };
    

    Step 4: Define the dispatcher method:

    void xWindow::dispatch(HWND, UINT uMessageID, WPARAM wParam, 
                           LPARAM lParam, LRESULT &Result)
    {
      xMessage message;
    
      //-- build up a message packet
      message.msg.message = uMessageID;
      message.msg.wParam  = wParam;
      message.msg.lParam  = lParam;
      message.lResult     = 0;
    
      //-- dispatch the message
      this->dispatchToMsgMap(&message);
    }
    

    Step 5: Define the static window procedure method (NOTE: this method will need to be used as the Window procedure of the window class when the class is first registered):

    LRESULT CALLBACK xWindow::wndProc(HWND hwnd, UINT msg, 
                                      WPARAM wParam, 
                                      LPARAM lParam)
    {
      LRESULT lResult = 0;
    
      //-- look for the creation message
      if (msg == WM_NCCREATE)
      {
        CREATESTRUCT *pCreateData = (CREATESTRUCT*)lParam;
    
        //-- get the window object passed in
        xWindow *pWindow = (xWindow)pCreateData->lpCreateParams;
    
        if (pWindow)
        {
          //-- attach the window object to the hwnd
          SetWindowLong(hwnd, pWindow);
    
          //-- let the window object dispatch the message
          pWindow->dispatch(hwnd, msg, wParam, lParam, lResult);
        }
        else
        {
          //-- leave the message to windows
          lResult = DefWindowProc(hwnd, msg, wParam, lParam);
        }
      }
      else if (hwnd)
      {
        //-- get the object attached to the hwnd
        xWindow *pWindow = (xWindow *)GetWindowLong(hwnd);
    
        //-- check to see if we have an object window attached to the handle
        if (pWindow)
        {
          //-- let the window object dispatch the message
          pWindow->dispatch(hwnd, msg, wParam, lParam, lResult);
        }
        else
        {
          //-- leave the message to windows
          lResult = ::DefWindowProc(hwnd, msg, wParam, lParam);
        }
      }
    
      return lResult;
    }
    

    Now, using this base class it is possible to define a new window class that will look like this:

    class MyWindow : public xWindow
    {
    protected:  
      //-- the WM_COMMAND message handlers
      virtual void onAdd(int);
      virtual void onDelete(int);
    
      //-- the WM_CLOSE message handler
      virtual void onClose(xMessage *pMessage);
    
      //-- the WM_SIZE message handler
      virtual void onSize(xMessage *pMessage);
    
    public:
      //-- ctor and dtor
      MyWindow();
      virtual ~MyWindow();
    
      BEGIN_MSG_MAP
        //-- command message handlers
        CMD_HANDLER(onAdd   , IDPB_ADD   )
        CMD_HANDLER(onDelete, IDPB_DELETE)
    
        //-- other message handling
        MSG_HANDLER(onClose , WM_CLOSE)
        MSG_HANDLER(onSize  , WM_SIZE )
      END_MSG_MAP(xWindow)
    };
    

    Edit: How this code works.

    The secret to understanding how this code works is to remember the wndProc in the xWindow class is nothing but a Win32 Window procedure passed to RegisterClassEx when the Win32 Window is registered.

    Now if you look at the wndProc code you will see it does a bit of setting up and checking but generally it does nothing more than send the Windows message to the dispatch method.

    The dispatch method is even simpler as it does nothing more than pack the Windows message into an easy to move structure and then sends it off to the dispatchToMsgMap method.

    Now look at the MyWindow class an you will see this code:

    BEGIN_MSG_MAP    
       //-- command message handlers    
       CMD_HANDLER(onAdd   , IDPB_ADD   )    
       CMD_HANDLER(onDelete, IDPB_DELETE)    
    
       //-- other message handling    
       MSG_HANDLER(onClose , WM_CLOSE)    
       MSG_HANDLER(onSize  , WM_SIZE )  
    END_MSG_MAP(xWindow)
    

    This code is just using the macros defined earlier. If you take a close look at these macros you will see the code above is in fact creating a dispatchToMsgMap method. This is the exact same dispatchToMsgMap method that was called by the dispatch method.

    I know this method of handling Windows messages does work as I use this exact same approach in the Zeus for Windows editor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Google Checkout and I could like to create an absolute URL
I would like to create custom SEO friendly routes similar to what is used
I have a MySQL database and would like to have a similar structure in
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
Someone on IRC told me I could create objective c objects directly from plist
I am wondering how I could create a custom data type to use within
In Microsoft Visual Studio 6.0, you could create several files at once by spacing
On other windows operating systems, I could create a Windows Service that listens to
I'm trying to use the microsoft_maps_mapcontrol. I see how one could create a pushpin
How could i create a trigger that at any insertion on my table [users]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.