Drawing lines using captured coordinates

adb11a

New Member
Joined
Jul 4, 2010
Messages
1
Hi,

Using the following code I've found on the net today I'm capturing coordinates of mouse clicks (2) and then trying to plot a line (arrow) from the first click to the second. It basically works but the arrow isn't matching being placed where I clicked. The near I am to the top left-hand side of the screen the more accurate the plotted line is.

I'm guessing it may have something to do with twips and zoom but unfortunately the answer is beyond my capabilities.

Sub CaptureClicks()
Dim MousePT As POINTAPI
Dim x As Double, y As Double
GetCursorPos MousePT
x = MousePT.x
y = MousePT.y

If [T1] <> "" And [T2] <> "" Then
[T1] = ""
[T2] = ""
[U1] = ""
[U2] = ""
End If

If [T1].Value = 0 Then
[S1] = "Click 1"
[T2] = ""
[U2] = ""
[T1] = x
[U1] = y
Else
[S2] = "Click 2"
[T2] = x
[U2] = y
End If

xLoc1 = [T1].Value
xLoc2 = [T2].Value
yLoc1 = [U1].Value
yLoc2 = [U2].Value
If [T2] <> 0 Then
Set myShape = ActiveSheet.Shapes.AddLine(xLoc1, yLoc1, xLoc2, yLoc2)

With myShape
.Name = "ArrowSegment" & CStr(Ipts)
With .Line
.ForeColor.SchemeColor = 12 ' blue
.EndArrowheadLength = msoArrowheadLong
.EndArrowheadWidth = msoArrowheadWidthMedium
.EndArrowheadStyle = msoArrowheadTriangle
End With
End With
End If
End Sub


Andrew
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Your code captures the mouse position for the whole screen, whereas drawing objects coordinates are relative to the position of the top left of cell A1 ( which of course is not at the very top left of the screen ). How are you calling your code? Could you use cell reference instead, and use the Top/Left etc of the cell?

If you are interested, Chip Pearson covers how to do XY positioning over cells, and so you could do the reverse calcs to convert XY into worksheet position:

http://www.cpearson.com/excel/FormPosition.htm
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,774
Members
452,942
Latest member
VijayNewtoExcel

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