Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ MFC将数据从CViewTree传递到CFormView_C++_Mfc - Fatal编程技术网

C++ MFC将数据从CViewTree传递到CFormView

C++ MFC将数据从CViewTree传递到CFormView,c++,mfc,C++,Mfc,我有一个基于CView类的MFC应用程序 我需要将CViewTree之间的数据传递到主视图 pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CMFCApplication1Doc), RUNTIME_CLASS(CMainFrame), // <-- the CViewTree is here RUNTIME_CLASS(CMFCApplication1Vi

我有一个基于CView类的MFC应用程序

我需要将CViewTree之间的数据传递到主视图

pDocTemplate = new CSingleDocTemplate(
    IDR_MAINFRAME,
    RUNTIME_CLASS(CMFCApplication1Doc),
    RUNTIME_CLASS(CMainFrame),       // <-- the CViewTree  is here
    RUNTIME_CLASS(CMFCApplication1View)); // <-- the main view (CFormView) is here
pDocTemplate=新的CSingleDocTemplate(
IDR_大型机,
运行时类(CMFCApplication1Doc),

运行时类(CMainFrame),//定义您自己的消息,然后使用
AfxGetApp()->m\u pMainWnd->SendMessage(MY\u message)

在您的CFormView(CMainView???)中处理MY_消息,并在必要时转换为派生的CFormView。

一个通用示例-在“销毁”主应用程序之前,在虚拟项目中尝试它

1a)将数据放入文档类中。每当修改数据时,调用

1b)添加一个特殊功能,从任何地方抓取文档 在你的应用程序中

// Data class
class Item : public CObject
{
    // your data here
};

class MyDocument : public CDocument
{
public:

    // Get the document from 'anywhere' for SDI projects
    // See:
    // http://asg.unige.ch/Past/fuentes/Mfc/HowTo_8.html
    // http://forums.codeguru.com/showthread.php?473808-MFC-Doc-View-How-to-get-the-active-document-anywhere-in-my-application

    static MyDocument* GetDoc()
    {
        auto frame = (CFrameWnd*)(AfxGetApp()->m_pMainWnd);
        return (MyDocument*)frame->GetActiveDocument();
    }

    static const int HintItemAdded = 1;
    // Add other hint constants here

    // Data functions
    void AddItem(Item const& item)
    {
        m_items.Add(item);

        // Call this if you need it    
        SetModifiedFlag();

        // Notify views that data has changed (via CView::OnUpdate)
        UpdateAllViews(nullptr, HintItemAdded,
            &m_items[m_items.GetUpperBound()]);
    }

    // Functions for inserting, updating, getting, removing data, etc.

private:
    CArray<Item> m_items;  // data
};
3) 确保视图具有OnUpdate处理程序。这是由 CDocument::UpdateAllViews函数

class MyView : public CFormView
{
    void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) override
    {
        CFormView::OnUpdate(pSender, lHint, pHint);

        // Just added a new item. pHint points to the new item
        if (lHint == MyDocument::HintItemAdded)    
        {
            auto newItem = (Item*)(pHint);

            // Add the new item to a list box
            m_listBox.AddString(newItem.GetText());
        }
   }

   CListBox m_listBox;
}

据我所知,这将有助于抓住这一事件。但是,如果我希望两者都使用相同的数据集合,该怎么办呢。是否可能?使数据收集保持静态,不要忘记同步读/写操作。只有在两个或多个线程之间共享数据时,才应同步。。。如果你不使用多线程,那么就不需要数据同步。“有什么建议吗?”——不要在FC++中先跳脚,没有足够的C++和Windows API编程背景。这些问题不断出现,而且本质上都是一样的:如何在运行时在实例之间传递数据。需要解决的核心问题是,OP始终无法理解其应用程序的运行时结构,这主要是因为MFC非常复杂。
class MyView : public CFormView
{
    void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) override
    {
        CFormView::OnUpdate(pSender, lHint, pHint);

        // Just added a new item. pHint points to the new item
        if (lHint == MyDocument::HintItemAdded)    
        {
            auto newItem = (Item*)(pHint);

            // Add the new item to a list box
            m_listBox.AddString(newItem.GetText());
        }
   }

   CListBox m_listBox;
}