MFC - 如何向从 CView 类派生的所有视图发布消息? [英] MFC - How to post a message to all views that were derived from CView class?

查看:69
本文介绍了MFC - 如何向从 CView 类派生的所有视图发布消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向所有视图发布消息.我正在考虑获取 Document 全局引用,然后实现如下方法

I want to post a message to all views. I'm considering get a Document global reference and then implement a method like below

void SomeAppDoc::DispatchToAll( UINT msg, WPARAM wP, LPARAM lP )
{
  //some how get all view's reference
  //iterate and update each views 
}

什么是有效的方法?

推荐答案

最简单的方法是调用 CDocument::UpdateAllViews,它调用 OnUpdate 附加到文档的每个视图的函数.

The simplest way is to call CDocument::UpdateAllViews, which calls the OnUpdate function of each view attached to the document.

如果您确实需要向每个视图发布消息,而不是调用 OnUpdate,请执行类似于 UpdateAllViews 的操作:

If you really need to post a message to each view, rather than call OnUpdate, do something similar to UpdateAllViews:

void SomeAppDoc::DispatchToAll(UINT msg, WPARAM wParam, LPARAM lParam)
{
    POSITION pos = GetFirstViewPosition();
    while (pos != NULL)
    {
        CView* pView = GetNextView(pos);
        pView->PostMessage(msg, wParam, lParam);
    }
}

我希望这会有所帮助!

这篇关于MFC - 如何向从 CView 类派生的所有视图发布消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