窗体上画直线


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值