DOBON.NET

ステータスバーのパネルの色とフォントを変更する

StatusStripコントロールの場合

.NET Framework 2.0から追加されたStatusStripでは、パネルの色やフォントを変更するのは簡単です。ToolStripStatusLabelをパネルとして使用しているならば、Fontプロパティでフォントを変更できます。また、ForeColorプロパティで前景色を、BackColorプロパティで背景色を変更できます。

StatusBarコントロールの場合

StatusBarのパネルの色とフォントを変更する方法は、マイクロソフトサポート技術情報の「HOW TO: Change the Color and the Font of the StatusBarPanel Object by Using Visual Basic .NET」「HOW TO: Change the Color and the Font of the StatusBarPanel Object by Using Visual C# .NET」などで紹介されています。

ここでは専用のクラスを作成する方法を紹介します。まず、StatusBarPanelクラスから派生した新しいクラス(ColorPanelクラス)を作成します。

VB.NET
コードを隠すコードを選択
Imports System
Imports System.Drawing
Imports System.Windows.Forms

''' <summary>
''' 色とフォントを変更できるStatusBarのPanel
''' </summary>
Public Class ColorPanel
    Inherits StatusBarPanel

    Private _font As Font = Nothing
    ''' <summary>
    ''' 使用するフォント
    ''' </summary>
    Public Property Font() As Font
        'StatusBarを再描画する
        Get
            Return _font
        End Get
        Set(ByVal Value As Font)
            _font = Value
            If Not (Me.Parent Is Nothing) Then
                Me.Parent.Refresh()
            End If
        End Set
    End Property

    Private _foreColor As Color = Color.Empty
    ''' <summary>
    ''' 前景色
    ''' </summary>
    Public Property ForeColor() As Color
        'StatusBarを再描画する
        Get
            Return _foreColor
        End Get
        Set(ByVal Value As Color)
            _foreColor = Value
            If Not (Me.Parent Is Nothing) Then
                Me.Parent.Refresh()
            End If
        End Set
    End Property

    Private _backColor As Color = Color.Empty
    ''' <summary>
    ''' 背景色
    ''' </summary>
    Public Property BackColor() As Color
        'StatusBarを再描画する
        Get
            Return _backColor
        End Get
        Set(ByVal Value As Color)
            _backColor = Value
            If Not (Me.Parent Is Nothing) Then
                Me.Parent.Refresh()
            End If
        End Set
    End Property

    ''' <summary>
    ''' コンストラクタ
    ''' </summary>
    ''' <param name="sb">パネルを追加するStatusBar</param>
    Public Sub New(ByVal sb As StatusBar)
        'オーナードローを指定する
        Me.Style = StatusBarPanelStyle.OwnerDraw

        'StatusBarのDrawItemイベントハンドラの追加
        AddHandler sb.DrawItem, AddressOf sb_DrawItem
    End Sub

    Private Sub sb_DrawItem(ByVal sender As Object, _
            ByVal sbdevent As StatusBarDrawItemEventArgs)
        '描画するパネルが自分自身か確かめる
        If sbdevent.Panel.Equals(Me) Then
            '文字の配置位置を決定
            Dim sf As New StringFormat
            If Me.Alignment = HorizontalAlignment.Left Then
                sf.Alignment = StringAlignment.Near
            Else If Me.Alignment = HorizontalAlignment.Center Then
                sf.Alignment = StringAlignment.Center
            Else If Me.Alignment = HorizontalAlignment.Right Then
                sf.Alignment = StringAlignment.Far
            End If
            sf.LineAlignment = StringAlignment.Center
            '前景色、背景色のブラシを作成
            Dim foreBrush As Brush
            If _foreColor.IsEmpty = False Then
                foreBrush = New SolidBrush(_foreColor)
            Else
                foreBrush = New SolidBrush(sbdevent.ForeColor)
            End If
            Dim backBrush As Brush
            If _backColor.IsEmpty = False Then
                backBrush = New SolidBrush(_backColor)
            Else
                backBrush = New SolidBrush(sbdevent.BackColor)
            End If
            '使用するフォントを決定
            Dim fnt As Font
            If Not (_font Is Nothing) Then
                fnt = _font
            Else
                fnt = sbdevent.Font
            End If
            '背景を描画
            sbdevent.Graphics.FillRectangle(backBrush, sbdevent.Bounds)
            '文字列を描画
            Dim rectf As New RectangleF( _
                sbdevent.Bounds.X, sbdevent.Bounds.Y, _
                sbdevent.Bounds.Width, sbdevent.Bounds.Height)
            sbdevent.Graphics.DrawString(Me.Text, fnt, foreBrush, rectf, sf)

            '破棄
            foreBrush.Dispose()
            backBrush.Dispose()
        End If
    End Sub
End Class
C#
コードを隠すコードを選択
using System;
using System.Drawing;
using System.Windows.Forms;

