How to set the color and the font of the StatusBarPanel object by using Visual C++ .NET or Visual C++ 2005 (816182)



The information in this article applies to:

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


For a Microsoft Visual Basic .NET version of this article, see 319312.
For a Microsoft Visual C# .NET version of this article, see 319311.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System::Collections
  • System::Windows::Forms
  • System::ComponentModel
  • System::Data
  • System::Drawing

IN THIS TASK

SUMMARY

This step-by-step article discusses how to programmatically set the color and the font of the StatusBarPanel object by using Microsoft Visual C++ .NET or Microsoft Visual C++ 2005.

The StatusBar control includes a Panels property that is a collection of StatusBarPanel objects. The StatusBarPanel class does not have any members that you can use to change the background color or the font. However, you can use GDI+ to paint the panel with a background color and to draw the text by using a font and a color that you specify.

back to the top

Requirements

This article assumes that you are familiar with the following topics:
  • Intermediate Visual C++ programming concepts
  • .NET Framework fundamentals
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Visual C++ .NET 2003 or Microsoft Visual C++ 2005
back to the top

Create the sample

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual C++ Projects under Project Types, and then click Windows Forms Application (.NET) under Templates.

    Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates.
  4. In the Name box, type Q816182, and then click OK. By default, Form1 is created.
  5. Add a StatusBar control to Form1. By default, the control is named as statusBar1.
  6. In the Properties window of the statusBar1 control, click the Panels property, and then click the ellipsis button (...) next to the Panels property.
  7. Follow these steps in the StatusBarPanel Collection Editor dialog box:
    1. Click Add three times to add three panels to the StatusBar control. By default, the panels are named statusBarPanel1, statusBarPanel2, and statusBarPanel3.
    2. Change the value of the Style property of each panel to OwnerDraw.
    3. Click OK to close the StatusBarPanel Collection Editor dialog box.
  8. In the Properties window of the statusBar1 control, set the value of the ShowPanels property to True.
  9. Right-click Form1, and then click View Code.
  10. Declare the following variables in the Form1 class:
    private:
    	Pen *p;
    	SolidBrush *brYellowFontBrush;
    	SolidBrush *arBrushes[];
  11. Add the following code in the Form1 constructor after the call for the InitializeComponent function:
    p = new Pen(Color::Yellow);
    brYellowFontBrush = new SolidBrush(Color::Yellow);
    arBrushes = new SolidBrush *[3];
    
    arBrushes[0] = new SolidBrush (Color::Blue);
    arBrushes[1] = new SolidBrush (Color::Green);
    arBrushes[2] = new SolidBrush (Color::Pink);
    this->statusBar1->DrawItem += new System::Windows::Forms::StatusBarDrawItemEventHandler(this,statusBar1_DrawItem);
    
  12. Replace the code in the Dispose method with the following code:
    if( disposing )
    {
    	if (components != NULL) 
    	{
    		components->Dispose();
    	}
    }
    p->Dispose();
    brYellowFontBrush->Dispose();
    
    int i;
    for (i = 0; i < arBrushes->Length; i++)
    	arBrushes[i]->Dispose();
    
    __super::Dispose(disposing);
  13. Add the following code in Form1 class:
    private: void statusBar1_DrawItem(Object *sender, System::Windows::Forms::StatusBarDrawItemEventArgs *sbdevent)
    {
    	Graphics *g = sbdevent->Graphics;
    	StatusBar *sb = static_cast<StatusBar*>(sender);
    
    	float BoundsX = float(sbdevent->Bounds.X);
    	float BoundsY = float(sbdevent->Bounds.Y);
    	float BoundsWidth = float(sbdevent->Bounds.Width);
    	float BoundsHeight = float(sbdevent->Bounds.Height);
    	
    	RectangleF rectf = System::Drawing::RectangleF(BoundsX, BoundsY, BoundsWidth, BoundsHeight);
    	
    	g->DrawRectangle(p, sbdevent->Bounds);
    	sbdevent->Graphics->FillRectangle(arBrushes[sbdevent->Index], rectf);
    	g->DrawString("Panel", sb->Font, brYellowFontBrush, rectf);
    }
    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 option, visit the following Microsoft Web site:

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

  14. Press CTRL+SHIFT+B to build the solution.
  15. Press CTRL+F5 to run the solution.
back to the top

Modification Type:MajorLast Reviewed:1/12/2006
Keywords:kbStatBar kbGDIPlus kbCtrl kbHOWTOmaster KB816182 kbAudDeveloper kbAudITPRO