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 TASKSUMMARYThis 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 topRequirementsThis
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 topCreate 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.
- Follow these steps in the StatusBarPanel Collection
Editor dialog box:
- Click Add three times to add three
panels to the StatusBar control. By default, the panels
are named statusBarPanel1, statusBarPanel2, and statusBarPanel3.
- Change the value of the Style property of each
panel to OwnerDraw.
- Click OK to close the
StatusBarPanel Collection Editor dialog box.
- In the Properties window of the statusBar1 control, set the value of the ShowPanels property to
True.
- Right-click Form1, and then click
View Code.
- Declare the following variables in the Form1 class:
private:
Pen *p;
SolidBrush *brYellowFontBrush;
SolidBrush *arBrushes[]; - 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);
- 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); - 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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- 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: - Press CTRL+SHIFT+B to build the
solution.
- Press CTRL+F5 to run the
solution.
back to the
top
Modification Type: | Major | Last Reviewed: | 1/12/2006 |
---|
Keywords: | kbStatBar kbGDIPlus kbCtrl kbHOWTOmaster KB816182 kbAudDeveloper kbAudITPRO |
---|
|