Click here to Skip to main content
15,889,335 members
Home / Discussions / Visual Basic
   

Visual Basic

 
Questionhow to capture mouse events on shapes or graphic objects Pin
eyes200714-Aug-07 3:44
eyes200714-Aug-07 3:44 
AnswerRe: how to capture mouse events on shapes or graphic objects Pin
Taylor Kobani14-Aug-07 3:55
Taylor Kobani14-Aug-07 3:55 
GeneralRe: how to capture mouse events on shapes or graphic objects Pin
eyes200714-Aug-07 4:01
eyes200714-Aug-07 4:01 
GeneralRe: how to capture mouse events on shapes or graphic objects Pin
Taylor Kobani14-Aug-07 4:35
Taylor Kobani14-Aug-07 4:35 
GeneralRe: how to capture mouse events on shapes or graphic objects Pin
eyes200714-Aug-07 4:49
eyes200714-Aug-07 4:49 
GeneralRe: how to capture mouse events on shapes or graphic objects Pin
Richard Blythe14-Aug-07 16:12
Richard Blythe14-Aug-07 16:12 
GeneralRe: how to capture mouse events on shapes or graphic objects Pin
eyes200714-Aug-07 19:43
eyes200714-Aug-07 19:43 
GeneralRe: how to capture mouse events on shapes or graphic objects Pin
Richard Blythe15-Aug-07 6:12
Richard Blythe15-Aug-07 6:12 
This is pretty lengthy but I think it will get you going. I had to convert the original code from C# into VB.NET code.
Create a new class and call it: ShapeManager. Once the class file opens, replace all code with this code below: (Note: Copy down until you see: "End Code"

'Start Code below
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing.Drawing2D
Imports System.Drawing
Public Class ShapeManager
'//All shapes will be exposed through this collection
Public Shapes As System.Collections.ArrayList

Public Sub PaintShapes(ByVal g As Graphics)
'note: the clipping area should already be set
'in the graphics object through this.Invalidate()

'We will now loop through all the shapes and paint
'only those that are part of the clipping rectangle.
'Processing is faster if we use a Int in our For loop
'instead of the ArrayList.Count property
Dim intCount As Short = Convert.ToInt16(Shapes.Count - 1)
For intNum As Short = 0 To intCount
'Don't send shape to graphics object if it's bounds
'do not fall inside the clipbounds. This helps when
'you have thousands of shapes and you are only wanting to
'update a small portion of the screen. For more info, do a
'search on Graphics.Clipping
Dim curShape As MyShape = CType(Shapes(intNum), MyShape)
If curShape.IsInsideClipBounds(g.ClipBounds) Then
'ensure that our Path object has been initialized
If Not curShape.Path Is Nothing Then
'Sample paint code
g.FillPath(New SolidBrush(curShape.FillColor), curShape.Path)
g.DrawPath(New Pen(curShape.LineColor), curShape.Path)
End If
End If
Next


End Sub
End Class
Public Class MyShape
Public Path As GraphicsPath
Public LineColor As Color
Public FillColor As Color
Public Sub New()
'Allows us to create a new shape without having to pass
'any initial properties
End Sub

Public Sub New(ByVal gp As GraphicsPath, ByVal lineColor As Color, ByVal fillColor As Color)
'Initialize the shape
Path = gp
lineColor = lineColor
fillColor = fillColor
End Sub

'*** Here's the function that will help you ***
Public Function ContainsMousePoint(ByVal pnt As Point) As Boolean
'A more common name for this function is HitTest but I think
'you'll connect with this name better. The pnt passed to us
'should be the location of the mouse.
If Not Path Is Nothing Then
Return Path.IsVisible(pnt)
Else
Return False
End If
End Function

Public Function IsInsideClipBounds(ByVal clipBounds As RectangleF) As Boolean
Return clipBounds.Contains(Path.GetBounds())
End Function
End Class


'***********************************************************
'***********************************************************
'End Code


The way to wire this up to your form is to trap your form's MouseDown event
and write some code like the example below:

'Example***********
Dim shapeManager As New ShapeManager()
Private Sub frmMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
Dim intCount As Short = Convert.ToInt16(shapeManager.Shapes.Count - 1)
For intNum As Short = 0 To intCount
Dim curShape As MyShape = CType(shapeManager.Shapes(intNum), MyShape)
If curShape.ContainsMousePoint(e.Location) Then
MessageBox.Show("The mouse hit me!")
End If
Next
End Sub
'End Example***************************

Hope this helps you! Smile | :)
Richard



may your code be error free
QuestionHow can i get IP address of my computer using Vb 6.0 Pin
koolprasad200314-Aug-07 1:52
professionalkoolprasad200314-Aug-07 1:52 
AnswerRe: How can i get IP address of my computer using Vb 6.0 Pin
Froz3n14-Aug-07 1:55
Froz3n14-Aug-07 1:55 
GeneralRe: How can i get IP address of my computer using Vb 6.0 Pin
koolprasad200314-Aug-07 2:26
professionalkoolprasad200314-Aug-07 2:26 
GeneralRe: How can i get IP address of my computer using Vb 6.0 Pin
Froz3n14-Aug-07 2:28
Froz3n14-Aug-07 2:28 
GeneralRe: How can i get IP address of my computer using Vb 6.0 Pin
Dave Kreskowiak14-Aug-07 3:12
mveDave Kreskowiak14-Aug-07 3:12 
GeneralRe: How can i get IP address of my computer using Vb 6.0 Pin
Dave Kreskowiak14-Aug-07 3:13
mveDave Kreskowiak14-Aug-07 3:13 
AnswerRe: How can i get IP address of my computer using Vb 6.0 Pin
Taylor Kobani14-Aug-07 3:45
Taylor Kobani14-Aug-07 3:45 
AnswerRe: How can i get IP address of my computer using Vb 6.0 Pin
Vasudevan Deepak Kumar14-Aug-07 5:16
Vasudevan Deepak Kumar14-Aug-07 5:16 
AnswerRe: How can i get IP address of my computer using Vb 6.0 Pin
koolprasad200315-Aug-07 19:46
professionalkoolprasad200315-Aug-07 19:46 
QuestionFormatting Numbers Pin
Froz3n14-Aug-07 1:49
Froz3n14-Aug-07 1:49 
AnswerRe: Formatting Numbers Pin
Tom Deketelaere14-Aug-07 2:10
professionalTom Deketelaere14-Aug-07 2:10 
GeneralRe: Formatting Numbers Pin
Froz3n14-Aug-07 2:14
Froz3n14-Aug-07 2:14 
QuestionI want to compare between the dates something like that Pin
Ahmed El-Badry14-Aug-07 0:09
Ahmed El-Badry14-Aug-07 0:09 
AnswerRe: I want to compare between the dates something like that Pin
Christian Graus14-Aug-07 0:19
protectorChristian Graus14-Aug-07 0:19 
GeneralRe: I want to compare between the dates something like that Pin
jensenx1-Sep-07 19:26
jensenx1-Sep-07 19:26 
QuestionCombobox Value Bound to DB Pin
Trupti Mehta13-Aug-07 23:50
Trupti Mehta13-Aug-07 23:50 
AnswerRe: Combobox Value Bound to DB Pin
i gr814-Aug-07 1:40
i gr814-Aug-07 1:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.