picturebox into hexagon shape
picturebox into hexagon shape
picturebox into hexagon shape
picturebox into hexagon shape
picturebox into hexagon shape
picturebox into hexagon shape picturebox into hexagon shape picturebox into hexagon shape picturebox into hexagon shape picturebox into hexagon shape picturebox into hexagon shape picturebox into hexagon shape picturebox into hexagon shape
picturebox into hexagon shape picturebox into hexagon shape
picturebox into hexagon shape
Go Back  Xtreme Visual Basic Talk > > > picturebox into hexagon shape


Reply
 
Thread Tools Display Modes
  #1  
Old 01-03-2010, 10:52 AM
daves2369 daves2369 is offline
Newcomer
 
Join Date: Jan 2010
Posts: 2
Default picturebox into hexagon shape


here is my problem i am making a blockbusters game and have come across a problem where 2 picture boxes overlap each other which i dont want
so i need to make a picture box or a button into a hexagon shape so they wont overlap

any help would be appreciated
thanks
dave
Reply With Quote
  #2  
Old 01-03-2010, 06:28 PM
boops boops's Avatar
boops boops boops boops is offline
Centurion
 
Join Date: Dec 2006
Location: Holland and France
Posts: 146
Default

Here's a class you can add to your project to provide hexagonal picture boxes. You can set its properties in code or in the designer like a normal picture box. To add it in the designer, first build your project with the new class. The HexPictureBox tool will appear in the toolbox with a cogwheel icon. It will look like a normal picture box on the form but it will be hexagonal when you run the program.

vb.net Code:
Public Class HexPictureBox Inherits PictureBox Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs) SetRegion() MyBase.OnSizeChanged(e) End Sub Private Sub SetRegion() Dim p(5) As Point Dim v As Integer = CInt(Me.Width / 2 * Math.Sin(30 * Math.PI / 180)) p(0) = New Point(Me.Width \ 2, 0) p(1) = New Point(Me.Width, v) p(2) = New Point(Me.Width, Me.Height - v) p(3) = New Point(Me.Width \ 2, Me.Height) p(4) = New Point(0, Me.Height - v) p(5) = New Point(0, v) Using gp As New Drawing2D.GraphicsPath gp.AddPolygon(p) Me.Region = New Region(gp) End Using End Sub End Class
Attached Images
File Type: png hexPictureBox.png (237.2 KB, 36 views)

Last edited by boops boops; 01-03-2010 at 06:35 PM.
Reply With Quote
  #3  
Old 01-04-2010, 03:05 AM
daves2369 daves2369 is offline
Newcomer
 
Join Date: Jan 2010
Posts: 2
Default

thanks "boops boops"
it works
just what i was looking for
Reply With Quote
  #4  
Old 01-25-2015, 02:55 PM
zacacp zacacp is offline
Newcomer
 
Join Date: Jan 2015
Posts: 6
Default What about smoothing mode?

vb.net Code:
Public Class HexPictureBox Inherits PictureBox Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs) SetRegion() MyBase.OnSizeChanged(e) End Sub Private Sub SetRegion() Dim p(5) As Point Dim v As Integer = CInt(Me.Width / 2 * Math.Sin(30 * Math.PI / 180)) p(0) = New Point(Me.Width \ 2, 0) p(1) = New Point(Me.Width, v) p(2) = New Point(Me.Width, Me.Height - v) p(3) = New Point(Me.Width \ 2, Me.Height) p(4) = New Point(0, Me.Height - v) p(5) = New Point(0, v) Using gp As New Drawing2D.GraphicsPath gp.AddPolygon(p) Me.Region = New Region(gp) End Using End Sub End Class
[/QUOTE]

Hi , the code was very usefull for me! so first i must thanks, i really appreciate people who is involve in this stuff... but the reply is getting bored so i ll go straight to the point...
How about if you add some more code to let the polygon be antialiased?

i am trying to achive this but i am bit lost...

i add this code to the class but no signal that the polygon is getting smooth =

vb.net Code:
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias = 3 MyBase.OnPaint(e) End Sub
[/QUOTE]
Reply With Quote
  #5  
Old 01-25-2015, 04:38 PM
passel's Avatar
passelpicturebox into hexagon shape passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,014
Default

You can't antiAlias a filled polygon.
antiAlias is for smoothing lines.
With a normal solid color polygon, you would draw the polygon twice, with the same bounds.
First would be using the DrawFilled method, then again with the DrawPoly method.
The DrawFilled fills the interior, and the drawpoly draws a line around the edges, and it is that line around the edges that is antialiased.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #6  
Old 01-25-2015, 05:07 PM
zacacp zacacp is offline
Newcomer
 
Join Date: Jan 2015
Posts: 6
Default

ok i understood the concept of what you say, i ll try to write it. thanks!
Reply With Quote
  #7  
Old 01-25-2015, 05:28 PM
zacacp zacacp is offline
Newcomer
 
Join Date: Jan 2015
Posts: 6
Default

This way?

vb.net Code:
Public Sub DrawPolygonPoint(ByVal e As PaintEventArgs) Dim blackPen As New Pen(Color.Black, 3) Dim v As Integer = CInt(Me.Width / 2 * Math.Sin(30 * Math.PI / 180)) Dim point1 As New Point(Me.Width \ 2, 0) Dim point2 As New Point(Me.Width, v) Dim point3 As New Point(Me.Width, Me.Height - v) Dim point4 As New Point(Me.Width \ 2, Me.Height) Dim point5 As New Point(0, Me.Height - v) Dim point6 As New Point(0, v) Dim curvePoints As Point() = {point1, point2, point3, point4, _ point5, point6} e.Graphics.DrawPolygon(blackPen, curvePoints) e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias = 2 ' Me.Region = New Region(e) ? (HOW? jaja End Sub
[/QUOTE]
Reply With Quote
  #8  
