public StatusBarPanelEx() { _foreColor = Color.Black; Style = StatusBarPanelStyle.OwnerDraw; }
public void Paint( Graphics g, Rectangle bounds ) { if( Parent != null ) { // Draw a blue background in the owner-drawn panel. g.FillRectangle(Brushes.AliceBlue, e.Bounds);
// Create a StringFormat object to align text in // the panel and create the fore color brush. using( StringFormat textFormat = new StringFormat() ) using( Brush brush = new SolidBrush( ForeColor )) { textFormat.LineAlignment = StringAlignment.Center; textFormat.Alignment = StringAlignment.Center;
// Draw the panel's text in dark blue using the Panel // and Bounds properties of the StatusBarEventArgs object // and the StringFormat object. g.DrawString( Text, Parent.Font, brush, bounds, textFormat); } } } }
How to change the font colour of StatusBarPanel
Rajesh77
Here is a little example:
Interface and inhired class
public interface IOwnerDrawPanel
{
void Paint( Graphics g, Rectangle bounds );
}
public class StatusBarPanelEx : StatusBarPanel, IOwnerDrawPanel
{
private Color _foreColor;
public Color ForeColor
{
get
{
return _foreColor;
}
set
{
if( !_foreColor.Equals( value ) )
{
_foreColor = value;
if( Parent != null )
{
Parent.Invalidate();
}
}
}
}
public StatusBarPanelEx()
{
_foreColor = Color.Black;
Style = StatusBarPanelStyle.OwnerDraw;
}
public void Paint( Graphics g, Rectangle bounds )
{
if( Parent != null )
{
// Draw a blue background in the owner-drawn panel.
g.FillRectangle(Brushes.AliceBlue, e.Bounds);
// Create a StringFormat object to align text in
// the panel and create the fore color brush.
using( StringFormat textFormat = new StringFormat() )
using( Brush brush = new SolidBrush( ForeColor ))
{
textFormat.LineAlignment = StringAlignment.Center;
textFormat.Alignment = StringAlignment.Center;
// Draw the panel's text in dark blue using the Panel
// and Bounds properties of the StatusBarEventArgs object
// and the StringFormat object.
g.DrawString( Text, Parent.Font, brush, bounds, textFormat);
}
}
}
}
DrawItem event code
private void statusBar1_DrawItem(object sender, StatusBarDrawItemEventArgs sbdevent)
{
IOwnerDrawPanel ownerDrawPanel = sbdevent.Panel as IOwnerDrawPanel;
if( ownerDrawPanel != null )
{
ownerDrawPanel.Paint( sbdevent.Graphics, sbdevent.Bounds );
}
}