如何将MFC消息重定向到另一个对象?

发布于 2024-07-23 05:22:27 字数 184 浏览 10 评论 0原文

在从 CWnd 派生的类的实例中,是否可以将所有 MFC 消息转发(重定向)到另一个对象,而无需为每个可能的消息编写单独的处理程序和消息映射? 具体来说,我想将所有未处理的 WM_ 消息转发到另一个对象。 如果这是不可能的,那么对象是否可以侦听其他对象内发生的事件? 如果这有助于澄清我的问题,我可以提供详细信息。

谢谢。

In an instance of a class derived from CWnd, is it possible to forward (redirect) all MFC messages to another object, without writing separate handlers and message mappings for each possible message? Specifically, I'd like to forward all unhandled WM_ messages to another object. If this isn't possible, then is it possible for objects to listen to events that occur within other objects? I can provide details if that would help clarify my question.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不爱素颜 2024-07-30 05:22:27

您的目标需要比 MFC 低一点。 您应该重写窗口的 PreTranslateMessage 方法并直接处理消息。

一旦有了主消息循环,您就可以选择哪些消息由您的应用程序处理,哪些消息发送/发布到另一个应用程序。 如果您选择发送消息,我建议使用 SendMessageTimeout(...)

You'll need to aim a little lower than MFC. You should override the PreTranslateMessage method of your window and process the messages directly.

Once you have the main message loop, you can pick and choose which ones are handled by your app and which ones are Sent/Posted to another. If you choose to send the message, I'd recommend SendMessageTimeout(...)

始于初秋 2024-07-30 05:22:27

我认为你需要子类化

I think you need subclassing.

陌上青苔 2024-07-30 05:22:27

不,你不能真正做到你所要求的,但你可能没有必要这样做。 (您的问题的一些更多详细信息可能按顺序排列。)当您创建消息映射时,您指定它所应用的类以及该类的基类。 如果派生类没有特定消息的消息映射条目,MFC 将检查基类的消息映射。 如果基类消息映射没有条目,它将检查其基类的消息映射,依此类推。

例如,假设您有一堆具有通用功能的对话框。 您可以这样布局您的类:

class CMyBaseDialog : public CDialog {
    DECLARE_MESSAGE_MAP();
}

BEGIN_MESSAGE_MAP(CMyBaseDialog, CDialog)
    // Handle any common messages here...
END_MESSAGE_MAP()

class CDerivedDialog : public CMyBaseDialog {
     DECLARE_MESSAGE_MAP();
}

BEGIN_MESSAGE_MAP(CDerivedDialog, CMyBaseDialog)
    // Handle any specific messages here...
END_MESSAGE_MAP()

这同样适用于所有其他基于 HWND 的类,例如 CWnd、CView、CFrame 等。 如果您专门处理命令消息,那么您还有一些其他选项。

No, you can't really do what you're asking, but you probably don't have to. (Some more details of your problem may be in order.) When you create a message map, you specify both the class to which it applies and the base class for that class. If your derived class doesn't have a message map entry for a particular message, MFC will check the message map for the base class. If the base class message map has no entry, it will check the message map for its base class, and so on.

For example, suppose you have a bunch of dialogs with common functionality. You could lay out your classes thusly:

class CMyBaseDialog : public CDialog {
    DECLARE_MESSAGE_MAP();
}

BEGIN_MESSAGE_MAP(CMyBaseDialog, CDialog)
    // Handle any common messages here...
END_MESSAGE_MAP()

class CDerivedDialog : public CMyBaseDialog {
     DECLARE_MESSAGE_MAP();
}

BEGIN_MESSAGE_MAP(CDerivedDialog, CMyBaseDialog)
    // Handle any specific messages here...
END_MESSAGE_MAP()

The same applies to all other HWND based classes, such as CWnd, CView, CFrame, and so on. If you're dealing specifically with command messages, then you have some additional options.

陪你搞怪i 2024-07-30 05:22:27

好吧,因为我似乎无法发表评论,所以我将其作为答案发布。 我在遵循 Brad 的回答时遇到了问题,其中一些 WM_COMMANd 消息未通过 PreTranslateMessage 函数路由(请参阅我对问题的回答 如果我没有声明相应的消息映射条目,如何阻止 MFC 禁用我的控件命令?),但是是通过 OnCommand 所以基本上我重写了 OnCommand 函数来转发所有 WM_COMMAND 消息。 我发布这个以防有人遇到同样的问题。

不管怎样,感谢布拉德的帮助,你的回答对我帮助很大。

Well since I can't seem to be able to post a comment I'll post this as an answer. I had a problem following Brad's answer where some WM_COMMANd messages where not routed through the PreTranslateMessage function (see my answer to my question How to stop MFC from disabling my controls if I don't declare a message map entry for it's corresponding command?) but were through OnCommand so basically I overriden the OnCommand function to forward all WM_COMMAND messages too. I'm posting this in case anyone get the same problem.

Anyway thanks for the helps Brad, your answer helped me a lot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文