We create a
Windowsapplication
and a MainMenu control with three menu items on the form. We call these menu items GetCapStyle, LineDashStyle, and LineDashCap, respectively, and write menu click event handlers by double-clicking on them. On the GetCapStyle menu item click event handler, we will read different line caps and generate output using these line caps; on the LineDashStyle menu item click event handler, we will generate lines with different dash styles; and on the LineDashCap menu item click event handler, we will generate output with different line dash caps.
The GetCapStyle menu item click event handler is shown in Listing 9.2. We create a pen and set the starting and ending caps using the StartCap and EndCap properties of the Pen Object, and then we draw a line.
LISTING 9.2: Getting line caps
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Namespace GettingSetting_Line_CapsStyles
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Paint(ByVal sender As Object,ByVal e As PaintEventArgs)
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Me.BackColor)
' Create a pen
Dim blackPenAs New Pen(Color.Black, 10)
' Set line styles
blackPen.StartCap = LineCap.Triangle
blackPen.EndCap = LineCap.Triangle
g.DrawLine(blackPen, 20, 10, 200, 10)
blackPen.StartCap = LineCap.Square
blackPen.EndCap = LineCap.AnchorMask
g.DrawLine(blackPen, 20, 50, 200, 50)
blackPen.StartCap = LineCap.DiamondAnchor
blackPen.EndCap = LineCap.DiamondAnchor
g.DrawLine(blackPen, 20, 70, 200, 70)
blackPen.StartCap = LineCap.Flat
blackPen.EndCap = LineCap.Flat
g.DrawLine(blackPen, 20, 110, 200, 110)
blackPen.StartCap = LineCap.RoundAnchor
blackPen.EndCap = LineCap.RoundAnchor
g.DrawLine(blackPen, 20, 130, 200, 130)
blackPen.StartCap = LineCap.Square
blackPen.EndCap = LineCap.Square
g.DrawLine(blackPen, 20, 150, 200, 150)
blackPen.StartCap = LineCap.SquareAnchor
blackPen.EndCap = LineCap.SquareAnchor
g.DrawLine(blackPen, 20, 170, 200, 170)
blackPen.StartCap = LineCap.Flat
blackPen.EndCap = LineCap.Flat
g.DrawLine(blackPen, 20, 190, 200, 190)
' Dispose of objects
blackPen.Dispose()
g.Dispose()
End Sub
End Class
End Namespace
The output of Listing 9.2 looks like Figure 9.4, in which the lines have different caps.
The LineDashStyle menu item click event handler code is given in Listing 9.3. We create a pen and set the dash style and dash offset values using the DashStyle and DashOffset properties of the Pen object, and then we draw lines.
FIGURE 9.4: Reading line caps
LISTING 9.3: Getting line dash styles
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Namespace GettingSetting_Line_CapsStyles
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Me.BackColor)
' Create a pen
Dim blackPen As New Pen(Color.Black, 6)
' Set line styles
blackPen.DashStyle = DashStyle.Dash
blackPen.DashOffset = 40
blackPen.DashCap = DashCap.Triangle
g.DrawLine(blackPen, 20, 10, 500, 10)
blackPen.DashStyle = DashStyle.DashDot
g.DrawLine(blackPen, 20, 30, 500, 30)
blackPen.DashStyle = DashStyle.DashDotDot
g.DrawLine(blackPen, 20, 50, 500, 50)
blackPen.DashStyle = DashStyle.Dot
g.DrawLine(blackPen, 20, 70, 500, 70)
blackPen.DashStyle = DashStyle.Solid
g.DrawLine(blackPen, 20, 70, 500, 70)
' Dispose of objects
blackPen.Dispose()
g.Dispose()
End Sub
End Class
End Namespace
FIGURE 9.5: Reading line dash styles
Figure 9.5 shows the output from Listing 9.3. The lines have different dash styles.
The GetCapStyle menu item click event handler code is given in Listing 9.4. We create a pen and set the dash cap styles using the DashCap property of the Pen object.
LISTING 9.4: Getting dash caps
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Namespace GettingSetting_Line_CapsStyles
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
Dim g As Graphics = Me.CreateGraphics()
g.Clear(Me.BackColor)
' Create a pen
Dim blackPen As New Pen(Color.Black, 10)
' Set DashCap styles
blackPen.DashStyle = DashStyle.DashDotDot
blackPen.DashPattern = New Single() {10}
blackPen.DashCap = DashCap.Triangle
g.DrawLine(blackPen, 20, 10, 500, 10)
blackPen.DashCap = DashCap.Flat
g.DrawLine(blackPen, 20, 30, 500, 30)
blackPen.DashCap = DashCap.Round
g.DrawLine(blackPen, 20, 50, 500, 50)
' Dispose of objects
blackPen.Dispose()
End Sub
End Class
End Namespace
Figure 9.6 shows the output from Listing 9.4. The lines have different dash caps: triangular, flat, and round, respectively.
FIGURE 9.6: Getting line dash caps
Conclusion
Hope the article would have helped you in understanding Getting and Setting Line Caps and Styles in GDI+. Read other articles on GDI+ on the website.