How can I draw a line on the form that can go around itself ?

I would really appreciate if someone could help me out with this question. I have no clue how to solve it. Thanks in advance :)

Dani AI

Generated

As hinted, the movement of a “clock‑hand” is just trig: compute the end point with Cos/Sin and redraw at a new angle each tick. Use a PictureBox with AutoRedraw = True (to keep drawn pixels) and a Timer to update the angle. Note that VB6's Sin/Cos expect radians and that VB6 Y coordinates increase downward (so subtract the Sin term when converting to screen Y). The example below rotates a line about the PictureBox center; changing the math slightly will make it rotate about one endpoint instead.

Option Explicit

Private Const PI As Double = 3.14159265358979

Dim angDeg As Double
Dim cx As Single, cy As Single, armLen As Single

Private Sub Form_Load()
    ' Add a PictureBox named picCanvas (AutoRedraw = True) and a Timer named tmrSpin.
    picCanvas.AutoRedraw = True
    picCanvas.ScaleMode = vbPixels
    picCanvas.BorderStyle = 0
    picCanvas.Cls

    cx = picCanvas.ScaleWidth / 2
    cy = picCanvas.ScaleHeight / 2

    If cx < cy Then
        armLen = cx * 0.9
    Else
        armLen = cy * 0.9
    End If

    angDeg = 0
    tmrSpin.Interval = 50   ' milliseconds (adjust for speed)
    tmrSpin.Enabled = True
End Sub

Private Sub tmrSpin_Timer()
    Dim rad As Double
    Dim x2 As Single, y2 As Single

    picCanvas.Cls

    angDeg = angDeg + 6
    If angDeg >= 360 Then angDeg = angDeg - 360

    rad = angDeg * PI / 180#
    x2 = cx + armLen * Cos(rad)
    y2 = cy - armLen * Sin(rad)   ' subtract because Y grows downward

    picCanvas.Line (cx, cy)-(x2, y2), vbBlack
    picCanvas.Circle (cx, cy), 3, vbBlack   ' pivot dot
End Sub

Troubleshooting notes: clearing with .Cls each tick is simplest but can flicker; for smoother animation use an off‑screen PictureBox (AutoRedraw = True) as a back buffer and copy it to the visible control (or use BitBlt). The rotation speed = (360 / degreesPerTick) (Interval_ms / 1000) seconds — the sample uses 6° per 50 ms → 3 seconds per revolution. For rotation about the line center compute two symmetric endpoints using ±Cos/Sin (length/2). This implementation follows 's trig tip and expands on 's rotate idea without relying on the Shape control.

Recommended Answers

All 6 Replies

.Circle?

Do you mean a line that goes around the edge of the form, like a border? If so, you can Shape control, and redraw the Shape's dimensions in the Form_Resize event.

As an example, create a Shape on the form with a Top and Left of 60. Then add

Shape1.Height = Me.ScaleHeight - 120
Shape1.Width = Me.ScaleWidth - 120

to the Form_Resize subroutine. This resizes the Shape when the form is resized by the user.

No not going around the edge of the form like a border.
I mean a line that can go around its axis.

If you want to move that like hands of a clock use SIN and COS trigonometric functions.

Yes DEBASISDAS like hands of a clock. But can you write it for me please? I'm not an expert in VB6
And SCBWV the link which you gave me didn't solve the question.
But Thank you. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.