webdreamer
webdreamer

Reputation: 2499

Understanding CStatic and CWnd and message routing

I'm trying to understand how message routing works in MFC, and I have some questions regarding it. Imagine a control that extends CWnd. My first question is: are all messages in that control passed on to the parent control?

I know that doesn't happen with CStatic which only passes specific messages when you set the SS_NOTIFY style. What I'm trying to understand if that's specific to CStatic or happens with all the controls. Specifically I'm trying to make a control that has several child controls with the sole purpose of defining their layout. I wanted all messages of the child controls to be handled by the parent of this intermmediate control. For example if this layout control has a child button, when the button is clicked the message would be sent to the parent to be treated.

However I don't intend to treat all messages manually. So, if I extend my control from CWnd instead of CStatic will the message be passed on? Is FORWARD_NOTIFICATIONS() available in MFC? If not I'd rather extend my intermmediate classes to handle messages as needed. Any other solutions you know off?

Upvotes: 1

Views: 1546

Answers (2)

Roel
Roel

Reputation: 19642

Messages are sent to a window itself.

Some windows send messages to their parents, usually in the form of WM_NOTIFY messages, or (like for buttons) in 'special' message like BN_CLICKED.

MFC has a system (the 'reflection' system) to let windows send those messages back to the window itself, so that you can deal with messages in the control rather than involving the parent control.

This is roughly how it work in the abstract. What you want (have a parent control handle all messages send to all child controls) is not generally how you 'should' do it. For example, you don't want all WM_PAINT for child windows to be send to the parents.

What you want to do (handling button clicks) is different. Button clicks are 'emitted' by the button in the form of BN_CLICKED. Those would be handled by the parent anyway.

If you're still determined, you can take over the WndProc of the child windows to do some 'filtering'. Generally this is done using the PreTranslateMessage() virtual function.

Upvotes: 1

Mark Ingram
Mark Ingram

Reputation: 73673

You need to be using notifications, that means sending a WM_NOTIFY message with your own code specified in the attached structure. Your parent controls can then handle the messages with ON_NOTIFY, or you can get the owner class to handle the message itself with ON_NOTIFY_REFLECT.

You could always avoid the Windows\MFC messaging architecture and use an event based system instead. Something like Boost.Signals2. In our applications we use a mixture of WM_NOTIFY messages and Boost.Signals2.

Upvotes: 0

Related Questions