1

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.

Community
  • 1
  • 1
JaykeBird
  • 372
  • 6
  • 17
  • 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 May 03 '16 at 10:22
  • @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 May 03 '16 at 10:25
  • 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 May 03 '16 at 10:28
  • @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 May 03 '16 at 10:33
  • 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`](https://msdn.microsoft.com/en-us/library/ewt90sw3.aspx) 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 May 03 '16 at 10:38

1 Answers1

1

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;
}
Biruk Abebe
  • 2,235
  • 1
  • 13
  • 24
  • You don't need all of this "magic"; the document template (that invokes `OnNewDocument()`), will also update the frame, which in turn will update all views. You simply need to override `virtual void OnUpdate(...)` in your view class. – Vlad Feinstein May 03 '16 at 18:41
  • @VladFeinstein Yes you are right, I've updated the answer. But,if i am not mistaken, i don't think overriding the `OnUpdate(...)` is also necessary if you are using `OnDraw()` to display the contents in the view since the default implementation of `OnUpdate()` invalidate the entire client area. – Biruk Abebe May 03 '16 at 20:05
  • it *may* not be necessary, but these functions serve different purposes: `OnUpdate()` is called when the content of the document has changed, while `OnDraw()` - when you need to render some parts of the window when it, for example, is uncovered. `OnUpdate()` can also provide a hint to what exactly has changed. – Vlad Feinstein May 03 '16 at 20:10
  • @ValdFeinstein You are right again. What i meant is since `OnUpdate` invalidate the entire client area, it will mark the view for painting when `WM_PAINT` message is recivied which i believe will lead to a call to `OnDraw()`. And in this case since the user is creating a new document, i assume the whole document will change and you probably need to invalidate the entire client area anyway(which the default implementation will do) or did i get it wrong?. – Biruk Abebe May 03 '16 at 20:15