Old 01-26-2015, 10:44 PM
passel's Avatar
passelpicturebox into hexagon shape passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,014
Default

I'm not sure why you need anti-aliasing.
The region is setup, so you can't draw outside the region, so you won't be able to smooth the outside edge of the region.
But if you are trying to draw a border edge along the inside edge of the hexagon, then the inside edge of the lines should be smoothed by the anti-aliasing.

If that is what your are trying to do, then I guess there are two ways you can add a line around the border.
First, you need to set the SmoothingMode before you draw your line.

You could add the drawing of the line to the paint event you received from the control.
In that case, you need to reference the control in your sub, not "Me", which would be the Form the control is on.
Code:
  Private Sub HexPictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles HexPictureBox1.Paint
    DrawPolygonPoint(sender, e)
  End Sub

  Public Sub DrawPolygonPoint(Sender As Object, ByVal e As PaintEventArgs)
    Dim hb As HexPictureBox = DirectCast(Sender, HexPictureBox)

    Dim blackPen As New Pen(Color.Black, 3)
    Dim v As Integer = CInt(hb.Width / 2 * Math.Sin(30 * Math.PI / 180))

    Dim point1 As New Point(hb.Width \ 2, 0)
    Dim point2 As New Point(hb.Width, v)
    Dim point3 As New Point(hb.Width, hb.Height - v)
    Dim point4 As New Point(hb.Width \ 2, hb.Height)
    Dim point5 As New Point(0, hb.Height - v)
    Dim point6 As New Point(0, v)

    Dim curvePoints As Point() = {point1, point2, point3, point4, _
    point5, point6}

    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    e.Graphics.DrawPolygon(blackPen, curvePoints)

    blackPen.Dispose()
    ' Me.Region = New Region(e) ? (HOW? jaja

  End Sub
So, above, a control (HexPictureBox1) was added to the form.
In the paint event that control generates, the parameters were passed to the sub to draw a line around the control.
The sub converts the sender generic object into a HexPictureBox object, then uses that instead of "Me" to get the sizes and generate the points and draw.
This will work, if you want to try it.

But more than likely, you don't want to have to put code in the paint events of all the hex picturebox controls you create.
You'll want to add the sub to the HexPicturebox class.
In that case, you override the OnPaint event in that class.
You call the base class OnPaint handler first, so it will draw the image into the region, and then you call the routine to draw the border.
Code:
  Protected Overrides Sub OnPaint(pe As System.Windows.Forms.PaintEventArgs)
    MyBase.OnPaint(pe)
    DrawPolygonPoint(pe)
  End Sub

  Public Sub DrawPolygonPoint(ByVal e As PaintEventArgs)

    Dim blackPen As New Pen(Color.Black, 3)
    Dim v As Integer = CInt(Me.Width / 2 * Math.Sin(30 * Math.PI / 180))

    Dim point1 As New Point(Me.Width \ 2, 0)
    Dim point2 As New Point(Me.Width, v)
    Dim point3 As New Point(Me.Width, Me.Height - v)
    Dim point4 As New Point(Me.Width \ 2, Me.Height)
    Dim point5 As New Point(0, Me.Height - v)
    Dim point6 As New Point(0, v)

    Dim curvePoints As Point() = {point1, point2, point3, point4, _
    point5, point6}

    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    e.Graphics.DrawPolygon(blackPen, curvePoints)

    blackPen.Dispose()

    ' Me.Region = New Region(e) ? (HOW? jaja

  End Sub
Now, within the HexPicturebox class, "Me" is referring to the instance of the class, so is correct. This should draw the border along the edge of all HexPictureboxes, without the user having to code anything in the paint event of the controls.

For testing, I added two hexpicturebox controls to the form and put images in them. I set the SizeMode property to StretchImage so that most of the image was visible in the Hex area.
In the form, I added a little bit of code to a shared MouseMove event, so I could drag the pictureboxes around while the program was running
Code:
Public Class Form1

  Private Sub HexPictureBox_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles HexPictureBox1.MouseMove, HexPictureBox2.MouseMove
    Dim hpb As PictureBox = DirectCast(sender, HexPictureBox)
    Static lloc As Point
    If e.Button = Windows.Forms.MouseButtons.Left Then
      hpb.Location += New Size(e.X - lloc.X, e.Y - lloc.Y)
    Else
      lloc = e.Location
    End If
  End Sub

End Class
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #9  
Old 04-09-2015, 12:31 AM
zacacp zacacp is offline
Newcomer
 
Join Date: Jan 2015
Posts: 6
Default

I find the way to make it! i am sure that code will also work.
I leave that project in stand by... when i start to read i feel like "dam code"
jajaja
I am a bit Hanged so I forgot to enter the site and say "Thanks" for your commitment....
i ll write another thread, you are welcome to read it. send hugs for you.
Code:
Public class marrycrismas
 
 Private sub zacacp_helped(sender as object, e as eventargs) handless zacacp.helped
  if zacacp.happyhelped.intersectswith(passel.wisdom) then
     zacacp.location = passel.location
     HugAnimation.start()
   end if
end sub
jajajajajajajaja!
Reply With Quote
Reply

Tags
drawing, hexagon, picturebox, shape


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
picturebox into hexagon shape
picturebox into hexagon shape
picturebox into hexagon shape picturebox into hexagon shape
picturebox into hexagon shape
picturebox into hexagon shape
picturebox into hexagon shape picturebox into hexagon shape picturebox into hexagon shape picturebox into hexagon shape picturebox into hexagon shape picturebox into hexagon shape picturebox into hexagon shape
picturebox into hexagon shape
picturebox into hexagon shape
 
picturebox into hexagon shape
picturebox into hexagon shape
 
-->