Draw Image At Cursor Position Visual Basic

1k Views Asked by At

Basically all I want to do is draw a certain image at the position on a form or on a picture box that a mouse clicks on for 1 second. The code that I've tried already draws it at random offsets so I was hoping someone can guide me on this. Thank you

1

There are 1 best solutions below

0
On BEST ANSWER

Use the MouseDown() event of the Form and draw at the location specified by "e.X" and "e.Y". You can use the Timer() control to get a one second delay. Here's a quick example:

Public Class Form1

    Private WithEvents Tmr As New System.Windows.Forms.Timer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Tmr.Interval = 1000
        Tmr.Enabled = False
    End Sub

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
        Using G As Graphics = Me.CreateGraphics
            'G.DrawImage(yourImageReferenceHere, New Point(e.X, e.Y))
            Dim rc As New Rectangle(New Point(e.X, e.Y), New Size(1, 1))
            rc.Inflate(9, 9)
            G.DrawEllipse(Pens.Red, rc)
        End Using
        Tmr.Stop()
        Tmr.Start()
    End Sub

    Private Sub Tmr_Tick(sender As Object, e As EventArgs) Handles Tmr.Tick
        Tmr.Stop()
        Me.Refresh()
    End Sub

End Class