I want to change the presented View by clicking a button inside a Window... So not with the menue of MainFrame. I had a lot of problems at first to only change the view, i found a Tutorial here, how to do it without Doc/View Architecture Using views effectively without the doc/view overhead
] that was very helpful!
I use visual Studio community for my MFC Projects.
What I have tried:
I made an MFC Project (SDI) without Doc/View support.
I made two more Views in the Designer and added Classes to them. The new View Classes are derived from CFormView.
I changed the Constructor and Destructor of the new View Classes to public.
Added them as pointers to MainFrm.h:
I changed the OnCreate(),OnSetFocus() and OnCmdMsg() Method of MainFrm.cpp like this:
(That allows to present the FormView i made with the Designer)
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
ccx.m_pNewViewClass = RUNTIME_CLASS(CMainView);
m_pMainView = DYNAMIC_DOWNCAST(CMainView, this->CreateView(&ccx));
TRACE0("creation of view failed");
void CMainFrame::OnSetFocus(CWnd* )
BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
if (m_pMainView->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
Now here comes my problem! I have a button on the First presented View and if you click on it, the view should change. I made the following function with the event handler in the Designer:
If i handle it in the MainFrm.cpp class for example with menue buttons it is no problem... that works fine:
ccx2.m_pNewViewClass = RUNTIME_CLASS(CSecondView);
m_pSecondView = DYNAMIC_DOWNCAST(CSecondView, this->CreateView(&ccx2));
I tried to write a function in CMainFrame and call this function in CMainView::OnBnClickedButton1() but i dont know how to get the current MainFrm Object. A pointer on MainFrm or a member of it in CMainView did not work.
I searched and red tutorials for days to solve my problem. I also tried it with Doc/View support like shown here: https://docs.microsoft.com/en-us/cpp/mfc/adding-multiple-views-to-a-single-document?view=vs-2019 but i dont know where to call switchView() correctly.
Maybe anyone can help...