Jaafar Tribak
Well-known Member
- Joined
- Dec 5, 2002
- Messages
- 9,797
- Office Version
- 2016
- Platform
- Windows
Hi all,
Following is a hacky workaround for capturing the above mentioned Mouse events for worksheet shapes.
The code uses the OnUpdate CommandBars event. This event is slow and doesn't fire with fast mouse movements but this is the best I could achieve short of using a Windows timer.
I am not entirely sure this hack will work with all versions although, I think, it should work as expected.
Here is a workbook example
Put the following code in the ThisWorkbook module so that when the workbook is opened, all the shapes located in worksheets(1) will respond to the user's mouse actions.
Following is a hacky workaround for capturing the above mentioned Mouse events for worksheet shapes.
The code uses the OnUpdate CommandBars event. This event is slow and doesn't fire with fast mouse movements but this is the best I could achieve short of using a Windows timer.
I am not entirely sure this hack will work with all versions although, I think, it should work as expected.
Here is a workbook example
Put the following code in the ThisWorkbook module so that when the workbook is opened, all the shapes located in worksheets(1) will respond to the user's mouse actions.
Code:
Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
#If VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
#Else
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
#End If
Private WithEvents CmndBrarsEvent As CommandBars
Private Sub Workbook_Open()
Set CmndBrarsEvent = Application.CommandBars
ActiveWindow.RangeSelection.Select
End Sub
Private Sub CmndBrarsEvent_OnUpdate()
Static oPrevShape As Shape
Dim tPt As POINTAPI
Dim oShape As Shape
On Error Resume Next
GetCursorPos tPt
Set oShape = ActiveSheet.Shapes(ActiveWindow.RangeFromPoint(tPt.x, tPt.y).Name)
If Not oShape Is Nothing And (oPrevShape Is Nothing) Then
On Error GoTo 0
Call Shape_MouseEnter(oShape)
GoTo Xit
End If
If Not oShape Is Nothing Then
On Error GoTo 0
Call Shape_MouseMove(oShape, tPt.x, tPt.y)
GoTo Xit
End If
If oShape Is Nothing And Not (oPrevShape Is Nothing) Then
On Error GoTo 0
Call Shape_MouseLeave(oPrevShape)
End If
Xit:
If TypeName(Selection) = "Range" Then
ActiveWindow.RangeSelection.Select
End If
Set oPrevShape = oShape
End Sub
[B][COLOR=#008000]'=====================
'Mouse events handlers.
'=====================[/COLOR][/B]
Private Sub Shape_MouseEnter(ByVal Shape As Shape)
If Shape.Parent Is Worksheets(1) Then
With Shape
.Fill.ForeColor.RGB = RGB(255, 0, 0)
If Shape.Type = 1 Then .TextFrame2.TextRange.Text = "ACTIVE"
End With
Range("b2") = Shape.Name
End If
End Sub
Private Sub Shape_MouseLeave(ByVal Shape As Shape)
If Shape.Parent Is Worksheets(1) Then
With Shape
.Fill.ForeColor.RGB = RGB(255, 255, 0)
If Shape.Type = 1 Then .TextFrame2.TextRange.Text = ""
End With
Range("b3") = Shape.Name
Range("b1").ClearContents
End If
End Sub
Private Sub Shape_MouseMove(ByVal Shape As Shape, ByVal MouseX As Long, MouseY As Long)
If Shape.Parent Is Worksheets(1) Then
Range("b1") = Shape.Name
Range("c1") = "X:=" & MouseX & " " & "Y:=" & MouseY
End If
End Sub