Hi, I'm a web developer new to windows forms. I have a windows form that is one picturebox of an image I made in Photoshop. Can I create hotspots or imagemaps over the portions of the image that I want to be clickable
Okay...good... If you want some element of the image to change indicating that it is clickable, you can use something similar to what I posted... Or, you can simply handle MouseMove and change the cursor accordingly.
Yes, you absolutely can do this... You will need to create your own control derived from PictureBox to do it easiest. But, I did a simple version of this about 2 years ago, so, I'll post it here to get you started:
Private m_HotSpot As RectangleF Private m_HotSpotText As String Private m_HotSpotForeColor As Color Private m_HotSpotFont As Font
Public Sub New() MyBase.New() HotSpot = New RectangleF(20, 20, 100, 100) HotSpotText = "Hot Spot!" HotSpotForeColor = Color.Red HotSpotFont = New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point) 'TODO: Maybe specify default image. End Sub
'TODO: Create Editor for RectangleF Public Property HotSpot() As RectangleF Get Return m_HotSpot End Get Set(ByVal Value As RectangleF) m_HotSpot = Value End Set End Property
Public Property HotSpotText() As String Get Return m_HotSpotText End Get Set(ByVal Value As String) 'TODO: Validate that the text will still fit in the hot spot size if this changes m_HotSpotText = Value End Set End Property
Public Property HotSpotForeColor() As Color Get Return m_HotSpotForeColor End Get Set(ByVal Value As Color) m_HotSpotForeColor = Value End Set End Property
Public Property HotSpotFont() As Font Get Return m_HotSpotFont End Get Set(ByVal Value As Font) 'TODO: Validate that the text will still fit in the hot spot size if this changes m_HotSpotFont = Value End Set End Property
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs) If HotSpot.Contains(e.X, e.Y) Then Dim g As Graphics = Me.CreateGraphics g.DrawString(HotSpotText, HotSpotFont, New SolidBrush(HotSpotForeColor), HotSpot) g.Dispose() Else Me.Refresh() End If End Sub End Class
Thank you. It was so simple, I was just overlooking it. I just used the picturebox.mousedown and then checked the coordinates of the mouse to determine where they clicked.
hotspot type links on picture box controls
Syed Faraz Mahmood
Helge Norvang
Option Strict On
Option Explicit On
Imports System.Windows.Forms
Imports System.Design
Imports System.Windows.Forms.Design
Imports System.Drawing
Imports System.ComponentModel
Public Class HotSpotPicture
Inherits PictureBox
Private m_HotSpot As RectangleF
Private m_HotSpotText As String
Private m_HotSpotForeColor As Color
Private m_HotSpotFont As Font
Public Sub New()
MyBase.New()
HotSpot = New RectangleF(20, 20, 100, 100)
HotSpotText = "Hot Spot!"
HotSpotForeColor = Color.Red
HotSpotFont = New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
'TODO: Maybe specify default image.
End Sub
'TODO: Create Editor for RectangleF
Public Property HotSpot() As RectangleF
Get
Return m_HotSpot
End Get
Set(ByVal Value As RectangleF)
m_HotSpot = Value
End Set
End Property
Public Property HotSpotText() As String
Get
Return m_HotSpotText
End Get
Set(ByVal Value As String)
'TODO: Validate that the text will still fit in the hot spot size if this changes
m_HotSpotText = Value
End Set
End Property
Public Property HotSpotForeColor() As Color
Get
Return m_HotSpotForeColor
End Get
Set(ByVal Value As Color)
m_HotSpotForeColor = Value
End Set
End Property
Public Property HotSpotFont() As Font
Get
Return m_HotSpotFont
End Get
Set(ByVal Value As Font)
'TODO: Validate that the text will still fit in the hot spot size if this changes
m_HotSpotFont = Value
End Set
End Property
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
If HotSpot.Contains(e.X, e.Y) Then
Dim g As Graphics = Me.CreateGraphics
g.DrawString(HotSpotText, HotSpotFont, New SolidBrush(HotSpotForeColor), HotSpot)
g.Dispose()
Else
Me.Refresh()
End If
End Sub
End Class
AllanCER
Thanks again for the fast response.
Jon