Jimi2000

CrossHair Class - vb.net

Apr 24th, 2018
970
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.65 KB | None | 0 0
  1.     Public Class CrossHair
  2.  
  3.         Private Canvas As Control = Nothing
  4.         Private CrossHairPosition As Point = Point.Empty
  5.         Private DrawingLocation As CHLocation
  6.         Public Sub New(DrawingSurface As Control, InitialPosition? As Point)
  7.             Me.CrossHairSize = New Size(50, 50)
  8.             Me.LineStyle = DashStyle.Solid
  9.             Me.PenColor = Color.Red
  10.             Me.PenSize = 3
  11.             Me.SetPen()
  12.  
  13.             Me.Canvas = DrawingSurface
  14.             If InitialPosition = Nothing Then
  15.                 Me.CrossHairPosition = If(Me.Canvas Is Nothing,
  16.                                           Point.Empty,
  17.                                           New Point((Me.Canvas.Left + Me.Canvas.Width) \ 2,
  18.                                                     (Me.Canvas.Top + Me.Canvas.Height) \ 2))
  19.                 Me.MoveCrossHair()
  20.             End If
  21.             Me.DrawingLocation = New CHLocation(Me.CrossHairPosition, Me.CrossHairSize, Me.PenSize)
  22.             If DrawingSurface IsNot Nothing Then
  23.                 AddHandler Me.Canvas.Paint, AddressOf Me.PaintOnCanvas
  24.             End If
  25.         End Sub
  26.  
  27.         Public Property Position() As Point
  28.             Get
  29.                 Return Me.CrossHairPosition
  30.             End Get
  31.             Set(value As Point)
  32.                 Me.Canvas.Invalidate(Me.DrawingLocation.Area)
  33.                 Me.CrossHairPosition = value
  34.                 Me.MoveCrossHair()
  35.             End Set
  36.         End Property
  37.  
  38.         Public Property DrawingPen As Pen
  39.         Public Property LineStyle As DashStyle
  40.         Public Property PenColor As Color
  41.         Public Property PenSize As Single
  42.         Public Property CrossHairSize As Size
  43.  
  44.         Private Sub MoveCrossHair()
  45.             If Me.Canvas IsNot Nothing Then
  46.                 DrawingLocation.Measure(Me.CrossHairPosition, Me.CrossHairSize, Me.PenSize)
  47.                 Me.Canvas.Invalidate(Me.DrawingLocation.Area)
  48.             End If
  49.         End Sub
  50.  
  51.         Public Sub SetPen()
  52.             Me.DrawingPen = New Pen(Me.PenColor, Me.PenSize)
  53.             Me.DrawingPen.DashStyle = LineStyle
  54.         End Sub
  55.  
  56.         Protected Sub PaintOnCanvas(Surface As Object, args As PaintEventArgs)
  57.             If DrawingLocation.Area.Width > 0 Then
  58.                 args.Graphics.DrawLine(Me.DrawingPen, DrawingLocation.LineX.Item1, DrawingLocation.LineX.Item2)
  59.                 args.Graphics.DrawLine(Me.DrawingPen, DrawingLocation.LineY.Item1, DrawingLocation.LineY.Item2)
  60.             End If
  61.         End Sub
  62.  
  63.         Private Class CHLocation
  64.             Public Sub New(CHPosition As Point, CHSize As Size, CHPenSize As Single)
  65.                 Me.Measure(CHPosition, CHSize, CHPenSize)
  66.             End Sub
  67.             Public Area As Rectangle
  68.             Public LineX As Tuple(Of Point, Point)
  69.             Public LineY As Tuple(Of Point, Point)
  70.  
  71.             Public Sub Measure(RefPosition As Point, RefSize As Size, RefPenSize As Single)
  72.                 If RefPosition = Point.Empty Then Return
  73.                 Me.Area = New Rectangle(New Point(RefPosition.X - RefSize.Width,
  74.                                                   RefPosition.Y - RefSize.Height),
  75.                                         New Size(RefSize.Width * 2, RefSize.Height * 2))
  76.  
  77.                 Me.Area.Inflate(CInt(RefPenSize / 2) + 1, CInt(RefPenSize / 2) + 1)
  78.                 Dim PointX1 As Point = New Point(RefPosition.X - RefSize.Width, RefPosition.Y)
  79.                 Dim PointX2 As Point = New Point(RefPosition.X + RefSize.Width, RefPosition.Y)
  80.                 Dim PointY1 As Point = New Point(RefPosition.X, RefPosition.Y - RefSize.Height)
  81.                 Dim PointY2 As Point = New Point(RefPosition.X, RefPosition.Y + RefSize.Height)
  82.  
  83.                 Me.LineX = Tuple.Create(PointX1, PointX2)
  84.                 Me.LineY = Tuple.Create(PointY1, PointY2)
  85.  
  86.             End Sub
  87.         End Class
  88.  
  89.     End Class
  90.  
  91. -----------------------------------------------------------------------------------
  92.  
  93.     Private DrawCrossHair As CrossHair
  94.  
  95.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  96.         DrawCrossHair = New CrossHair(Me, New Point(100, 100))
  97.     End Sub
  98.  
  99. -----------------------------------------------------------------------------------
  100.  
  101.     'Draw a CrossHair when a Mouse button is clicked
  102.     Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
  103.         DrawCrossHair.Position = e.Location
  104.     End Sub
  105.  
  106. or
  107.     'Follows the Mouse pointer when it moves
  108.     Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
  109.         DrawCrossHair.Position = e.Location
  110.     End Sub
Advertisement
Comments
  • Chesley
    2 years
    # text 0.15 KB | 0 0
    1. 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