How to change the background color for an MDI parent form in Visual C++ .NET or in Visual C++ 2005 (816184)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft .NET Framework 1.1

For a Microsoft Visual C# .NET version of this article, see 319465.

For a Microsoft Visual Basic .NET version of this article, see 319417.

SUMMARY

This step-by-step article describes how to programmatically change the background color for a Multiple Document Interface (MDI) parent form by using Microsoft Visual C++ .NET 2003 or Microsoft Visual C++ 2005.

When you use a Windows form as an MDI parent form, the Application Background color setting in Control Panel determines the background color of the form. The color is not determined by the BackColor property of the form.

MORE INFORMATION

The following steps demonstrate how to programmatically change the background color of the MDI parent form to another color:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual C++ Projects.

    Note In Visual Studio 2005, click Visual C++ under Project Types.
  4. Under Templates, click Windows Forms Application (.NET).

    Note In Visual Studio 2005, click Windows Forms Application under Templates.
  5. In the Name box, type Q816184.
  6. In the Location box, type C:\Test, and then click OK.

    By default, a form that is named Form1 is created.
  7. Click the Form1 form. On the View menu, click Properties Window to view the properties for the form.
  8. Set the BackColor property to the color that you want.
  9. Set the IsMDIContainer property to True.

    Notice that the background color of the form changes to the color that the Application Background color is set to in Control Panel.
  10. Set the WindowState property to Maximized.
  11. On the View menu, click Code.
  12. Add the following code to the Load event handler of the Form1 form:
    {
    	MdiClient* ctlMDI;
     
    	// Loop through all the controls of the form controls looking
    	// for the control of type MdiClient.
    	IEnumerator *pEnum = this->Controls->GetEnumerator();
    	while(pEnum->MoveNext())
    	{
    		try
    		{
    			// Try to cast the control to type MdiClient.
    			System::Windows::Forms::Control *pCtrl = 
    				__try_cast<System::Windows::Forms::Control *>(pEnum->Current);
    				ctlMDI = __try_cast<MdiClient*>(pCtrl);
    		}
    		catch(InvalidCastException *)
    		{
    			continue;
    		}
    
    		// Set the BackColor property of the MdiClient control.
    		ctlMDI->BackColor = this->BackColor;
    		break;
    	}
    
    		Q816184::Form2* frm = new Q816184::Form2();
    		frm->MdiParent = this;
    		frm->Show();
    	}
    Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample.
    To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
    1. Click Project, and then click <ProjectName> Properties.
      Note<ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
    For more information about the common language runtime support compiler options, visit the following Microsoft Developer Network Web site:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

  13. On the View menu, click Solution Explorer, and then right-click the Q816184 project.
  14. Click Add, and then click Add New Item.

    The Add New Item - Q816184 dialog box appears.
  15. Under Templates, click Windows Form (.NET).
  16. In the Name box, type Form2, and then click Open.

    New source files, such as Form2.h and Form2.cpp are created, and then Windows Forms Designer opens.
  17. Switch to Form1.h file, and then add the following code after the #pragma directive:
    #include "Form2.h"
  18. Press CTRL+F5 to build and to run the application.

    Notice that the MDI parent form appears with the background color that you wanted.

REFERENCES

For more information about the MdiClient class, visit the following Microsoft Developer Network (MSDN) Web site:For more information about the Form.BackColor property, visit the following MSDN Web site:

Modification Type:MajorLast Reviewed:1/21/2006
Keywords:kbMDI kbForms kbWindowsForms kbHOWTOmaster KB816184 kbAudDeveloper kbAudITPRO