言語フィルタ:
概要
コントロールの境界線を描画する方法を紹介します。
境界線を描画すると次のようになります。
| Solid |
Dash |
 |
 |
対象コントロール
- System.Windows.Forms.Control から派生しているクラス
解説
コントロールに描画する方法がわからない方は、まず「コントロールの詳細を描画する」を参照してください。
境界線を描画するには Graphics.DrawRectangle メソッドを使用するだけです。Graphics.DrawRectangle メソッドに渡す Pen クラスの DashStyle プロパティを変更することで境界線のスタイルを変更することが出来ます。
一部のコントロールには既に境界線を表示出来るものがあります。その場合は境界線のスタイルを示す BorderStyle プロパティを Shadows キーワード(C# では new)を使用して隠蔽することで、独自の境界線のスタイルとして使用することが出来るようになります。
通常、境界線は非クライアント領域に描画されるものです。しかし、非クライアント領域に対する操作は OS 側で行うため、プログラム側からは操作することが出来ません。そのため、クライアント領域に境界線を描画します。
クライント領域に描画すると Panel などのコンテナでは、内部のコントロールがドッキングされた場合に境界線の上にコントロールが位置づけられてしまいます。そうならないためには Control.Padding プロパティの値を境界線の幅と等しい値にすることでコントロールが境界線の上に位置づけられないようにすることが出来ます。
ソースコード
Panel コントロールに独自の境界線を描画するサンプルを紹介します。
Panel コントロールは既に境界線を表示できるため BorderStyle プロパティを隠蔽し、独自の境界線スタイルとして使用しています。境界線のスタイルとして Pen クラスの DashStyle プロパティで描画できる破線を、全て描画できるようにしています。
境界線の色を示す BorderColor プロパティと、境界線の幅を示す BorderWidth プロパティを追加しています。
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Namespace Extentions
Public Enum BorderStyle As Integer
None = 0
Dash = 2
DashDot = 4
DashDotDot = 5
Dot = 3
Solid = 1
End Enum
Public Class BorderControl
Inherits System.Windows.Forms.Panel
Public Sub New()
MyBase.BorderStyle = System.Windows.Forms.BorderStyle.None
Me._BorderColor = Color.Black
Me._BorderStyle = Extentions.BorderStyle.None
Me._BorderWidth = 1
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
If Me.BorderStyle <> Extentions.BorderStyle.None Then
Using p As New Pen(Me.BorderColor, Me.BorderWidth)
p.DashStyle = Me.ConvertToDashStyle(Me.BorderStyle)
Dim r As Rectangle = Me.ClientRectangle
r.X += r.X + Convert.ToInt32(Decimal.Floor(Convert.ToDecimal(Me.BorderWidth / 2)))
r.Y += r.Y + Convert.ToInt32(Decimal.Floor(Convert.ToDecimal(Me.BorderWidth / 2)))
r.Width -= Me.BorderWidth
r.Height -= Me.BorderWidth
e.Graphics.DrawRectangle(p, r)
End Using
End If
End Sub
Private Function ConvertToDashStyle(ByVal style As Extentions.BorderStyle) As DashStyle
Return DirectCast(style - 1, DashStyle)
End Function
Private _BorderColor As Color
<Category("表示")> _
<DefaultValue(GetType(Color), "Black")> _
<Description("コントロールの境界線色を取得または設定します。")> _
Public Property BorderColor() As Color
Get
Return Me._BorderColor
End Get
Set(ByVal value As Color)
Me._BorderColor = value
Me.Invalidate()
End Set
End Property
Private _BorderStyle As BorderStyle
<Category("表示")> _
<DefaultValue(GetType(BorderStyle), "None")> _
<Description("コントロールの境界線スタイルを取得または設定します。")> _
Public Shadows Property BorderStyle() As BorderStyle
Get
Return Me._BorderStyle
End Get
Set(ByVal value As BorderStyle)
Me._BorderStyle = value
Me.Invalidate()
End Set
End Property
Private _BorderWidth As Integer
<Category("表示")> _
<DefaultValue(1)> _
<Description("コントロールの境界線の幅を取得または設定します。")> _
Public Property BorderWidth() As Integer
Get
Return Me._BorderWidth
End Get
Set(ByVal value As Integer)
Me._BorderWidth = 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
{
public enum BorderStyle : int
{
None = 0,
Dash = 2,
DashDot = 4,
DashDotDot = 5,
Dot = 3,
Solid = 1,
}
class BorderDraw : System.Windows.Forms.Panel
{
public BorderDraw()
{
base.BorderStyle = System.Windows.Forms.BorderStyle.None;
this._BorderColor = Color.Black;
this._BorderStyle = Extentions.BorderStyle.None;
this._BorderWidth = 1;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.BorderStyle != BorderStyle.None)
{
using (Pen p = new Pen(this.BorderColor, this.BorderWidth))
{
p.DashStyle = this.ConvertToDashStyle(this.BorderStyle);
Rectangle r = this.ClientRectangle;
r.X += r.X + Convert.ToInt32(decimal.Floor(Convert.ToDecimal(this.BorderWidth / 2)));
r.Y += r.Y + Convert.ToInt32(decimal.Floor(Convert.ToDecimal(this.BorderWidth / 2)));
r.Width -= this.BorderWidth;
r.Height -= this.BorderWidth;
e.Graphics.DrawRectangle(p, r);
}
}
}
private DashStyle ConvertToDashStyle(Extentions.BorderStyle style)
{
return (DashStyle)style - 1;
}
private Color _BorderColor;
[Category("表示")]
[DefaultValue(typeof(Color), "Black")]
[Description("コントロールの境界線色を取得または設定します。")]
public Color BorderColor
{
get { return this._BorderColor; }
set
{
this._BorderColor = value;
this.Invalidate();
}
}
private BorderStyle _BorderStyle;
[Category("表示")]
[DefaultValue(typeof(BorderStyle), "None")]
[Description("コントロールの境界線スタイルを取得または設定します。")]
public new BorderStyle BorderStyle
{
get { return this._BorderStyle; }
set
{
this._BorderStyle = value;
this.Invalidate();
}
}
private int _BorderWidth;
[Category("表示")]
[DefaultValue(1)]
[Description("コントロールの境界線の幅を取得または設定します。")]
public int BorderWidth
{
get { return this._BorderWidth; }
set
{
this._BorderWidth = value;
this.Invalidate();
}
}
}
}