简体   繁体   English

了解CStatic和CWnd以及消息路由

[英]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. 我试图了解消息路由在MFC中的工作方式,对此我有一些疑问。 Imagine a control that extends CWnd. 想象一个扩展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. 我知道CStatic不会发生这种情况,CStatic只会在您设置SS_NOTIFY样式时传递特定的消息。 What I'm trying to understand if that's specific to CStatic or happens with all the controls. 我想了解的是CStatic特有的还是所有控件都发生了什么。 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? 因此,如果我从CWnd而不是CStatic扩展控件,消息将继续传递吗? Is FORWARD_NOTIFICATIONS() available in MFC? 在MFC中可以使用FORWARD_NOTIFICATIONS()吗? If not I'd rather extend my intermmediate classes to handle messages as needed. 如果没有,我宁愿扩展我的中间类来根据需要处理消息。 Any other solutions you know off? 您还知道其他解决方案吗?

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. 一些窗口通常以WM_NOTIFY消息的形式发送消息给父母,或者以“特殊”消息的形式(例如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. MFC有一个系统(“反射”系统),使窗口将这些消息发送回窗口本身,以便您可以在控件中处理消息,而不必涉及父控件。

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. 例如,您不希望将所有用于子窗口的WM_PAINT发送给父母。

What you want to do (handling button clicks) is different. 您想要做的(处理按钮单击)是不同的。 Button clicks are 'emitted' by the button in the form of BN_CLICKED. 按钮单击以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'. 如果仍然确定,则可以接管子窗口的WndProc进行一些“过滤”。 Generally this is done using the PreTranslateMessage() virtual function. 通常,这是使用PreTranslateMessage()虚拟函数完成的。

You need to be using notifications, that means sending a WM_NOTIFY message with your own code specified in the attached structure. 您需要使用通知,这意味着发送WM_NOTIFY消息,并在附加结构中指定您自己的代码。 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 . 然后,您的父控件可以处理消息ON_NOTIFY ,或者你可以得到主人类来处理消息本身ON_NOTIFY_REFLECT

You could always avoid the Windows\\MFC messaging architecture and use an event based system instead. 您始终可以避免使用Windows \\ MFC消息传递体系结构,而使用基于事件的系统。 Something like Boost.Signals2 . Boost.Signals2之类的东西。 In our applications we use a mixture of WM_NOTIFY messages and Boost.Signals2. 在我们的应用程序中,我们混合使用WM_NOTIFY消息和Boost.Signals2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM