PublicClass Form1Class Form1 Dim PtStart As Point '记录绘制直线的起始点 Dim PtEnd As Point '记录绘制直线的终点 Dim ShouldDrawLine AsBoolean'是否绘制直线 '记录鼠标左键点击的位置,第二次点击后开始绘制直线 PrivateSub Pic1_MouseDown()Sub Pic1_MouseDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseDown If e.Button = Windows.Forms.MouseButtons.Left Then IfNot 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 EndIf EndIf End Sub '绘制鼠标的移动轨迹(仅在鼠标第一次点击后开始绘制轨迹) PrivateSub Pic1_MouseMove()Sub Pic1_MouseMove(ByVal sender AsObject, ByVal 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 <>NothingThen ControlPaint.DrawReversibleLine(p, pt, Color.Red) '绘制鼠标移动后新的轨迹 pt = Pic1.PointToScreen(New Point(e.X, e.Y)) ControlPaint.DrawReversibleLine(p, pt, Color.Red) Else pt =Nothing'清除鼠标原位置 EndIf End Sub '绘制鼠标两次点击位置之间的直线 PrivateSub DrawLine()Sub DrawLine(ByVal mPoint1 As Point, ByVal mPoint2 As Point) Pic1.Refresh() '用于刷新Picturebox表面 Pic1.CreateGraphics.DrawLine(Pens.Blue, mPoint1, mPoint2) '绘制两点间的直线 End Sub '绘制多条直线,每两次鼠标点击确定一条线 PrivateSub DrawLines()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 PrivateSub Form1_Load()Sub Form1_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True) End Sub End Class
注:此代码运行需要一个Form,Form上存在一个Pic1的Picturebox控件。
举一反三,附另外一段代码:
如何在窗体上随鼠标的移动快速绘制十字形轨迹源码
PublicClass Form2Class Form2 Dim OldPoint As Point PrivateSub Form2_Load()Sub Form2_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True) End Sub PrivateSub Form1_MouseMove()Sub Form1_MouseMove(ByVal sender AsObject, ByVal e As System.Windows.Forms.MouseEventArgs) HandlesMe.MouseMove Dim p1, p2, p3, p4 As Point If OldPoint <>NothingThen 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) EndIf 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上实时绘制鼠标选中的矩形框
PublicClass Form2Class Form2 Dim PtStart, pt As Point Dim RectSize As Size PrivateSub Pic1_MouseDown()Sub Pic1_MouseDown(ByVal sender AsObject, ByVal 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 PrivateSub Pic1_MouseMove()Sub Pic1_MouseMove(ByVal sender AsObject, ByVal 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 <>NothingThen 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) EndIf End Sub PrivateSub DrawRect()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) EndIf Pic1.CreateGraphics.DrawRectangle(Pens.Blue, New Rectangle(PtStart, RectSize)) MsgBox(String.Format("左上角坐标{0}"+ vbCrLf +"矩形大小{1}", PtStart.ToString, RectSize.ToString), MsgBoxStyle.Information, "Info") End Sub PrivateSub Form1_Load()Sub Form1_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True) End Sub PrivateSub Pic1_MouseUp()Sub Pic1_MouseUp(ByVal sender AsObject, ByVal 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 EndIf End Sub End Class