Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,
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:

C++
CMainView*        m_pMainView;
CSecondView*      m_pSecondView;



I changed the OnCreate(),OnSetFocus() and OnCmdMsg() Method of MainFrm.cpp like this:
(That allows to present the FormView i made with the Designer)


C++
...

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);
}



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:

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



If i handle it in the MainFrm.cpp class for example with menue buttons it is no problem... that works fine:


C++
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);

}


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...
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900