HOWTO: Add Multiple Window Types to a Non-Document/View MDI App
ID: Q201045
|
The information in this article applies to:
-
The Microsoft Foundation Classes (MFC), included with:
-
Microsoft Visual C++, 32-bit Editions, version 6.0
SUMMARY
The following steps create a simple application with two types of windows, one for drawing, the other for text, starting from the wizard-generated non document view MDI application. At the end, the application has two CMDIChildFrame-derived classes called CDrawFrame and CTextFrame, two CWnd-derived classes called CDrawWnd and CTextWnd, and resources (accelerators, menus, icons, strings) called IDR_DRAWTYPE and IDR_TEXTTYPE.
The application has two types of windows, each with its own menu, accelerator table, title and icon. More complex features like persistency are not implemented in this code.
MORE INFORMATIONStep-by-Step Procedure
- Create a MDI application with the MFC Application Wizard, and clear the 'Document/View architecture support' in step 1. In step 6, CChildFrame and CChildView are the default names the Wizard uses for frame and window classes. Rename these to CDrawFrame and CDrawWnd, respectively. Call the header and implementation files DrawFrm and DrawWnd.
If you want to paste the following code into your application without any changes, name your application class CNDVWApp.
- Delete the OnFileNew() handler declaration, definition and entry in the message map for the application class.
- Create a new header and source file called TextFrm.h and TextFrm.cpp. Copy DrawFrm.h and DrawFrm.cpp to TextFrm.h and TextFrm.cpp, respectively. Add them to the project. In TextFrm.h and TextFrm.cpp, replace all occurrences of CDrawFrame with CTextFrame and DrawFrm with TextFrm.
- Create a new header and source file called TextWnd.h and TextWnd.cpp. Copy DrawWnd.h and DrawWnd.cpp to TextWnd.h and TextWnd.cpp, respectively. Add them to the project. In TextWnd.h and TextWnd.cpp replace all occurrences of CDrawWnd with CTextWnd and DrawWnd with TextWnd.
- Change the m_wndView member of CTextFrame to be of CTextWnd type. Change the name of the included file DrawWnd.h to TextWnd.h.
- Include TextFrm.h in the implementation file for the application class after include of DrawFrm.h. The project should now compile.
- Recreate the Class Wizard information, (.clw file), for the project. Close the workspace, delete the .clw file and reload the workspace. Next, invoke the Class Wizard, press Yes in the message box and then click the OK button in the dialog box to close the Class Wizard.
- Differentiate the two CWnd-derived classes for members and drawing.
In CDrawWnd:
// data members
protected:
CRect m_Ellipse;
CRect m_Rectangle;
// initialization: replace existing contructor
CDrawWnd::CDrawWnd():
m_Ellipse(10, 10, 100, 100),
m_Rectangle(5, 5, 105, 105)
{
}
// painting: replace existing OnPaint
void CDrawWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.Rectangle(m_Rectangle);
dc.Ellipse(m_Ellipse);
}
In CTextWnd:
// data members
protected:
CString m_Text;
// initialization: replace existing contructor
CTextWnd::CTextWnd():
m_Text("Text Window ")
{
}
// painting: replace existing OnPaint
void CTextWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect r(10, 10, 400, 50);
dc.DrawText(m_Text, r, DT_BOTTOM);
}
- Delete the menu and accelerator handles in the application class and create the following:
HMENU m_hDrawMenu;
HMENU m_hTextMenu;
HACCEL m_hDrawAccel;
HACCEL m_hTextAccel;
- Modify the application class's InitInstance() and ExitInstance() to load and unload resources IDR_DRAWTYPE and IDR_TEXTTYPE instead of the Wizard-generated resources.
For InitInstance():
HINSTANCE hInst = AfxGetResourceHandle();
m_hDrawMenu = ::LoadMenu(hInst, MAKEINTRESOURCE(IDR_DRAWTYPE));
m_hTextMenu = ::LoadMenu(hInst, MAKEINTRESOURCE(IDR_TEXTTYPE));
m_hDrawAccel = ::LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_DRAWTYPE));
m_hTextAccel = ::LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_TEXTTYPE));
For ExitInstance():
int CNDVWApp::ExitInstance()
{
if (m_hDrawMenu != NULL)
FreeResource(m_hDrawMenu);
if (m_hDrawAccel != NULL)
FreeResource(m_hDrawAccel);
if (m_hTextMenu != NULL)
FreeResource(m_hTextMenu);
if (m_hTextAccel != NULL)
FreeResource(m_hTextAccel);
return CWinApp::ExitInstance();
}
- In the menu IDR_MAINFRAME, delete the menu item 'New' and define 'New Draw' (ID_FILE_NEW_DRAW) and 'New Text' (ID_FILE_NEW_TEXT). In the toolbar, IDR_MAINFRAME, delete the ID_FILE_NEW button and add two new buttons for ID_FILE_NEW_DRAW and ID_FILE_NEW_TEXT.
- Define handlers for these two items in the application class.
For OnFileNewDraw():
void CNDVWApp::OnFileNewDraw()
{
CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
// create a new MDI child window
pFrame->CreateNewChild(
RUNTIME_CLASS(CDrawFrame), IDR_DRAWTYPE, m_hDrawMenu, m_hDrawAccel);
}
For OnFileNewText():
void CNDVWApp::OnFileNewText()
{
CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
// create a new MDI child window
pFrame->CreateNewChild(
RUNTIME_CLASS(CTextFrame), IDR_TEXTTYPE, m_hTextMenu, m_hTextAccel);
}
- Copy the IDR_MAINFRAME menu and accelerator resources and paste them into IDR_DRAWTYPE and IDR_TEXTTYPE.
- Add a 'Text' pop-up menu to IDR_TEXTTYPE, and an 'Add Text' menu item, associating it to ID_TEXT_ADD. Also define ID_TEXT_ADD accelerator (CTRL+A) to the IDR_TEXTTYPE accelerator table.
- Add a 'Draw' pop-up menu to IDR_DRAWTYPE, and a 'Widen draw' menu item, associating it to ID_DRAW_WIDEN. Also define ID_DRAW_WIDEN accelerator (CTRL+W) to IDR_DRAWTYPE accelerator table.
- Define handlers for both commands in the respective CWnd-derived classes:
void CDrawWnd::OnDrawWiden()
{
m_Ellipse.InflateRect(10, 10);
m_Rectangle.InflateRect(10, 10);
Invalidate();
}
void CTextWnd::OnTextAdd()
{
m_Text += " Window";
Invalidate();
}
- Define IDR_DRAWTYPE and IDR_TEXTTYPE strings in stringtable in order to set window titles:
IDR_DRAWTYPE \nDraw\nDraw
IDR_TEXTTYPE \nText\nText
- Define IDR_DRAWTYPE and IDR_TEXTTYPE icon resources and associate them with the frames: modify the PreCreateWindow() function in the frames as follows:
BOOL CDrawFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CMDIChildWnd::PreCreateWindow(cs) )
return FALSE;
HICON hWndIcon = ::LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_DRAWTYPE));
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(0, 0, 0, hWndIcon);
return TRUE;
}
BOOL CTextFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CMDIChildWnd::PreCreateWindow(cs) )
return FALSE;
HICON hWndIcon = ::LoadIcon(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_TEXTTYPE));
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(0, 0, 0, hWndIcon);
return TRUE;
}
- Build the application.
Testing the Application
Execute the application and test different features:
- You can create multiple windows of either the draw or text type using both the menu and the toolbar.
- The menu resource is automatically changed accordingly to the current window type.
- Accelerator keys are also automatically loaded and unloaded depending on the current window type. Text CTRL+A and CTRL+W.
- Text and draw child frames have different window titles and icons.
Additional query words:
Keywords : kbDocView kbMFC kbVC
Version : winnt:6.0
Platform : winnt
Issue type : kbhowto
|