笔记4:与事件的交互



鼠标点击、弹起、按下等,其中的参数e记录了鼠标的位置信息(e.X ,  e.Y)

可将其当作直线的起点或终点。







通过两次点击,画出对应的直线。

Imports System.Drawing

Public Class Form1
    Dim gr As Graphics
    Dim FirstClick As Boolean = False '是否为第一次点击
    Dim p1 As Point
    Dim p2 As Point

    '鼠标事件中记录了鼠标的位置(e.x,e.y)
    Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
        FirstClick = Not FirstClick
        gr = Me.CreateGraphics
        If FirstClick = True Then
            p1.X = e.X
            p1.Y = e.Y
            gr.DrawString(e.X & " " & e.Y, Me.Font, Brushes.Red, p1) '输出坐标
        Else
            p2.X = e.X
            p2.Y = e.Y
            gr.DrawString(e.X & " " & e.Y, Me.Font, Brushes.Red, p2)
            gr.DrawLine(Pens.Maroon, p1, p2)
        End If
    End Sub
End Class








通过连续点击,画连续的折线

Imports System.Drawing

Public Class Form1
    Dim gr As Graphics
    Dim FirstClick As Boolean = False
    Dim p1 As Point
    Dim p2 As Point

    Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
        If FirstClick = False Then
            p1.X = e.X
            p1.Y = e.Y
            FirstClick = True
        Else
            p2.X = e.X
            p2.Y = e.Y
            gr = Me.CreateGraphics
            gr.DrawLine(Pens.Red, p1, p2)
            p1 = p2
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Refresh() '清屏
        FirstClick = False
    End Sub
End Class








画任意曲线

通过鼠标按下、弹起作为标识,来画直线,不断替换起点和终点。

Imports System.Drawing

Public Class Form1
    Dim gr As Graphics
    Dim FirstClick As Boolean = False
    Dim p1 As Point
    Dim p2 As Point

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        FirstClick = True
        p1.X = e.X
        p1.Y = e.Y
    End Sub

    Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        FirstClick = False
    End Sub

    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        If FirstClick = True Then
            p2.X = e.X
            p2.Y = e.Y

            gr = Me.CreateGraphics
            gr.DrawLine(Pens.Blue, p1, p2)
            p1 = p2
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Refresh() '清屏
        FirstClick = False
    End Sub
End Class





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值