VB6 monitoring the mouse track or a straight line on a form

December 2016


Here are some tips on how you can monitor some lines in a straight line on a form

Random lines

To follow that track the mouse, click on the form, press and hold the right button and move.
Release button to finish.

Straight Lines

To draw straight lines, click on the form, from the starting point and press and hold the button, move the mouse pointer to the destination point and release the button.

Coding

For this function, move the code as shown below.
It can be used on any control that contains an hdc.



Option Explicit
Const PS_SOLID = 0
Const Epp = 10 'size of the line

Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) 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 Lib "gdi32" (ByVal hdc As Long, _
ByVal X As Long, ByVal Y As Long, lpPoint As Any) As Long


Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
DeleteObject SelectObject(Me.hdc, CreatePen(PS_SOLID, Epp, RGB(0, 255, 0)))
MoveToEx Me.hdc, X, Y, &H0
End Sub

'Put the code in Form_MouseUp for a straight line
'without a line>> If Button <> 1 Then Exit Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button <> 1 Then Exit Sub
DeleteObject SelectObject(Me.hdc, CreatePen(PS_SOLID, Epp, RGB(0, 255, 0)))
LineTo Me.hdc, X, Y
Me.Refresh
End Sub

Related :

This document entitled « VB6 monitoring the mouse track or a straight line on a form » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.