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.
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:
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.