言語フィルタ:
概要
コントロールの背景を描画する方法を紹介します。
背景にパターンを描画すると次のようになります。
| Horizontal |
BackwardDiagonal |
 |
 |
対象コントロール
- System.Windows.Forms.Control から派生しているクラス
解説
コントロールの背景を描画するには OnPaintBackground メソッドをオーバーライドします。OnPaintBackground メソッドの PaintEventArgs 引数の Graphics プロパティを使用して描画することで、コントロールの背景を描画することができます。たとえば、背景にグラデーションをかけたり、パターンを描画することができます。
OnPaintBackground メソッドではコントロール全体を描画することができますが、背景のみ描画するようにします。コントロールのテキストや枠線、画像などの詳細は OnPaint メソッドなどで描画します。詳しくは「コントロールの詳細を描画する」を参照してください。
一部のコントロールは OnPaintBackground メソッドが呼ばれません。そのコントロールには別の手段で描画する方法が用意されている場合があります。詳細については、そのコントロールのページで紹介します。
ソースコード
PictureBox コントロールの背景にパターンを描画するサンプルを紹介します。
パターンのスタイルを示す HatchStyle プロパティと、パターン色を示す HatchColor プロパティを追加しています。
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Namespace Extentions
Public Class BackgroundDraw
Inherits System.Windows.Forms.PictureBox
Public Sub New()
Me._HatchColor = Color.Black
End Sub
Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
If Me.BackColor = Color.Transparent OrElse Me.HatchColor = Color.Transparent Then
MyBase.OnPaintBackground(pevent)
End If
Using hb As New HatchBrush(Me.HatchStyle, Me.HatchColor, Me.BackColor)
pevent.Graphics.FillRectangle(hb, Me.ClientRectangle)
End Using
End Sub
Private _HatchColor As Color
<Category("表示")> _
<DefaultValue(GetType(Color), "Black")> _
<Description("コントロールの背景のパターン色です。")> _
Public Property HatchColor() As Color
Get
Return Me._HatchColor
End Get
Set(ByVal value As Color)
Me._HatchColor = value
Me.Invalidate()
End Set
End Property
Private _HatchStyle As HatchStyle
<Category("表示")> _
<DefaultValue(GetType(HatchStyle), "Horizontal")> _
<Description("コントロールの背景のパターンです。")> _
Public Property HatchStyle() As HatchStyle
Get
Return Me._HatchStyle
End Get
Set(ByVal value As HatchStyle)
Me._HatchStyle = value
Me.Invalidate()
End Set
End Property
End Class
End Namespace
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Extentions
{
class BackgroundDraw : System.Windows.Forms.PictureBox
{
public BackgroundDraw()
{
this._HatchColor = Color.Black;
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
if ((this.BackColor == Color.Transparent) || (this.HatchColor == Color.Transparent))
{
base.OnPaintBackground(pevent);
}
using (HatchBrush hb = new HatchBrush(this.HatchStyle, this.HatchColor, this.BackColor))
{
pevent.Graphics.FillRectangle(hb, this.ClientRectangle);
}
}
private Color _HatchColor;
[Category("表示")]
[DefaultValue(typeof(Color), "Black")]
[Description("コントロールの背景のパターン色です。")]
public Color HatchColor
{
get { return this._HatchColor; }
set
{
this._HatchColor = value;
this.Invalidate();
}
}
private HatchStyle _HatchStyle;
[Category("表示")]
[DefaultValue(typeof(HatchStyle), "Horizontal")]
[Description("コントロールの背景のパターンです。")]
public HatchStyle HatchStyle
{
get { return this._HatchStyle; }
set
{
this._HatchStyle = value;
this.Invalidate();
}
}
}
}