如何通过单击 MainFrame 中的按钮来更改 MFC 视图
How to change MFC View by clicking a Button inside the MainFrame
我想通过单击 Window 中的按钮来更改呈现的视图
like this。
我的项目设置:
- 我在没有 Doc/View 支持的情况下制作了一个 MFC 项目 (SDI)。
我在设计器中又做了两个视图并添加了类。新视图 类 来自 CFormView
。我将新视图的构造函数和析构函数类更改为public。
将它们添加为指向 MainFrm.h 的指针:
CMainView* m_pMainView;
CSecondView* m_pSecondView;
- 我改变了 MainFrm.cpp 的
OnCreate()
、OnSetFocus()
和 OnCmdMsg()
方法,如下所示:
(这允许展示我用 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);
}
我的问题来了!我在 First presented View 上有一个按钮,如果你点击它,视图应该改变。我在设计器中使用事件处理程序创建了以下函数:
void CMainView::OnBnClickedButton1()
{
// What to do here? I want to change the current view to another View by clicking the button
}
如果我在 MainFrm.cpp class 中处理它,例如使用菜单按钮,那没问题...工作正常:
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对象。 CMainView
中 MainFrm 或其成员的指针无效。
我搜索和红色教程好几天来解决我的问题。我还尝试了 Doc/View 支持,如下所示:
https://docs.microsoft.com/en-us/cpp/mfc/adding-multiple-views-to-a-single-document?view=vs-2019 但我不知道在哪里正确调用 switchView()。
也许任何人都可以提供帮助...
首先,您不应该真的覆盖 OnCmdMsg
- 相反,在您的头文件中使用 DECLARE_MESSAGE_MAP
并在您的实现文件中使用 BEGIN_MESSAGE_MAP
/END_MESSAGE_MAP
,并且在这两个宏之间插入处理程序消息。
我看到您的 CMainView
class 中已经有一个按钮点击处理程序!从这里,您应该调用 CMainFrame
函数以更改到下一个视图 - 就像您在给出菜单命令时所做的那样(您说有效)。使该函数 public 并让 MainView class 访问主框架的指针(或使用 AfxGetMainWnd()
并将其转换为 class 的指针)。像这样:
void CMainView::OnBnClickedButton1()
{
AfxGetMainWnd()->PostMessage(WM_COMMAND, menuID); // ID of menu command that works!
}
给阿德里安一个大大的拥抱,我成功了!
我也成功添加了第三个视图:)
如果您想实现更多视图,隐藏最后显示的 Window 非常重要。你可以这样做:
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
}
我想通过单击 Window 中的按钮来更改呈现的视图 like this。 我的项目设置:
- 我在没有 Doc/View 支持的情况下制作了一个 MFC 项目 (SDI)。
我在设计器中又做了两个视图并添加了类。新视图 类 来自
CFormView
。我将新视图的构造函数和析构函数类更改为public。将它们添加为指向 MainFrm.h 的指针:
CMainView* m_pMainView;
CSecondView* m_pSecondView;
- 我改变了 MainFrm.cpp 的
OnCreate()
、OnSetFocus()
和OnCmdMsg()
方法,如下所示: (这允许展示我用 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);
}
我的问题来了!我在 First presented View 上有一个按钮,如果你点击它,视图应该改变。我在设计器中使用事件处理程序创建了以下函数:
void CMainView::OnBnClickedButton1()
{
// What to do here? I want to change the current view to another View by clicking the button
}
如果我在 MainFrm.cpp class 中处理它,例如使用菜单按钮,那没问题...工作正常:
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对象。 CMainView
中 MainFrm 或其成员的指针无效。
我搜索和红色教程好几天来解决我的问题。我还尝试了 Doc/View 支持,如下所示: https://docs.microsoft.com/en-us/cpp/mfc/adding-multiple-views-to-a-single-document?view=vs-2019 但我不知道在哪里正确调用 switchView()。
也许任何人都可以提供帮助...
首先,您不应该真的覆盖 OnCmdMsg
- 相反,在您的头文件中使用 DECLARE_MESSAGE_MAP
并在您的实现文件中使用 BEGIN_MESSAGE_MAP
/END_MESSAGE_MAP
,并且在这两个宏之间插入处理程序消息。
我看到您的 CMainView
class 中已经有一个按钮点击处理程序!从这里,您应该调用 CMainFrame
函数以更改到下一个视图 - 就像您在给出菜单命令时所做的那样(您说有效)。使该函数 public 并让 MainView class 访问主框架的指针(或使用 AfxGetMainWnd()
并将其转换为 class 的指针)。像这样:
void CMainView::OnBnClickedButton1()
{
AfxGetMainWnd()->PostMessage(WM_COMMAND, menuID); // ID of menu command that works!
}
给阿德里安一个大大的拥抱,我成功了! 我也成功添加了第三个视图:)
如果您想实现更多视图,隐藏最后显示的 Window 非常重要。你可以这样做:
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
}