I have a SDI MFC App without Document/View support. I want to embed the CFormView with controls designed in resource editor in ChildView. How do I do that?
The MFC Wizard generated 3 files:
- App.cpp (derived from
CWinApp
) - MainFrame.cpp (derived from
CFrameWnd
) - ChildView.cpp (derived from
CWnd
)
Now, I have generated custom class that derives from CFormView
, where IDD_MYVIEW
is the generated ID to resource GUI.
class MyFormView: public CFormView
{
public:
enum { IDD = IDD_MYVIEW };
MyFormView(): CFormView(IDD) {};
virtual ~MyFormView() {};
}
How do I display this MyFormView in ChildView?
As I tried generating the project again and checked in the MFC wizard the Document/View architecture checkbox and changed the base class of View to CFormView. I realized that the App initialization is different than the initially generated one.
Currently the first app is initialized as follows:
BOOL MfcApp::InitInstance()
{
// (...)
CMainFrame* pFrame = new CMainFrame;
if (!pFrame)
return FALSE;
m_pMainWnd = pFrame;
// create and load the frame with its resources
pFrame->LoadFrame(IDR_MAINFRAME,
WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
NULL);
// The one and only window has been initialized, so show and update it
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
furthermore the MainFrame initializes the ChildView in the OnCreate method.
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// create a view to occupy the client area of the frame
if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
{
TRACE0("Failed to create view window\n");
return -1;
}
}
where m_wndView
is the ChildView
. I think that I should initialize the CFormView
in OnCreate
method of the ChildView
, but I don't know how to do that nor how to "show" it. Because the CFormView
does not have these methods.
On the other hand the initialization with the Doc/View architecture looks like this. And seems to automatically cover what I want to achieve.
BOOL MfcApp::InitInstance()
{
// (...)
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMFCPlaygroundDoc),
RUNTIME_CLASS(CMainFrame),
RUNTIME_CLASS(CMFCPlaygroundView)); // <-- derived from CFormView
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
// (...)
}
The thing is, I see that in the second generated Project the CFormView is provided to the SingleDocTemplate constructor and I could place my controls there. But in the first generated project I don't know where I could connect the CFormView to displayed ChildView. I don't know how and where can I connect my new CFormView.
I find the Doc/View architecture overwhelming and unnecessary for the app I need and would like to proceed with it just for the sake of understanding it.