Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class CrossHair
- Private Canvas As Control = Nothing
- Private CrossHairPosition As Point = Point.Empty
- Private DrawingLocation As CHLocation
- Public Sub New(DrawingSurface As Control, InitialPosition? As Point)
- Me.CrossHairSize = New Size(50, 50)
- Me.LineStyle = DashStyle.Solid
- Me.PenColor = Color.Red
- Me.PenSize = 3
- Me.SetPen()
- Me.Canvas = DrawingSurface
- If InitialPosition = Nothing Then
- Me.CrossHairPosition = If(Me.Canvas Is Nothing,
- Point.Empty,
- New Point((Me.Canvas.Left + Me.Canvas.Width) \ 2,
- (Me.Canvas.Top + Me.Canvas.Height) \ 2))
- Me.MoveCrossHair()
- End If
- Me.DrawingLocation = New CHLocation(Me.CrossHairPosition, Me.CrossHairSize, Me.PenSize)
- If DrawingSurface IsNot Nothing Then
- AddHandler Me.Canvas.Paint, AddressOf Me.PaintOnCanvas
- End If
- End Sub
- Public Property Position() As Point
- Get
- Return Me.CrossHairPosition
- End Get
- Set(value As Point)
- Me.Canvas.Invalidate(Me.DrawingLocation.Area)
- Me.CrossHairPosition = value
- Me.MoveCrossHair()
- End Set
- End Property
- Public Property DrawingPen As Pen
- Public Property LineStyle As DashStyle
- Public Property PenColor As Color
- Public Property PenSize As Single
- Public Property CrossHairSize As Size
- Private Sub MoveCrossHair()
- If Me.Canvas IsNot Nothing Then
- DrawingLocation.Measure(Me.CrossHairPosition, Me.CrossHairSize, Me.PenSize)
- Me.Canvas.Invalidate(Me.DrawingLocation.Area)
- End If
- End Sub
- Public Sub SetPen()
- Me.DrawingPen = New Pen(Me.PenColor, Me.PenSize)
- Me.DrawingPen.DashStyle = LineStyle
- End Sub
- Protected Sub PaintOnCanvas(Surface As Object, args As PaintEventArgs)
- If DrawingLocation.Area.Width > 0 Then
- args.Graphics.DrawLine(Me.DrawingPen, DrawingLocation.LineX.Item1, DrawingLocation.LineX.Item2)
- args.Graphics.DrawLine(Me.DrawingPen, DrawingLocation.LineY.Item1, DrawingLocation.LineY.Item2)
- End If
- End Sub
- Private Class CHLocation
- Public Sub New(CHPosition As Point, CHSize As Size, CHPenSize As Single)
- Me.Measure(CHPosition, CHSize, CHPenSize)
- End Sub
- Public Area As Rectangle
- Public LineX As Tuple(Of Point, Point)
- Public LineY As Tuple(Of Point, Point)
- Public Sub Measure(RefPosition As Point, RefSize As Size, RefPenSize As Single)
- If RefPosition = Point.Empty Then Return
- Me.Area = New Rectangle(New Point(RefPosition.X - RefSize.Width,
- RefPosition.Y - RefSize.Height),
- New Size(RefSize.Width * 2, RefSize.Height * 2))
- Me.Area.Inflate(CInt(RefPenSize / 2) + 1, CInt(RefPenSize / 2) + 1)
- Dim PointX1 As Point = New Point(RefPosition.X - RefSize.Width, RefPosition.Y)
- Dim PointX2 As Point = New Point(RefPosition.X + RefSize.Width, RefPosition.Y)
- Dim PointY1 As Point = New Point(RefPosition.X, RefPosition.Y - RefSize.Height)
- Dim PointY2 As Point = New Point(RefPosition.X, RefPosition.Y + RefSize.Height)
- Me.LineX = Tuple.Create(PointX1, PointX2)
- Me.LineY = Tuple.Create(PointY1, PointY2)
- End Sub
- End Class
- End Class
- -----------------------------------------------------------------------------------
- Private DrawCrossHair As CrossHair
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- DrawCrossHair = New CrossHair(Me, New Point(100, 100))
- End Sub
- -----------------------------------------------------------------------------------
- 'Draw a CrossHair when a Mouse button is clicked
- Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
- DrawCrossHair.Position = e.Location
- End Sub
- or
- 'Follows the Mouse pointer when it moves
- Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
- DrawCrossHair.Position = e.Location
- End Sub
Advertisement
Comments
-
- Hi JIMI2000, I tried loading your code in VB 2022, however nothing. I was wondering if you updated the code or have any suggestions. Thanks Chesley.
Add Comment
Please, Sign In to add comment