Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27241

Some Graphics Help: Highlighting / Clickable Objects

$
0
0
As some of you might notice, I have been playing around with graphics a lot lately and unfortunately have run into some more problems/road blocks

Attachment 99497

That is an example of one of my graphic.

I am looking for a way to make each line clickable if possible, so when a user click on the line something will pop-up with information. It would also be nice if I could highlight that line :P

I found this lovely code at ---> http://social.msdn.microsoft.com/For...5-2e344389a6d8

Code:

Imports System.Drawing.Drawing2D

Public Class Form1
    'the pictureWithPaths
    Private pathBitmap As PathBitmap = Nothing
    'the control to draw on
    Private WithEvents panel1 As New Panel

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'cleanup
        If Not pathBitmap Is Nothing Then
            pathBitmap.Dispose()
        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'intialize the panel and add it to the forms control collection
        panel1.ClientSize = Me.ClientSize
        Me.Controls.Add(panel1)

        pathBitmap = New PathBitmap
        'get a bitmap of desired size and just fill in one color for testing here
        pathBitmap.Bmp = New Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height)

        Using g As Graphics = Graphics.FromImage(pathBitmap.Bmp)
            'draw the contents
            g.SmoothingMode = SmoothingMode.AntiAlias
            g.Clear(Color.Red)
            g.FillEllipse(Brushes.Blue, New Rectangle(New Point(0, 0), pathBitmap.Bmp.Size))

            'add the NamedPaths (GraphicsPaths and the names for the paths)
            Dim gP1 As New GraphicsPath
            gP1.FillMode = FillMode.Alternate
            gP1.AddRectangle(New Rectangle(New Point(0, 0), pathBitmap.Bmp.Size))
            gP1.AddEllipse(New Rectangle(New Point(0, 0), pathBitmap.Bmp.Size))

            Dim p1 As New NamedPath() With {.Name = "RectWithoutEllipse", .Path = gP1}
            pathBitmap.Add(p1)

            Dim gP2 As New GraphicsPath
            gP2.AddEllipse(New Rectangle(New Point(0, 0), pathBitmap.Bmp.Size))

            Dim p2 As New NamedPath() With {.Name = "Ellipse", .Path = gP2}
            pathBitmap.Add(p2)
        End Using
    End Sub

    Private Sub panel1_MouseClick(sender As Object, e As MouseEventArgs) Handles panel1.MouseClick
        'test the three Hittest methods
        MessageBox.Show(pathBitmap.HitTest(e.X, e.Y).Name)
        MessageBox.Show(pathBitmap.HitTest("Ellipse", e.X, e.Y).ToString)

        If Not pathBitmap.PathList Is Nothing AndAlso pathBitmap.PathList.Count > 0 Then
            MessageBox.Show(pathBitmap.HitTest(pathBitmap.PathList(0), e.X, e.Y).ToString)
        End If
    End Sub

    Private Sub panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles panel1.Paint
        'draw the picture
        If Not pathBitmap Is Nothing AndAlso Not pathBitmap.Bmp Is Nothing Then
            e.Graphics.DrawImageUnscaled(pathBitmap.Bmp, 0, 0)
        End If
    End Sub
End Class

Public Class PathBitmap
    Implements IDisposable

    Public Property Bmp As Bitmap
    Public Property PathList As List(Of NamedPath)

    Sub Add(gP As NamedPath)
        If PathList Is Nothing Then
            PathList = New List(Of NamedPath)
        End If

        PathList.Add(gP)
    End Sub

    Public Function HitTest(x As Single, y As Single) As NamedPath
        If PathList Is Nothing Then
            PathList = New List(Of NamedPath)
        End If

        For Each gP As NamedPath In PathList
            If gP.IsVisible(x, y) Then
                Return gP
            End If
        Next

        Return Nothing
    End Function

    Public Function HitTest(gP As NamedPath, x As Single, y As Single) As Boolean
        If PathList Is Nothing Then
            PathList = New List(Of NamedPath)
        End If

        If gP.IsVisible(x, y) Then
            Return True
        End If

        Return False
    End Function

    Public Function HitTest(name As String, x As Single, y As Single) As Boolean
        If PathList Is Nothing Then
            PathList = New List(Of NamedPath)
        End If

        For Each gP As NamedPath In PathList
            If gP.Name = name AndAlso gP.IsVisible(x, y) Then
                Return True
            End If
        Next

        Return False
    End Function

#Region "IDisposable Support"
    Private disposedValue As Boolean

    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                If Not PathList Is Nothing Then
                    For i As Integer = PathList.Count - 1 To 0 Step -1
                        PathList(i).Dispose()
                    Next

                    PathList.Clear()
                End If
            End If
        End If
        Me.disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region
End Class

Public Class NamedPath
    Implements IDisposable

    Public Property Path As GraphicsPath
    Public Property Name As String

    Public Function IsVisible(x As Single, y As Single)
        If Not Path Is Nothing Then
            Return Path.IsVisible(x, y)
        End If

        Return False
    End Function

#Region "IDisposable Support"
    Private disposedValue As Boolean

    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                If Not Path Is Nothing Then
                    Path.Dispose()
                End If
            End If

        End If
        Me.disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class

It is truly awesome and helped me a lot. It allows you assign a path to everything you draw and then allow you to check that path in a mouse click or move event.

I managed to get it to work for rectangles but for whatever reason it wont work with my lines, if you guys have the time, you can copy and paste the code above and give it a try by adding a line as well.
Attached Images
 

Viewing all articles
Browse latest Browse all 27241

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>