3

我想通过单击窗口内的按钮来更改呈现的视图, 如下所示。我的项目设置:

  1. 我制作了一个没有 Doc/View 支持的 MFC 项目 (SDI)。
  2. 我在设计器中制作了另外两个视图并向它们添加了类。新的视图类派生自CFormView. 我将新视图类的构造函数和析构函数更改为公开的。

  3. 将它们作为指向 MainFrm.h 的指针添加:

CMainView*        m_pMainView;
CSecondView*      m_pSecondView; 
  1. 我像这样更改了MainFrm.cpp 的OnCreate(),OnSetFocus()OnCmdMsg()Method :(允许展示我用 Designer 制作的 FormView)
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    // First, build the view context structure
    CCreateContext ccx;

    // Designate the class from which to build the view
    ccx.m_pNewViewClass = RUNTIME_CLASS(CMainView);

    // Using the structure, create a view
    m_pMainView = DYNAMIC_DOWNCAST(CMainView, this->CreateView(&ccx));


    if (!m_pMainView)
    {
        TRACE0("creation of view failed");
    }

    // Do layout recalc
    RecalcLayout();

    // Show the view and do an initial update

    m_pMainView->ShowWindow(SW_SHOW);
    m_pMainView->OnInitialUpdate();

    // Set this view active
    SetActiveView(m_pMainView);

    // Order it to resize the parent window to fit
    m_pMainView->ResizeParentToFit(FALSE);


    return 0;
}

...

void CMainFrame::OnSetFocus(CWnd* /*pOldWnd*/)
{

    m_pMainView->SetFocus();
}

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{

    if (m_pMainView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
        return TRUE;

    return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

现在我的问题来了!我在第一个呈现的视图上有一个按钮,如果你点击它,视图应该会改变。我在 Designer 中使用事件处理程序创建了以下函数:

void CMainView::OnBnClickedButton1()
{
// What to do here? I want to change the current view to another View by clicking the button
}

如果我在 MainFrm.cpp 类中处理它,例如使用菜单按钮,那没问题......工作正常:

void CMainFrame::OnViewNextview()
{
    CCreateContext ccx2;
    ccx2.m_pNewViewClass = RUNTIME_CLASS(CSecondView);
    m_pSecondView = DYNAMIC_DOWNCAST(CSecondView, this->CreateView(&ccx2));

    RecalcLayout();

    m_pMainView->ShowWindow(SW_SHOW);
    m_pMainView->OnInitialUpdate();

    SetActiveView(m_pMainView);

    m_pMainView->ResizeParentToFit(FALSE);

}

我试图在其中编写一个函数CMainFrame并在其中调用该函数,CMainView::OnBnClickedButton1()但我不知道如何获取当前的 MainFrm 对象。MainFrm 上的指针或其成员CMainView不起作用。

我搜索了几天的红色教程来解决我的问题。我还尝试使用 Doc/View 支持,如下所示: https ://docs.microsoft.com/en-us/cpp/mfc/adding-multiple-views-to-a-single-document?view=vs-2019但我不知道在哪里正确调用 switchView()。

也许任何人都可以帮助...

4

2 回答 2

3

首先,您不应该真正覆盖OnCmdMsg- 相反,DECLARE_MESSAGE_MAP在您的头文件和BEGIN_MESSAGE_MAP/END_MESSAGE_MAP在您的实现文件中使用,并在这两个宏之间插入处理程序消息。

我看到您的CMainView班级中已经有一个用于单击按钮的处理程序!从这里,您应该调用该CMainFrame函数以更改到下一个视图 - 就像您在给出菜单命令时所做的那样(您说它有效)。将该函数公开,并让 MainView 类访问指向主框架的指针(或使用AfxGetMainWnd()并将其转换为类的指针)。像这样的东西:

void CMainView::OnBnClickedButton1()
{
    AfxGetMainWnd()->PostMessage(WM_COMMAND, menuID); // ID of menu command that works!
}
于 2019-08-18T14:40:44.880 回答
1

给阿德里安一个大大的拥抱,我开始工作了!我还成功添加了第三个视图:)

如果您想实现更多视图,隐藏最后显示的窗口非常重要。你可以这样做:

void CMainFrame::OnView3()
{
    CCreateContext ccx3;
    ccx3.m_pNewViewClass = RUNTIME_CLASS(CThirdView);
    m_pThirdView = DYNAMIC_DOWNCAST(CThirdView, this->CreateView(&ccx3));

    RecalcLayout();

    m_pSecondView->ShowWindow(SW_HIDE); // Hide the last Window
    m_pThirdView->ShowWindow(SW_SHOW);  // Show the new Window

    m_pThirdView->OnInitialUpdate();
    SetActiveView(m_pThirdView);
    //m_pThirdView->ResizeParentToFit(FALSE); //if you call this, the size of the window is the same like in the Designer
}
于 2019-08-18T15:38:10.787 回答