That works really well.
What I am hoping to do is have a UserForm that has an oval permanently drawn in the centre and for a user to click on a place in the oval to indicate where a cricket ball was hit to.
A line would then automatically be drawn to that point from a point central to the oval and remain there, so when more positions where clicked on over the course of the game, a body of lines was recorded.
The various codes below, currently allow a user to left click on a spot and drag to another spot ... leaving a line in its path. How would I have to modify the various codes to do what I am looking for ?
Private Type POINTAPI_MB
X As Long
Y As Long
End Type
Private Type POINTAPI_MM
X As Long
Y As Long
End Type
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function MoveToEx_mb Lib "gdi32" Alias "MoveToEx" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, lpPoint As POINTAPI_MB) As Long
Private Declare Function MoveToEx_mm Lib "gdi32" Alias "MoveToEx" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, lpPoint As POINTAPI_MM) As Long
Dim startX_mb As Single, startY_mb As Single
Dim startX_mm As Single, startY_mm As Single
Private lngHdc As Long
Private blnDraw As Boolean
Dim p_mb As POINTAPI_MB
Dim p_mm As POINTAPI_MM
Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'store the mouse pointer starting positions
If Button = 1 Then
startX_mb = X
startY_mb = Y
startX_mm = X
startY_mm = Y
End If
'retrieve the UserForm Window handle and use that to return its Device Context
lngHdc = GetActiveWindow
lngHdc = GetDC(lngHdc)
'permit continuous drawing
blnDraw = True
End Sub
Private Sub UserForm_Initialize()
Me.ForeColor = vbRed
End Sub
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
'only draw continuously if the user wants to draw freehand
If Not Me.optFreehand Then Exit Sub
'only draw if the mouse button is held down
If Not blnDraw Then Exit Sub
'make sure it's the left mouse button
If Button = 1 Then
'supply the UDT mouse position values as pixels
p_mm.X = Get_PixelsFromPoints(startX_mm, True)
p_mm.Y = Get_PixelsFromPoints(startY_mm, False)
'pass the UDT to the API to specify where to the drawing began
MoveToEx_mm lngHdc, p_mm.X, p_mm.Y, p_mm
'pass the current mouse position to the API for drawing the line
LineTo lngHdc, Get_PixelsFromPoints(X, True), Get_PixelsFromPoints(Y, False)
're-assign the drawiing start position
startX_mm = X
startY_mm = Y
End If
End Sub
Private Sub UserForm_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
blnDraw = False
'only draw direct if the user wants to draw straight lines only
If Me.optFreehand Then Exit Sub
'draw lines direct to the current mouse position and terminate the drawing process
'same-same but different
If Button = 1 Then
p_mb.X = Get_PixelsFromPoints(startX_mb, True)
p_mb.Y = Get_PixelsFromPoints(startY_mb, False)
MoveToEx_mb lngHdc, p_mb.X, p_mb.Y, p_mb
LineTo lngHdc, Get_PixelsFromPoints(X, True), Get_PixelsFromPoints(Y, False)
End If
End Sub