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 8134755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:09:12+00:00 2026-06-06T10:09:12+00:00

I have a constructor for a window in my project setup such that it

  • 0

I have a constructor for a window in my project setup such that it looks like this. There are many windows in my project and I’m constantly adding or removing things.

LRESULT CPicture::Msg(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    { 
        HANDLE_MSG(hWnd, WM_CREATE, OnCreate);
        HANDLE_MSG(hWnd, WM_PAINT, OnPaint);
        HANDLE_MSG(hWnd, WM_SIZE, OnSize);
        HANDLE_MSG(hWnd, WM_CLOSE, OnDestroy);

        default:
            return (DefWindowProc(hWnd, uMsg, wParam, lParam));
    }
}

Here is an example of all the valid events;

// Csx / Dsx
virtual BOOL OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct) {return FALSE;};
virtual void OnDestroy(HWND hWnd){};

// Painting
virtual void OnPaint(HWND hWnd){};
virtual BOOL OnEraseBkgnd(HWND hWnd, HDC hDC) {return FALSE;};

// Movement / Sizing
virtual void OnMove(HWND hWnd, int x, int y) {};
virtual void OnSize(HWND hWnd, UINT state, int nWidth, int nHeight){};
virtual void OnGetMinMaxInfo(HWND hWnd, LPMINMAXINFO lpMinMaxInfo){};

// Focus
virtual void OnSetFocus(HWND hWnd, HWND hWndOldFocus){};
virtual void OnKillFocus(HWND hWnd, HWND hWndNewFocus){};

… and lots more. Is it possible using preprocessor macros to detect when one of these functions is overridden in a child class and automatically add a handler for it to the message loop?

I was told by someone that It was a really bad idea for my Msg() to handle every message even if it wasn’t defined, so I’m looking for an easy alternative that isn’t user-heavy on constantly adding/removing event definitions.

  • 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-06-06T10:09:15+00:00Added an answer on June 6, 2026 at 10:09 am

    It’s not possible to do what you want exactly using the standard preprocessor, But following Bo Persson’s comment, you could do something like this:

    class MessageHandler
    {
    public:
      virtual void Handle () = 0;
      static void Dispatch (int i) { m_messages [i]->Handle (); }
    protected:
      MessageHandler (int i) { m_messages [i] = this; }
    private:
      static map <int, MessageHandler *> m_messages;
    };
    
    map <int, MessageHandler *> MessageHandler::m_messages;
    
    template <int i>
    class MessageHandlerInstance : MessageHandler
    {
    protected:
      MessageHandlerInstance () : MessageHandler (i) { }
    };
    
    #define Handler(name,i) class name : public MessageHandlerInstance <i> { void Handle (); } name##Instance;
    
    // these are the actual handlers for the messages
    Handler (Msg1, 1);
    void Msg1::Handle () { cout << "Msg1" << endl; }
    
    Handler (Msg2, 2);
    void Msg2::Handle () { cout << "Msg2" << endl; }
    
    Handler (Msg3, 3);
    void Msg3::Handle () { cout << "Msg3" << endl; }
    
    
    int main ()
    {
      // and this is how they're called
      MessageHandler::Dispatch (1);
      MessageHandler::Dispatch (2);
      MessageHandler::Dispatch (3);
    }
    

    It needs a bit of error checking – the Dispatch call will fail if there’s no message handler defined. Also, you could probably move the template class into the #define, but that would make the #define bigger.

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

Sidebar

Related Questions

I have a constructor that looks like this (in c++): Interpreter::Interpreter() { tempDat ==
I have a constructor prototype that looks like: template <typename type_position> window( const int
I have a constructor signature like this public NavigationLink(Func<String> getName, Func<UrlHelper, String> getURL, Func<bool>
I have a constructor (for an auto generated class) that has 255 paremeters. Using
I have a constructor that has a series of classes and functions within it
I have a constructor question for C#. I have this class: public partial class
I want to have a constructor with an argument that gets inherited by all
I know that you can't have a constructor in an interface, but here is
I have a simple ViewModel like: public class MainViewModel { ObservableCollection<Project> _projects; public MainViewModel()
Hey peepz! I have this footer image, that I want to align to the

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.