MTMDI Sample: Demonstrates an MFC User Interface Thread

The MTMDI sample illustrates an MFC user interface thread where user interface events are processed in a separate thread from the main application thread. This sample is a modified version of the single-thread MDI sample.

MTMDI does not claim a strong rationale for putting the bouncing ball window in a separate thread. A user would not be able to detect the difference between the MDI and MTMDI samples on a single-processor computer. Even on a multiprocessor computer, the user would not be able to detect the difference, given that the ball movement is based on a window timer.

Nevertheless, MTMDI does illustrate techniques for implementing an MFC user interface thread. Compare the sources for the MDI and MTMDI samples to study the programming overhead associated with using MFC user interface threads.

The programming overhead in MTMDI should be a warning that you should have a good reason for using a user interface thread to justify the programming cost. The much more common type of thread in MFC is the worker thread, illustrated by the MTRECALC sample.

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 MTMDI sample

  1. Open the solution MtMdi.sln.

  2. On the Build menu, click Build.

  3. On the Debug menu, click Start Without Debugging.

You can use the File menu to create new windows of two different types. Once the windows are created, the application will let you change the attributes of the items in the window by using commands in one of these menus: Color, Speed, Window, and Help. Note that the "Hello!" windows do not have a Speed menu.

MTMDI makes use of its own CWinThread-derived class, called CBounceThread. CBounceThread is implemented in the Mtbounce.cpp file. The thread contains all the painting and timing code that the bouncing ball window needs to animate the ball in the window.

The application creates each thread just as the MDI child window is created. This happens in the CBounceMDIChildWnd::Create function, found in Bounce.cpp. This function creates both the window and the thread, associating the thread to the window.

Whenever you close a bounce window, the associated thread is automatically destroyed. The "Hello!" windows do not have a thread of their own; their messages are handled by the application's primary thread.

Keywords

This sample demonstrates the following keywords:

AfxGetInstanceHandle; AfxMessageBox; AfxRegisterWndClass; CBitmap::CreateCompatibleBitmap; CCmdTarget::OnCmdMsg; 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; CRect::Height; CRect::Width; CWinApp::ExitInstance; CWinApp::InitInstance; CWnd::Create; CWnd::DestroyWindow; CWnd::GetClientRect; CWnd::GetCurrentMessage; CWnd::GetDC; CWnd::GetDlgItem; CWnd::GetWindow; CWnd::Invalidate; CWnd::KillTimer; CWnd::MessageBox; CWnd::OnCreate; CWnd::OnDestroy; CWnd::OnLButtonDown; CWnd::OnPaint; CWnd::OnSize; CWnd::OnTimer; CWnd::ReleaseDC; CWnd::SendMessage; CWnd::SetTimer; CWnd::SetWindowPos; CWnd::ShowWindow; CWnd::UpdateWindow; CloseHandle; CreateEvent; CreateThread; EnumChildWindows; GetSysColor; GetVersion; LOWORD; LoadCursor; LoadIcon; LoadMenu; MAKEINTRESOURCE; RGB; SetEvent; Sleep; WaitForSingleObject; 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