'//----------------------------------------------------------------------
'//【楕円形ボタン・コントロール】- OvalButton
'//
'//
'//                                                       by Masato Hirai         
'//----------------------------------------------------------------------
Imports System
Imports System.Drawing
Imports System.Drawing.Text
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.ComponentModel

<ToolboxBitmap(GetType(OvalButton), "OvalButtonToolbox.bmp")> _
Public Class OvalButton : Inherits Button
  '//
  '// コンストラクタ
  '//
  Public Enum FaceColorConstant
    Gray = 0
    Green = 1
    Red = 2
    Yellow = 3
    Blue = 4
    Magenta = 5
  End Enum
  Private fc As FaceColorConstant
  Private isPress As Boolean = False
  Private img As Bitmap
  '//
  '// コンストラクタ
  '//
  Public Sub New()
    MyBase.New()
    Me.SetStyle(ControlStyles.ResizeRedraw _
             Or ControlStyles.Opaque _
             Or ControlStyles.AllPaintingInWmPaint _
             Or ControlStyles.UserPaint _
             Or ControlStyles.DoubleBuffer, True)
   img = New Bitmap(GetType(OvalButton), "ButtonFace_Gray.png")
  End Sub
  '//
  '// OnPaint イベント
  '//
  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    '背景の描画
    MyBase.OnPaintBackground(e)
    'ボタンの表面を描画
    If isPress Then
      e.Graphics.DrawImage(img, -2, -2, Me.Width + 2, Me.Height + 2)
    Else
      e.Graphics.DrawImage(img, -4, -4, Me.Width + 6, Me.Height + 6)
    End If
    'テキストの描画
    Dim fmt As New StringFormat
    fmt.Alignment = StringAlignment.Center
    fmt.LineAlignment = StringAlignment.Center
    Dim f As Font
    If isPress Then
      If Me.Focused Then
        f = New Font(Me.Font.FontFamily, Me.Font.Size, Me.Font.Style Or FontStyle.Underline, GraphicsUnit.Point)
      Else
        f = New Font(Me.Font.FontFamily, Me.Font.Size, Me.Font.Style, GraphicsUnit.Point)
      End If
      e.Graphics.DrawString(Me.Text, f, _
        New SolidBrush(Me.ForeColor), _
        New RectangleF(2, 2, Me.Width, Me.Height), fmt)
    Else
      If Me.Focused Then
        f = New Font(Me.Font.FontFamily, Me.Font.Size, Me.Font.Style Or FontStyle.Underline, GraphicsUnit.Point)
      Else
        f = New Font(Me.Font.FontFamily, Me.Font.Size, Me.Font.Style, GraphicsUnit.Point)
      End If
      e.Graphics.DrawString(Me.Text, f, _
        New SolidBrush(Me.ForeColor), _
        New RectangleF(0, 0, Me.Width, Me.Height), fmt)
    End If
  End Sub
  '//
  '// Resize イベント
  '//
  Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
    MyBase.OnResize(e)
    Dim pth As New System.Drawing.Drawing2D.GraphicsPath
    pth.AddEllipse(Me.ClientRectangle)
    Me.Region = New Region(pth)
  End Sub
  '//
  '// MouseDown イベント
  '//
  Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
    MyBase.OnMouseDown(e)
    isPress = True
    Me.Invalidate()
  End Sub
  '//
  '// MouseUp イベント
  '//
  Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
    MyBase.OnMouseUp(e)
    isPress = False
    Me.Invalidate()
  End Sub
  '//
  '// KeyDown イベント
  '//
  Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
    MyBase.OnKeyDown(e)
    If e.KeyCode = Keys.Space Then
      isPress = True
      Me.Invalidate()
    End If
  End Sub
  '//
  '// KeyUp イベント
  '//
  Protected Overrides Sub OnKeyUp(ByVal e As KeyEventArgs)
    MyBase.OnKeyUp(e)
    isPress = False
    Me.Invalidate()
  End Sub
  '//
  '// FaceColor プロパティ
  '//
  <Browsable(True), DefaultValue(0)> _
  Public Property FaceColor() As FaceColorConstant
    Get
      Return fc
    End Get
    Set(ByVal Value As FaceColorConstant)
      fc = Value
      Dim resName As String
      resName = "ButtonFace_" & [Enum].GetName(GetType(FaceColorConstant), fc) & ".png"
      img.Dispose()
      img = New Bitmap(GetType(OvalButton), resName)
      Me.Invalidate()
    End Set
  End Property
End Class
