Share via


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 noteSecurity Note

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To get samples and instructions for installing them:

To access samples from Visual Studio

  • On the Help menu, click Samples.

    By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.

  • For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.

Building and Running the Sample

To build and run the MDI sample

  1. Open the solution mdi.sln.

  2. On the Build menu, click Build.

  3. 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.

See Also

Other Resources

MFC Samples