Adjusting relation between mouse position and arrow position

mtheriault2000

Well-known Member
Joined
Oct 23, 2008
Messages
826
Hello again

I want to get a routine that will place an arrow where the mouse is pointing. To do that, i use a modified code base on member "Rorya" to catch the mouse position and display an arrow.

Works, but i got an offset on the resulting position.

In red, the value that i modified to get near the desired position.

What formula should i use to get it place at the good place.

Martin

Rich (BB code):
Option Explicit
Public XPos As Long, YPos As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    (ByVal hwnd1 As Long, ByVal hwnd2 As Long, ByVal lpsz1 As String, _
             ByVal lpsz2 As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
 X As Long
 Y As Long
End Type
Dim Pos As POINTAPI
Const csglSHAPE_SIZE As Single = 20
Public Sub GetCoordinates()
 'Action sub deployed by clicking the ActionKey.
 Dim P As RANGE, PN As String
 Dim shp As Shape
  GetCursorPos Pos
  'Record
  XPos = Pos.X
  YPos = Pos.Y
  With ActiveWindow
      XPos = 20 + (XPos - .PointsToScreenPixelsX(0)) * 58 / .Zoom
      YPos = (YPos - .PointsToScreenPixelsY(0)) * 60 / .Zoom
         End With
End Sub
 
Sub test1()
Dim mydocument As Variant
 Call GetCoordinates
 Set mydocument = Worksheets(1)
 mydocument.Shapes.AddShape msoShapeUpArrow, XPos, YPos, 5, 20
 
End Sub
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,213,528
Messages
6,114,154
Members
448,553
Latest member
slaytonpa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top