1
votes

I'm probably going to sound a bit like an idiot for not knowing any better, but frankly, I feel more inclined to blame the lack of help online in this matter.

I decided to create a new program using MFC (using SDI with View/Doc model), before I heard about the general opinion of MFC. So my app includes the main Frame window (CMainFrame) and a child window (CMFCTestView) and document (CMFCTestDoc). Anyway, I want to have it so that when the user presses the "New" button on the toolbar, it gets rid of the child window that I currently have and replaces it with a new, fresh child window.

The code I currently have for this is:

void CMainFrame::OnFileNew()
{
#ifdef _DEBUG
    _cprintf("New Game!");
#endif

    Board b = Board(9, 9, 9);

    theApp.RecieveBoard(b);

   // Create(_T("CMFCTestDoc"), _T("CMFCTestView"), WS_CHILD | WS_VISIBLE,
   //     rectDefault, this);

    CreateEx(NULL, _T("CMFCTestView"), _T("Minesweeper"), WS_CHILD | WS_VISIBLE, rectDefault, this, IDR_MAINFRAME, 0);
}

The small amount of information I could find online told me that CreateEx was the function to use to create that new child window, so I tried to apply the example here to my application, but yet I get an error because it couldn't make the window. How am I actually supposed to accomplish what I'm trying to do?

Bonus: How do you pass along data to a new child window? Say you want the window to have certain variables/values set when it is first shown to the user. I tried to follow along with this answer, but got confused after deriving my own class based upon CMultiDocTemplate.

1
Why do you want to replace the child window with a new one? The view is there to just display what ever data you have in your document class. can't you just update your data and redraw the view to reflect the changes? BTW the examples you follow address MDI MFC application while your is SDI MFC application. - Biruk Abebe
@bkVnet That would also be a cool option. That still depends upon me being able to pass along data to the child window, which I still don't know how to do. - JaykeBird
You don't need to create a child window when working with SDI MFC application. You update the data in your document class(in the process give it what ever default value you what) and update the view to reflect these changes. I am assuming you are using the view to display your data in the document class(which is the document/view architecture is all about), or did i misunderstand your question? - Biruk Abebe
@bkVnet That does help explain things. However, I still don't know how to access the document class from the frame window. Like I said in the original question, I probably sound like an idiot asking about this lol. - JaykeBird
MFC SDI application will reuse the same document. You don't have to access the document class from the mainframe window. When the user clicks on the new file command CDocument::OnNewDocument member function of the document class will be called by the default implementation. You can probably do what you want to do in there. - Biruk Abebe

1 Answers

1
votes

If i understand your question correctly,MFC SDI applications will reuse the same document and the default view to display the content of the document. You don't have to handle the new file command from the main frame window to create a new view. When the user clicks on the new file command CDocument::OnNewDocument member function of the document class will be called by the default implementation. From there you can reinitialize your data.

Probably something like this:

BOOL CMFC_testDoc::OnNewDocument()
{
    if (!CDocument::OnNewDocument())
        return FALSE;

    //here you reinitialize your data in the document class,which will be presented by the view to the user

    return TRUE;
}