使用 CString 参数调用 UpdateAllViews

Calling UpdateAllViews with a CString parameter

我需要使用以下参数调用 CDocument::UpdateAllViews 方法:

MSDN 文档中描述的一种方法是传递 CObject 派生的 class 并覆盖 CView 派生的 class 中的 CView::OnUpdate 成员函数。

还有其他方法吗?

不,没有!因为 UpdateAllViews 的最后一个参数需要一个指向对象的指针,而 CString 不是从 CObject 派生的,所以您需要将字符串包装在 class 派生自 [=12] =]:

class CMyHint : public CObject
{
public: 
    CString m_strHint;
};

...
CMyHint hint;
hint.m_strHint = _T("hint");
UpdateAllViews(nullptr,0,&hint);
...

void CMyView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
    CMyHint *pMyHint = static_cast<CMyHint*)(pHint);
    CString str = pMyHint->m_strHint;
...

编辑:我刚刚查看了 MFC 的源代码。在 CDocument::UpdateAllViewsCView::OnUpdate 之间没有使用 CObject *pHint。所以指针永远不会用作 CObject

所以有可能(但我不推荐它),您使用 reinterpret_cast<CObject*> 指向指向 CString 的指针,然后在 CView 你使用 reinterpret_cast<CString*> 再次获取字符串指针。

可能,但又一次:我不推荐它!