Click here to Skip to main content
14,450,268 members

How to change MFC view by clicking a button inside the mainframe?

Member 14561306 asked:

Open original thread
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:

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)


...

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:

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:


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...
Tags: MFC

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100