I don't know how simple it is. Here is a basic class that changes the
region of a button to a hexagon shape. The values that are used for
the points array are hardcoded for a button that is 40 x 40 pixels.
To refine this class, you would have to add code to paint the button
appropriately, override the OnResize event so that you can change the
region when the button changes size, etc.
Hope this gives you some ideas
Chris
Public Class HexButton
Inherits Button
Private _bRegionSet As Boolean = False
Public Sub New()
End Sub
Protected Overrides Sub OnPaint(ByVal pevent As
System.Windows.Forms.PaintEventArgs)
If Not _bRegionSet Then
setRegion()
_bRegionSet = True
End If
'pevent.Graphics.FillRegion(Brushes.Blue, Me.Region)
MyBase.OnPaint(pevent)
End Sub
Private Sub setRegion()
Dim gp As New Drawing2D.GraphicsPath()
Dim hexPoints As PointF() = _
{New PointF(17.3, 0), _
New PointF(34.6, 10), _
New PointF(34.6, 30), _
New PointF(17.3, 40), _
New PointF(0, 30), _
New PointF(0, 10)}
gp.AddPolygon(hexPoints)
gp.CloseFigure()
Dim rgn As New Region(gp)
Me.Region = rgn
rgn.Dispose()
gp.Dispose()
End Sub
End Class
Here's a little bit better class that at least allows for resizing.
You still will have to adjust the painting to customize its look.
Also, there is no visual indication when the button is clicked. You
will have to do that as well. Watch for typos.
Chris
'**** BEGIN CODE HERE
Imports System.Drawing.Drawing2D
Public Class HexButton
Inherits Button
'Set the styles so we can control the painting
Public Sub New()
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.UserPaint Or _
ControlStyles.OptimizedDoubleBuffer, True)
End Sub
'Paint the button
Protected Overrides Sub OnPaint(ByVal pevent As
System.Windows.Forms.PaintEventArgs)
'First set the region
If Me.Region Is Nothing Then
setRegion()
End If
'Get the points that correspond to our hex shape
'These are the same points used to create the region
Dim hexPoints As PointF() = getHexPoints()
'Create a GraphicsPath
Using gp As GraphicsPath = getPath(hexPoints)
'Create a Pen to draw with
Using p As New Pen(Color.Red, 5)
'Draw the background of the button
pevent.Graphics.FillPath(Brushes.Blue, gp)
'Draw the outline of the button
pevent.Graphics.DrawPolygon(p, hexPoints)
'Draw the text for the button
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
sf.Trimming = StringTrimming.Word
Dim rc As New RectangleConverter
pevent.Graphics.DrawString(Me.Text, Me.Font,
Brushes.White, Me.ClientRectangle, sf)
End Using
End Using
End Sub
'When the button is resized, it makes sure the button is a square
in shape and sets the new region
Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
Me.Height = Me.Width
setRegion()
End Sub
'Sets the button's region. Disposing of any existing region
Private Sub setRegion()
'Get the button's current region, if any
Dim tmpRegion As Region = Me.Region
'Set the button's new region
Me.Region = getRegion()
'Dispose of the old region
If tmpRegion IsNot Nothing Then
tmpRegion.Dispose()
End If
End Sub
'Creates a Region object from a GraphicsPath
Private Function getRegion() As Region
Dim rgn As Region = Nothing
Using gp As GraphicsPath = getPath()
rgn = New Region(gp)
End Using
Return rgn
End Function
Private Function getRegion(ByVal gp As GraphicsPath) As Region
Return New Region(gp)
End Function
'Creates a GraphicsPath object from an array of points
Private Function getPath() As GraphicsPath
Dim gp As New GraphicsPath()
gp.AddPolygon(getHexPoints())
gp.CloseFigure()
Return gp
End Function
Private Function getPath(ByVal hexPoints As PointF()) As
GraphicsPath
Dim gp As New GraphicsPath()
gp.AddPolygon(hexPoints)
gp.CloseFigure()
Return gp
End Function
'Uses the button's width, to calculate the points needed for the
hex shape
Private Function getHexPoints() As PointF()
Dim d As Single
d = CSng((Me.Width * Math.Sqrt(3)) / 4)
Dim hexPoints As PointF() = _
{New PointF(CSng(Me.Width / 2), 0), _
New PointF(CSng(Me.Width / 2) + d, CSng(Me.Width / 4)), _
New PointF(CSng(Me.Width / 2) + d, CSng(3 * (Me.Width /
4))), _
New PointF(CSng(Me.Width / 2), Me.Width), _
New PointF(CSng(Me.Width / 2) - d, CSng(3 * (Me.Width /
4))), _
New PointF(CSng(Me.Width / 2) - d, CSng(Me.Width / 4))}
Return hexPoints
End Function
End Class