MDI Sample: Demonstrates MDI Without Using Doc/View Architecture
The MDI sample uses MFC's multiple-document interface (MDI) support without using the document/view architecture.
For an additional MDI sample, see MDIDOCVW.
MDI also illustrates the Microsoft Windows timer, CColorDialog, CBitmap, and changing the default cursor of a window.
Security Note: |
---|
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended. |
To get samples and instructions for installing them:
On the Visual Studio Help menu, click Samples.
For more information, see Visual Studio Samples.
The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.
You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.
Building and Running the Sample
To build and run the MDI sample
Open the solution mdi.sln.
On the Build menu, click Build.
On the Debug menu, click Start Without Debugging.
The MDI sample application provides a parent window with two distinct types of MDI child windows: a Bounce window, in which a ball bounces around; and a Hello window, which displays the text "Hello, World!" You can create multiple instances of each type of window.
Click New Bounce or New Hello on the File menu to create a new MDI child window. Both MDI child windows determine what menus are shown when they are active. Both windows have a Color menu. When you choose a color from this menu, the color of the bouncing ball or of the Hello text is updated. Choose the Custom item to call a color dialog box, from which you can select a special color.
The Bounce window also has a Speed menu, from which you can select the speed at which the ball moves around the screen.
If you minimize the Bounce window, notice that there is not a static icon. Rather, a small ball bounces around in the icon. In contrast, the Hello window has a normal icon.
MDI Without Documents and Views
Unlike an MDI application produced by the application wizard, this sample application does not use the framework's support for documents and views. Thus, it does not use document templates; the application's InitInstance does not call AddDocTemplate.
This application nevertheless takes full advantage of the framework's MDI support. CMainFrame is derived from CMDIFrameWnd. CBounceWnd and CHelloWnd are derived from CMDIChildWnd. Commands on the Window menu, such as Tile, are handled by the framework's default implementation in CMDIFrameWnd.
Although CBounceWnd and CHelloWnd both override Create, the override is not necessary to take advantage of the framework's MDI support. The override of Create illustrates how to change the default cursor and icon of a window. Changing the default cursor or icon of a window requires registering a new WNDCLASS by calling AFXRegisterWndClass and passing the name of the WNDCLASS in the call to Create.
Keywords
This sample demonstrates the following keywords:
AfxGetInstanceHandle; AfxRegisterWndClass; CBitmap::CreateCompatibleBitmap; CCmdUI::SetCheck; CColorDialog::DoModal; CColorDialog::GetColor; CDC::BitBlt; CDC::CreateCompatibleDC; CDC::DeleteDC; CDC::DrawText; CDC::Ellipse; CDC::FillRect; CDC::GetDeviceCaps; CDC::SelectObject; CDC::SetBkColor; CDC::SetTextColor; CFrameWnd::LoadFrame; CFrameWnd::rectDefault; CGdiObject::DeleteObject; CMDIChildWnd::Create; CMenu::LoadMenu; CWinApp::InitInstance; CWnd::DestroyWindow; CWnd::GetClientRect; CWnd::GetCurrentMessage; CWnd::GetDC; CWnd::Invalidate; CWnd::KillTimer; CWnd::MessageBox; CWnd::OnCreate; CWnd::OnPaint; CWnd::OnSize; CWnd::OnTimer; CWnd::ReleaseDC; CWnd::SetTimer; CWnd::ShowWindow; CWnd::UpdateWindow; GetSysColor; LOWORD; LoadCursor; LoadIcon; MAKEINTRESOURCE; RGB; max; min
Note
Some samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.