ananth_adroit
ananth_adroit

Reputation: 347

Custom DrawLine function between rectangles

I just want to draw some lines between the rectangles in vb.net. I used g.drawstring() method to that.

But now, based on a value I just want to change the opacity of the arrow.

Upvotes: 1

Views: 542

Answers (1)

LarsTech
LarsTech

Reputation: 81620

Here is a working example (although your comment about using g.DrawString() to draw some lines is a bit confusing). You need to create a pen with an end cap for the arrow, and set the alpha channel of the color to obtain opaqueness:

Public Class Form1
  Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias

    Dim r1 As New Rectangle(10, 10, 32, 16)
    Dim p1 As New Point(Convert.ToInt32(r1.Left + (r1.Width / 2)), Convert.ToInt32(r1.Top + (r1.Height / 2)))

    Dim r2 As New Rectangle(96, 18, 32, 16)
    Dim p2 As New Point(Convert.ToInt32(r2.Left + (r2.Width / 2)), Convert.ToInt32(r2.Top + (r2.Height / 2)))

    e.Graphics.FillRectangle(Brushes.Yellow, r1)
    e.Graphics.DrawRectangle(Pens.Black, r1)
    e.Graphics.FillRectangle(Brushes.Orange, r2)
    e.Graphics.DrawRectangle(Pens.Black, r2)

    Using alphaPen As New Pen(Color.FromArgb(120, Color.Black), 3)
      alphaPen.EndCap = LineCap.ArrowAnchor
      e.Graphics.DrawLine(alphaPen, p1, p2)
    End Using
  End Sub
End Class

This example is using an alpha value of 120. Closer to 0 makes it invisible, closer to 255 makes it fully visible.

Results (enlarged to show the see-through):

enter image description here

Upvotes: 2

Related Questions