C
C
I get this thing to work fine but I am writing very inefficient and
dirty code with LinearGradientBrush. Either something is missing in
VB.net or I am doing something wrong.
Trouble 1:
For example, I have written
Dim arrowBrush As System.Drawing.Drawing2D.LinearGradientBrush
= New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White,
Color.Black, System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
Dim arrowBrushV As
System.Drawing.Drawing2D.LinearGradientBrush = New
System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White,
Color.Black, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
If hv <> 1 Then arrowBrush = arrowBrushV ' not horizontal =
vertical
This is because arrowBrush does not have a property called
LinearGradientMode. Is there a better way to do this? arrowBrush has a
horizontal gradient; arrowBrushV has a vertical. I define two brushes
and then set the first one to second if the arrow should be vertical
(hv not equal to 1).
Trouble 2:
Another thing that does not work is
arrowBrush.LinearColors(0) = arrowColor1
'Color.FromArgb(0, 201, 170)
arrowBrush.LinearColors(1) = arrowColor2 '
Color.FromArgb(255, 0, 0)
This seems to be a VB.net bug because it does not change the colours
at all.
arrowBrush.LinearColors = New Color() {arrowColor1,
arrowColor2}
works.
Question 3:
Do I need to dispose the brushes or do they disappear anyway at the
end of the subroutine?
The full subroutine is appended below. It works. Everyone is free to
use it.
Sub drawArrow(ByVal g As Graphics, ByVal hv As Short, ByVal
arrowStart As Point, ByVal arrowLength As Short, ByVal arrowThick As
Short, ByVal arrowWidth As Short, ByVal arrowColor1 As Color, ByVal
arrowColor2 As Color)
Dim arrowPoints(6) As Point
Dim rect As Rectangle = New Rectangle(10, 10, 6, 4)
If hv = 1 Then
rect.X = arrowStart.X
rect.Y = arrowStart.Y
rect.Height = arrowWidth
rect.Width = arrowLength + arrowThick + 1
Else
rect.X = arrowStart.X
rect.Y = arrowStart.Y
rect.Height = arrowLength + arrowThick + 1
rect.Width = arrowWidth
End If
Dim arrowBrush As System.Drawing.Drawing2D.LinearGradientBrush
= New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White,
Color.Black, System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
Dim arrowBrushV As
System.Drawing.Drawing2D.LinearGradientBrush = New
System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White,
Color.Black, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
If hv <> 1 Then arrowBrush = arrowBrushV
' arrowBrush.LinearColors(0) = arrowColor1
'Color.FromArgb(0, 201, 170)
' arrowBrush.LinearColors(1) = arrowColor2 '
Color.FromArgb(255, 0, 0)
arrowBrush.LinearColors = New Color() {arrowColor1,
arrowColor2}
If hv = 1 Then ' horizontal
arrowPoints(0).X = arrowStart.X
arrowPoints(0).Y = arrowStart.Y - arrowThick / 2
arrowPoints(1).X = arrowPoints(0).X + arrowLength
arrowPoints(1).Y = arrowPoints(0).Y
arrowPoints(2).X = arrowPoints(1).X
arrowPoints(2).Y = arrowPoints(1).Y - (arrowWidth -
arrowThick) / 2
arrowPoints(3).X = arrowPoints(2).X + arrowThick
arrowPoints(3).Y = arrowStart.Y
arrowPoints(4).X = arrowPoints(2).X
arrowPoints(4).Y = arrowPoints(2).Y + arrowWidth
arrowPoints(5).X = arrowPoints(2).X
arrowPoints(5).Y = arrowStart.Y + arrowThick / 2
arrowPoints(6).X = arrowStart.X
arrowPoints(6).Y = arrowPoints(5).Y
Else
arrowPoints(0).X = arrowStart.X - arrowThick / 2
arrowPoints(0).Y = arrowStart.Y
arrowPoints(1).X = arrowPoints(0).X
arrowPoints(1).Y = arrowPoints(0).Y - arrowLength
arrowPoints(2).X = arrowPoints(1).X - (arrowWidth -
arrowThick) / 2
arrowPoints(2).Y = arrowPoints(1).Y
arrowPoints(3).X = arrowStart.X ' tip
arrowPoints(3).Y = arrowPoints(2).Y - arrowThick
arrowPoints(4).X = arrowPoints(2).X + arrowWidth
arrowPoints(4).Y = arrowPoints(2).Y
arrowPoints(5).X = arrowStart.X + arrowThick / 2
arrowPoints(5).Y = arrowPoints(2).Y
arrowPoints(6).X = arrowPoints(5).X
arrowPoints(6).Y = arrowStart.Y
End If
g.FillPolygon(arrowBrush, arrowPoints)
' g.DrawPolygon(Pens.White, arrowPoints)
Me.Invalidate()
End Sub
dirty code with LinearGradientBrush. Either something is missing in
VB.net or I am doing something wrong.
Trouble 1:
For example, I have written
Dim arrowBrush As System.Drawing.Drawing2D.LinearGradientBrush
= New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White,
Color.Black, System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
Dim arrowBrushV As
System.Drawing.Drawing2D.LinearGradientBrush = New
System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White,
Color.Black, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
If hv <> 1 Then arrowBrush = arrowBrushV ' not horizontal =
vertical
This is because arrowBrush does not have a property called
LinearGradientMode. Is there a better way to do this? arrowBrush has a
horizontal gradient; arrowBrushV has a vertical. I define two brushes
and then set the first one to second if the arrow should be vertical
(hv not equal to 1).
Trouble 2:
Another thing that does not work is
arrowBrush.LinearColors(0) = arrowColor1
'Color.FromArgb(0, 201, 170)
arrowBrush.LinearColors(1) = arrowColor2 '
Color.FromArgb(255, 0, 0)
This seems to be a VB.net bug because it does not change the colours
at all.
arrowBrush.LinearColors = New Color() {arrowColor1,
arrowColor2}
works.
Question 3:
Do I need to dispose the brushes or do they disappear anyway at the
end of the subroutine?
The full subroutine is appended below. It works. Everyone is free to
use it.
Sub drawArrow(ByVal g As Graphics, ByVal hv As Short, ByVal
arrowStart As Point, ByVal arrowLength As Short, ByVal arrowThick As
Short, ByVal arrowWidth As Short, ByVal arrowColor1 As Color, ByVal
arrowColor2 As Color)
Dim arrowPoints(6) As Point
Dim rect As Rectangle = New Rectangle(10, 10, 6, 4)
If hv = 1 Then
rect.X = arrowStart.X
rect.Y = arrowStart.Y
rect.Height = arrowWidth
rect.Width = arrowLength + arrowThick + 1
Else
rect.X = arrowStart.X
rect.Y = arrowStart.Y
rect.Height = arrowLength + arrowThick + 1
rect.Width = arrowWidth
End If
Dim arrowBrush As System.Drawing.Drawing2D.LinearGradientBrush
= New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White,
Color.Black, System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
Dim arrowBrushV As
System.Drawing.Drawing2D.LinearGradientBrush = New
System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White,
Color.Black, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
If hv <> 1 Then arrowBrush = arrowBrushV
' arrowBrush.LinearColors(0) = arrowColor1
'Color.FromArgb(0, 201, 170)
' arrowBrush.LinearColors(1) = arrowColor2 '
Color.FromArgb(255, 0, 0)
arrowBrush.LinearColors = New Color() {arrowColor1,
arrowColor2}
If hv = 1 Then ' horizontal
arrowPoints(0).X = arrowStart.X
arrowPoints(0).Y = arrowStart.Y - arrowThick / 2
arrowPoints(1).X = arrowPoints(0).X + arrowLength
arrowPoints(1).Y = arrowPoints(0).Y
arrowPoints(2).X = arrowPoints(1).X
arrowPoints(2).Y = arrowPoints(1).Y - (arrowWidth -
arrowThick) / 2
arrowPoints(3).X = arrowPoints(2).X + arrowThick
arrowPoints(3).Y = arrowStart.Y
arrowPoints(4).X = arrowPoints(2).X
arrowPoints(4).Y = arrowPoints(2).Y + arrowWidth
arrowPoints(5).X = arrowPoints(2).X
arrowPoints(5).Y = arrowStart.Y + arrowThick / 2
arrowPoints(6).X = arrowStart.X
arrowPoints(6).Y = arrowPoints(5).Y
Else
arrowPoints(0).X = arrowStart.X - arrowThick / 2
arrowPoints(0).Y = arrowStart.Y
arrowPoints(1).X = arrowPoints(0).X
arrowPoints(1).Y = arrowPoints(0).Y - arrowLength
arrowPoints(2).X = arrowPoints(1).X - (arrowWidth -
arrowThick) / 2
arrowPoints(2).Y = arrowPoints(1).Y
arrowPoints(3).X = arrowStart.X ' tip
arrowPoints(3).Y = arrowPoints(2).Y - arrowThick
arrowPoints(4).X = arrowPoints(2).X + arrowWidth
arrowPoints(4).Y = arrowPoints(2).Y
arrowPoints(5).X = arrowStart.X + arrowThick / 2
arrowPoints(5).Y = arrowPoints(2).Y
arrowPoints(6).X = arrowPoints(5).X
arrowPoints(6).Y = arrowStart.Y
End If
g.FillPolygon(arrowBrush, arrowPoints)
' g.DrawPolygon(Pens.White, arrowPoints)
Me.Invalidate()
End Sub