|
-
Apr 16th, 2007, 08:52 AM
#1
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:
Public Class PictureBox_ColorMap
Inherits PictureBox
Private _MapBitmap As Bitmap
Private _RaiseEventOnMouseMove As Boolean = True
Private _RaiseEventOnMouseClick As Boolean = True
Public Property ColorMap() As Bitmap
Get
Return _MapBitmap
End Get
Set(ByVal value As Bitmap)
_MapBitmap = value
End Set
End Property
Public Property RaiseEventOnMouseMove() As Boolean
Get
Return _RaiseEventOnMouseMove
End Get
Set(ByVal value As Boolean)
_RaiseEventOnMouseMove = value
End Set
End Property
Public Property RaiseEventOnMouseClick() As Boolean
Get
Return _RaiseEventOnMouseClick
End Get
Set(ByVal value As Boolean)
_RaiseEventOnMouseClick = value
End Set
End Property
Public Event ColorMapClicked(ByVal sender As Object, ByVal ReturnedColor As Color)
Public Event ColorMapMouseMove(ByVal sender As Object, ByVal ReturnedColor As Color)
Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseClick(e)
If _RaiseEventOnMouseClick Then
If Not _MapBitmap Is Nothing AndAlso e.X <= _MapBitmap.Width AndAlso e.Y <= _MapBitmap.Height Then
RaiseEvent ColorMapClicked(Me, _MapBitmap.GetPixel(e.X, e.Y))
Else
RaiseEvent ColorMapClicked(Me, Nothing)
End If
End If
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseMove(e)
If _RaiseEventOnMouseMove Then
If Not _MapBitmap Is Nothing AndAlso e.X <= _MapBitmap.Width AndAlso e.Y <= _MapBitmap.Height Then
RaiseEvent ColorMapMouseMove(Me, _MapBitmap.GetPixel(e.X, e.Y))
Else
RaiseEvent ColorMapMouseMove(Me, Nothing)
End If
End If
End Sub
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|