/// <summary>
/// 色とフォントを変更できるStatusBarのPanel
/// </summary>
public class ColorPanel : StatusBarPanel
{
    private Font _font = null;
    /// <summary>
    /// 使用するフォント
    /// </summary>
    public Font Font
    {
        set
        {
            _font = value;
            //StatusBarを再描画する
            if (this.Parent != null)
            {
                this.Parent.Refresh();
            }
        }
        get { return _font; }
    }

    private Color _foreColor = Color.Empty;
    /// <summary>
    /// 前景色
    /// </summary>
    public Color ForeColor
    {
        set
        {
            _foreColor = value;
            //StatusBarを再描画する
            if (this.Parent != null)
            {
                this.Parent.Refresh();
            }
        }
        get { return _foreColor; }
    }

    private Color _backColor = Color.Empty;
    /// <summary>
    /// 背景色
    /// </summary>
    public Color BackColor
    {
        set
        {
            _backColor = value;
            //StatusBarを再描画する
            if (this.Parent != null)
            {
                this.Parent.Refresh();
            }
        }
        get { return _backColor; }
    }

    /// <summary>
    /// コンストラクタ
    /// </summary>
    /// <param name="sb">パネルを追加するStatusBar</param>
    public ColorPanel(StatusBar sb)
    {
        //オーナードローを指定する
        this.Style = StatusBarPanelStyle.OwnerDraw;

        //StatusBarのDrawItemイベントハンドラの追加
        sb.DrawItem +=
            new StatusBarDrawItemEventHandler(sb_DrawItem);
    }

    private void sb_DrawItem(object sender,
        StatusBarDrawItemEventArgs sbdevent)
    {
        //描画するパネルが自分自身か確かめる
        if (sbdevent.Panel == this)
        {
            //文字の配置位置を決定
            StringFormat sf = new StringFormat();
            if (this.Alignment == HorizontalAlignment.Left)
                sf.Alignment = StringAlignment.Near;
            else if (this.Alignment == HorizontalAlignment.Center)
                sf.Alignment = StringAlignment.Center;
            else if (this.Alignment == HorizontalAlignment.Right)
                sf.Alignment = StringAlignment.Far;
            sf.LineAlignment = StringAlignment.Center;

            //前景色、背景色のブラシを作成
            Brush foreBrush;
            if (_foreColor != Color.Empty)
                foreBrush = new SolidBrush(_foreColor);
            else
                foreBrush = new SolidBrush(sbdevent.ForeColor);
            Brush backBrush;
            if (_backColor != Color.Empty)
                backBrush = new SolidBrush(_backColor);
            else
                backBrush = new SolidBrush(sbdevent.BackColor);
            //使用するフォントを決定
            Font fnt;
            if (_font != null)
                fnt = _font;
            else
                fnt = sbdevent.Font;

            //背景を描画
            sbdevent.Graphics.FillRectangle(
                backBrush, sbdevent.Bounds);
            //文字列を描画
            sbdevent.Graphics.DrawString(
                this.Text, fnt, foreBrush, sbdevent.Bounds, sf);

            //破棄
            foreBrush.Dispose();
            backBrush.Dispose();
        }
    }
}

このクラスを使用してステータスバーに、前景色が赤、背景色が黄色、フォントがMS 明朝の太字で文字列が表示されるパネルを追加する例を以下に示します。

VB.NET
コードを隠すコードを選択
'パネルを表示する
StatusBar1.ShowPanels = True

'ColorPanelの作成
Dim cp As New ColorPanel(StatusBar1)
'プロパティを変更する
cp.ForeColor = Color.Red
cp.BackColor = Color.White
cp.Font = New Font("MS 明朝", 9, FontStyle.Bold)
cp.Text = "テストです"
'StatusBar1のパネルに追加する
StatusBar1.Panels.Add(cp)
C#
コードを隠すコードを選択
//パネルを表示する
StatusBar1.ShowPanels = true;

//ColorPanelの作成
ColorPanel cp =
    new ColorPanel(StatusBar1);
//プロパティを変更する
cp.ForeColor = Color.Red;
cp.BackColor = Color.White;
cp.Font = new Font("MS 明朝", 9, FontStyle.Bold);
cp.Text = "テストです";
//StatusBar1のパネルに追加する
StatusBar1.Panels.Add(cp);

注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。

  • このサイトで紹介されているコードの多くは、例外処理が省略されています。例外処理については、こちらをご覧ください。
  • イベントハンドラの意味が分からない、C#のコードをそのまま書いても動かないという方は、こちらをご覧ください。
  • .NET Tipsをご利用いただく際は、注意事項をお守りください。
共有する

この記事への評価

この記事へのコメント

この記事に関するコメントを投稿するには、下のボタンをクリックしてください。投稿フォームへ移動します。通常のご質問、ご意見等は掲示板へご投稿ください。