vb.net中如何快速绘制鼠标移动轨迹

 vb.net中如何在picturebox上绘制直线,且绘制鼠标移动轨迹

    主要代码及注释如下:

 

Public   Class Form1

    
Dim PtStart As Point                '记录绘制直线的起始点
    Dim PtEnd As Point                  '记录绘制直线的终点
    Dim ShouldDrawLine As Boolean       '是否绘制直线

    
'记录鼠标左键点击的位置,第二次点击后开始绘制直线
    Private Sub Pic1_MouseDown(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseDown
        
If e.Button = Windows.Forms.MouseButtons.Left Then
            
If Not ShouldDrawLine Then
                PtStart 
= New Point(e.X, e.Y)
                ShouldDrawLine 
= True
            
Else
                PtEnd 
= New Point(e.X, e.Y)

                
'下面两句根据需要进行取舍
                'Call DrawLine(PtStart, PtEnd)   '绘制一条直线
                Call DrawLines(PtStart, PtEnd)  '绘制多条直线

                ShouldDrawLine 
= False
            
End If
        
End If
    
End Sub


    
'绘制鼠标的移动轨迹(仅在鼠标第一次点击后开始绘制轨迹)
    Private Sub Pic1_MouseMove(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseMove
        
Static pt As Point
        
If ShouldDrawLine Then
            
'鼠标第一次点击的位置(需转化为屏幕坐标)
            Dim p As Point = Pic1.PointToScreen(PtStart)
            
'清除原先绘制的鼠标移动轨迹
            If pt <> Nothing Then ControlPaint.DrawReversibleLine(p, pt, Color.Red)
            
'绘制鼠标移动后新的轨迹
            pt = Pic1.PointToScreen(New Point(e.X, e.Y))
            ControlPaint.DrawReversibleLine(p, pt, Color.Red)
        
Else
            pt 
= Nothing '清除鼠标原位置
        End If
    
End Sub


    
'绘制鼠标两次点击位置之间的直线
    Private Sub DrawLine(ByVal mPoint1 As Point, ByVal mPoint2 As Point)
        Pic1.Refresh()      
'用于刷新Picturebox表面
        Pic1.CreateGraphics.DrawLine(Pens.Blue, mPoint1, mPoint2)   '绘制两点间的直线
    End Sub


    
'绘制多条直线,每两次鼠标点击确定一条线
    Private Sub DrawLines(ByVal mPoint1 As Point, ByVal mPoint2 As Point)
        
'此句不可删除,用于清除鼠标点击前的轨迹
        ControlPaint.DrawReversibleLine(Pic1.PointToScreen(mPoint1), Pic1.PointToScreen(mPoint2), Color.Red)
        Pic1.CreateGraphics.DrawLine(Pens.Blue, mPoint1, mPoint2) 
'绘制两点间的直线
    End Sub


    
Private Sub Form1_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
        
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
    
End Sub


End Class

注:此代码运行需要一个Form,Form上存在一个Pic1的Picturebox控件。

 

举一反三,附另外一段代码:

如何在窗体上随鼠标的移动快速绘制十字形轨迹源码

Public   Class Form2

    
Dim OldPoint As Point

    
Private Sub Form2_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
        
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
    
End Sub


    
Private Sub Form1_MouseMove(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        
Dim p1, p2, p3, p4 As Point

        
If OldPoint <> Nothing Then
            p1 
= PointToScreen(New Point(OldPoint.X, 0))
            p2 
= PointToScreen(New Point(OldPoint.X, Me.ClientSize.Height))
            p3 
= PointToScreen(New Point(0, OldPoint.Y))
            p4 
= PointToScreen(New Point(Me.ClientSize.Width, OldPoint.Y))

            ControlPaint.DrawReversibleLine(p1, p2, Color.Cyan)
            ControlPaint.DrawReversibleLine(p3, p4, Color.Cyan)
        
End If

        p1 
= PointToScreen(New Point(e.X, 0))
        p2 
= PointToScreen(New Point(e.X, Me.ClientSize.Height))
        p3 
= PointToScreen(New Point(0, e.Y))
        p4 
= PointToScreen(New Point(Me.ClientSize.Width, e.Y))

        ControlPaint.DrawReversibleLine(p1, p2, Color.Cyan)
        ControlPaint.DrawReversibleLine(p3, p4, Color.Cyan)

        OldPoint 
= New Point(e.X, e.Y)
    
End Sub


End Class

 再附一段代码,在picturebox上实时绘制鼠标选中的矩形框

Public   Class Form2

    
Dim PtStart, pt As Point
    
Dim RectSize As Size

    
Private Sub Pic1_MouseDown(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseDown
        
If e.Button = Windows.Forms.MouseButtons.Left Then PtStart = New Point(e.X, e.Y)
    
End Sub


    
Private Sub Pic1_MouseMove(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseMove
        
If e.Button = Windows.Forms.MouseButtons.Left Then
            
Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location)
            
If pt <> Nothing Then ControlPaint.DrawReversibleFrame(New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
            pt 
= PointToScreen(New Point(e.X, e.Y)) : pt.Offset(Pic1.Location)
            ControlPaint.DrawReversibleFrame(
New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
        
End If
    
End Sub


    
Private Sub DrawRect(ByVal mPoint1 As Point, ByVal mPoint2 As Point)
        Pic1.Refresh()
        
If mPoint2.X > mPoint1.X And mPoint2.Y > mPoint1.Y Then
            PtStart 
= New Point(mPoint1.X, mPoint1.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint2.Y - mPoint1.Y)
        
ElseIf mPoint2.X > mPoint1.X And mPoint2.Y < mPoint1.Y Then
            PtStart 
= New Point(mPoint1.X, mPoint2.Y) : RectSize = New Size(mPoint2.X - mPoint1.X, mPoint1.Y - mPoint2.Y)
        
ElseIf mPoint2.X < mPoint1.X And mPoint2.Y > mPoint1.Y Then
            PtStart 
= New Point(mPoint2.X, mPoint1.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint2.Y - mPoint1.Y)
        
ElseIf mPoint2.X < mPoint1.X And mPoint2.Y < mPoint1.Y Then
            PtStart 
= New Point(mPoint2.X, mPoint2.Y) : RectSize = New Size(mPoint1.X - mPoint2.X, mPoint1.Y - mPoint2.Y)
        
End If
        Pic1.CreateGraphics.DrawRectangle(Pens.Blue, 
New Rectangle(PtStart, RectSize))
        
MsgBox(String.Format("左上角坐标{0}" + vbCrLf + "矩形大小{1}", PtStart.ToString, RectSize.ToString), MsgBoxStyle.Information, "Info")
    
End Sub


    
Private Sub Form1_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
        
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
    
End Sub


    
Private Sub Pic1_MouseUp(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseUp
        
If e.Button = Windows.Forms.MouseButtons.Left Then
            
Dim p As Point = PointToScreen(PtStart) : p.Offset(Pic1.Location)
            ControlPaint.DrawReversibleFrame(
New Rectangle(p.X, p.Y, pt.X - p.X, pt.Y - p.Y), Color.Red, FrameStyle.Dashed)
            
Call DrawRect(PtStart, New Point(e.X, e.Y))
            pt 
= Nothing
        
End If
    
End Sub

End Class

 

 

在屏幕上的指定边界内,按指定背景色绘制处于指定状态的可逆框架。参见msdn

 

 

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页