Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Square
Inherits System.Windows.Forms.UserControl
#Region " Windows Form Designer generated code "
Private rec As Rectangle
Private gp As GraphicsPath
Public Enum PieceType
Empty
Black
BlackPotential
White
WhitePotential
End Enum
'most new squares will empty, so set it to empty
Private _piece As Square.PieceType = PieceType.Empty
Public Property Piece() As PieceType
Get
Return _piece
End Get
Set(ByVal Value As PieceType)
_piece = Value
Refresh()
End Set
End Property
Private Sub Square_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
e.Graphics.Clear(Me.BackColor)
'Draw a border
e.Graphics.DrawRectangle(Pens.Black, Me.ClientRectangle)
Dim brGrad As PathGradientBrush
brGrad = New Drawing2D.PathGradientBrush(gp)
'to give 3D effect, move the centerpoint
brGrad.CenterPoint = New PointF(CSng(rec.Right - 2), CSng(rec.Top - 2))
Select Case _piece
Case Is = PieceType.Empty
Exit Sub
Case Is = PieceType.Black
brGrad.CenterColor = Color.Gray
brGrad.SurroundColors = New Color() {Color.DarkGray, Color.Black}
Case Is = PieceType.BlackPotential
brGrad.CenterColor = Color.FromArgb(80, Color.Black)
brGrad.SurroundColors = New Color() {Color.FromArgb(80, Color.Black)}
Case Is = PieceType.White
brGrad.CenterColor = Color.LightSteelBlue
brGrad.SurroundColors = New Color() {Color.LightGray, Color.White}
Case Is = PieceType.WhitePotential
brGrad.CenterColor = Color.FromArgb(120, Color.White)
brGrad.SurroundColors = New Color() {Color.FromArgb(120, Color.White)}
End Select
e.Graphics.FillEllipse(brGrad, rec) 'Me.ClientRectangle)
brGrad.Dispose()
End Sub
Private Sub Square_Resize(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Resize
SetGraphicsObjects()
Refresh()
End Sub
Private Sub SetGraphicsObjects()
'make the circular piece a little smaller than the square
rec = New Rectangle(2, 2, Width - 4, Height - 4)
'set the graphics path for the PathGradientBrush
gp = New GraphicsPath()
gp.AddEllipse(Me.ClientRectangle)
End Sub
End Class