Mouse drag and draw : Mouse Draw « 2D Graphics « VB.Net Tutorial






Mouse drag and draw
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class MouseDrawLine
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()

        InitializeComponent()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
    End Sub

    Dim g As System.Drawing.Graphics
    Dim x1, y1, x2, y2 As Integer
    Dim pen1 As New System.Drawing.Pen(Color.Black)
    Dim mouseDown As Integer = 0


    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        mouseDown = 1
        x1 = e.X
        y1 = e.Y
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        mouseDown = 0
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        If mouseDown = 0 Then
            Exit Sub
        Else
            x2 = e.X
            y2 = e.Y
            g = Me.CreateGraphics
            g.DrawLine(pen1, x1, y1, x2, y2)
            x1 = x2
            y1 = y2
        End If
    End Sub
End Class








17.25.Mouse Draw
17.25.1.Mouse drag and drawMouse drag and draw
17.25.2.Using the mouse to draw on a formUsing the mouse to draw on a form