Microsoft KB Archive/816182

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

Article ID: 816182

Article Last Modified on 1/12/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-





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
 * Requirements
 * Steps to Create the Sample



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
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. 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. In the Name box, type Q816182, and then click OK. By default, Form1 is created. Add a StatusBar control to Form1. By default, the control is named as statusBar1. In the Properties window of the statusBar1 control, click the Panels property, and then click the ellipsis button (...) next to the Panels property.</li> Follow these steps in the StatusBarPanel Collection Editor dialog box: <ol style="list-style-type: lower-alpha;"> Click Add three times to add three panels to the StatusBar control. By default, the panels are named statusBarPanel1, statusBarPanel2, and statusBarPanel3.</li> Change the value of the Style property of each panel to OwnerDraw.</li> Click OK to close the StatusBarPanel Collection Editor dialog box.</li></ol> </li> In the Properties window of the statusBar1 control, set the value of the ShowPanels property to True .</li> Right-click Form1, and then click View Code.</li>  Declare the following variables in the Form1 class: private: Pen *p; SolidBrush *brYellowFontBrush; SolidBrush *arBrushes[]; </li>  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); </li>  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); </li>  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(&quot;Panel&quot;, 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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

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

</li> Press CTRL+SHIFT+B to build the solution.</li> Press CTRL+F5 to run the solution.</li></ol>

back to the top

<div class="idea_section">

Keywords: kbhowtomaster kbstatbar kbgdiplus kbctrl KB816182

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.