Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET Interface and Graphics (http://www.xtremevbtalk.com/-net-interface-and-graphics/)
-   -   picturebox into hexagon shape (http://www.xtremevbtalk.com/-net-interface-and-graphics/313016-picturebox-hexagon-shape.html)

daves2369 01-03-2010 09:52 AM

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

boops boops 01-03-2010 05:28 PM

1 Attachment(s)
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

daves2369 01-04-2010 02:05 AM

thanks "boops boops"
it works
just what i was looking for

zacacp 01-25-2015 01:55 PM

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]

passel 01-25-2015 03:38 PM

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.

zacacp 01-25-2015 04:07 PM

ok i understood the concept of what you say, i ll try to write it. thanks!

zacacp 01-25-2015 04:28 PM

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]

passel 01-26-2015 09:44 PM

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


zacacp 04-08-2015 11:31 PM

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!


All times are GMT -6. The time now is 05:31 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.