Polygons in VS8?

  • Thread starter Thread starter Number 11950 - GPEMC! Replace number with 11950
  • Start date Start date
N

Number 11950 - GPEMC! Replace number with 11950

Are polygonal buttons or perhaps polygons with tooltips and and click events
available in VS8 or is it too early yet?

Thanks in advance...
 
Are polygonal buttons or perhaps polygons with tooltips and and click events
available in VS8 or is it too early yet?

Thanks in advance...

--
Timothy Casey GPEMC! >> 11950 is the (e-mail address removed) 2email
Terms & conditions apply. Seewww.fieldcraft.biz/GPEMC
Discover valid interoperable web menus, IE security, TSR Control,
& the most advanced speed reading application @www.fieldcraft.biz

You can assign a polygonal region to a button to change its shape, so
in a sense, yes they do exist.

Chris
 
Chris Dunaway said:
You can assign a polygonal region to a button to change its shape, so
in a sense, yes they do exist.

Chris

So is there a relatively simple way to display an interlocking grid of
polygons (eg hexagons), each with its own tooltip and click response?

Thanks in Advance...
 
So is there a relatively simple way to display an interlocking grid of
polygons (eg hexagons), each with its own tooltip and click response?

Thanks in Advance...

--
Timothy Casey GPEMC! >> 11950 is the (e-mail address removed) 2email
Terms & conditions apply. Seewww.fieldcraft.biz/GPEMC
Discover valid interoperable web menus, IE security, TSR Control,
& the most advanced speed reading application @www.fieldcraft.biz

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
 
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
 
<snip>

Ok, I couldn't leave well enough alone. I changed it again, so that
you can set the backcolor, forecolor and I added properties for
BorderColor and BorderWidth.

Chris

Imports System.Drawing.Drawing2D

Public Class HexButton
Inherits Button

Public Sub New()
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or _
ControlStyles.UserPaint Or _
ControlStyles.OptimizedDoubleBuffer, True)

_borderColor = Me.ForeColor
_borderWidth = 3
End Sub

Protected Overrides Sub OnPaint(ByVal pevent As
System.Windows.Forms.PaintEventArgs)

If Me.Region Is Nothing Then
setRegion()
End If

Dim hexPoints As PointF() = getHexPoints()

Using gp As GraphicsPath = getPath(hexPoints)
Using p As New Pen(Me.BorderColor, Me.BorderWidth)

Using br As New SolidBrush(Me.BackColor)
pevent.Graphics.FillPath(br, gp)
End Using

pevent.Graphics.DrawPolygon(p, hexPoints)

Dim sf As New StringFormat

sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
sf.Trimming = StringTrimming.Word

Dim rc As New RectangleConverter

Using br As New SolidBrush(Me.ForeColor)
pevent.Graphics.DrawString(Me.Text, Me.Font, br,
Me.ClientRectangle, sf)
End Using
End Using
End Using

End Sub

Private _borderWidth As Single
Public Property BorderWidth() As Single
Get
Return _borderWidth
End Get
Set(ByVal value As Single)
If value <> _borderWidth Then
_borderWidth = value
Me.Refresh()
End If
End Set
End Property

Private _borderColor As Color
Public Property BorderColor() As Color
Get
Return _borderColor
End Get
Set(ByVal value As Color)
If value <> _borderColor Then
_borderColor = value
Me.Refresh()
End If
End Set
End Property

Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
Me.Height = Me.Width
setRegion()
End Sub

Private Sub setRegion()
Dim tmpRegion As Region = Me.Region
Me.Region = getRegion()

If tmpRegion IsNot Nothing Then
tmpRegion.Dispose()
End If
End Sub

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

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

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
 
Chris Dunaway said:
<snip>

Ok, I couldn't leave well enough alone. I changed it again, so that
you can set the backcolor, forecolor and I added properties for
BorderColor and BorderWidth.

Chris
[SNIP]

I can see there is a lot of new syntax I need to learn. Thank you very much
for showing me...
 
Back
Top