且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MFC - 如何向从 CView 类派生的所有视图发布消息?

更新时间:2022-04-27 15:21:19

最简单的方法是调用 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);
    }
}

我希望这会有所帮助!