Results 1 to 1 of 1

Thread: VB.NET - Picturebox with clickable areas

  1. #1

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    VB.NET - Picturebox with clickable areas

    Ive written a small and simple class that inherits the PictureBox class. It works just like an ordinary picturebox, just that you can load a "ColorMap" (ie, just a copy of the original image with colorized areas) image into it aswell.
    Whenever the picturebox is clicked the ImageClicked event is raised and the clicked color is returned, this is good when you want clickable areas of an image that are not regularly shaped.
    Its not that briliant, but I thought it could be useful for some.

    Here is the class:
    VB Code:
    1. Public Class PictureBox_ColorMap
    2.     Inherits PictureBox
    3.     Private _MapBitmap As Bitmap
    4.     Private _RaiseEventOnMouseMove As Boolean = True
    5.     Private _RaiseEventOnMouseClick As Boolean = True
    6.     Public Property ColorMap() As Bitmap
    7.         Get
    8.             Return _MapBitmap
    9.         End Get
    10.         Set(ByVal value As Bitmap)
    11.             _MapBitmap = value
    12.         End Set
    13.     End Property
    14.  
    15.     Public Property RaiseEventOnMouseMove() As Boolean
    16.         Get
    17.             Return _RaiseEventOnMouseMove
    18.         End Get
    19.         Set(ByVal value As Boolean)
    20.             _RaiseEventOnMouseMove = value
    21.         End Set
    22.     End Property
    23.  
    24.     Public Property RaiseEventOnMouseClick() As Boolean
    25.         Get
    26.             Return _RaiseEventOnMouseClick
    27.         End Get
    28.         Set(ByVal value As Boolean)
    29.             _RaiseEventOnMouseClick = value
    30.         End Set
    31.     End Property
    32.  
    33.     Public Event ColorMapClicked(ByVal sender As Object, ByVal ReturnedColor As Color)
    34.     Public Event ColorMapMouseMove(ByVal sender As Object, ByVal ReturnedColor As Color)
    35.  
    36.     Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs)
    37.         MyBase.OnMouseClick(e)
    38.         If _RaiseEventOnMouseClick Then
    39.             If Not _MapBitmap Is Nothing AndAlso e.X <= _MapBitmap.Width AndAlso e.Y <= _MapBitmap.Height Then
    40.                 RaiseEvent ColorMapClicked(Me, _MapBitmap.GetPixel(e.X, e.Y))
    41.             Else
    42.                 RaiseEvent ColorMapClicked(Me, Nothing)
    43.             End If
    44.         End If
    45.     End Sub
    46.     Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
    47.         MyBase.OnMouseMove(e)
    48.         If _RaiseEventOnMouseMove Then
    49.             If Not _MapBitmap Is Nothing AndAlso e.X <= _MapBitmap.Width AndAlso e.Y <= _MapBitmap.Height Then
    50.                 RaiseEvent ColorMapMouseMove(Me, _MapBitmap.GetPixel(e.X, e.Y))
    51.             Else
    52.                 RaiseEvent ColorMapMouseMove(Me, Nothing)
    53.             End If
    54.         End If
    55.     End Sub
    56. End Class

    Note: It currently only works with SizeMode set to AutoSize (Or set to Normal, but then you have to make sure they are equally sized)

    Here is an example on how to use it. (Couldnt attach in my post because of the 500kb attachment limit)
    Last edited by Atheist; Oct 3rd, 2007 at 02:04 PM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width