Public Class Form1
Dim Bmp1 As Bitmap Dim IsDown As Boolean = False
Dim myPen As New Pen(Color.GreenYellow, 2)
Dim arrayLine(,) As Point
Dim pLine1, pLine2 As Point
Dim countLine, countRect, countEillpse As Integer
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then ' 直接判断是否鼠标左键按下即可. 不用状态变量记录
pLine1.X = e.X
pLine1.Y = e.Y
End If
End Sub
' Move 事件里面不画图,
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then 'If IsDown Then ' 直接判断是否鼠标左键按下即可. 不用状态变量记录
pLine2.X = e.X
pLine2.Y = e.Y
Me.Invalidate() ' 刷新屏幕 , 触发 paint 事件
End If
End Sub
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
If e.Button = Windows.Forms.MouseButtons.Left Then ' 直接判断是否鼠标左键按下即可. 不用状态变量记录
countLine += 1
ReDim Preserve arrayLine(2, countLine)
arrayLine(1, countLine) = pLine1
arrayLine(2, countLine) = pLine2
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Bmp1 = New Bitmap(Me.Size.Width, Me.Size.Height)
countLine = 0
Me.DoubleBuffered = True ' 如果要防止屏幕闪烁的话, 打开双缓存就可以了.
End Sub
'新增程序段: 使用窗口的Paint事件, 来画图
'这样的好外是速度快, 也不闪烁, 并且窗口最小化再恢复以后, 不会变成空白屏幕.
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' e.Graphics 就是窗口的图形, 直接在上面画就可以了
Dim i As Integer
If countLine <> 0 Then
For i = 0 To countLine
e.Graphics.DrawLine(myPen, arrayLine(1, i).X, arrayLine(1, i).Y, arrayLine(2, i).X, arrayLine(2, i).Y)
Next
End If
e.Graphics.DrawLine(myPen, pLine1.X, pLine1.Y, pLine2.X, pLine2.Y)
End Sub
End Class
窗体上画直线
最新推荐文章于 2021-11-12 16:51:48 发布