VB.Net/2D/LineCaps

Материал из VB Эксперт
Перейти к: навигация, поиск

Custom Line Cap Construction

<source lang="vbnet"> Imports System Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Drawing.Imaging Public Class MainClass

 Shared Sub Main()
    Dim form1 As Form1 = new Form1
    Application.Run(form1)
 End Sub
 

End Class


Public Class Form1

 Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
 Public Sub New()
   MyBase.New()
   "This call is required by the Windows Form Designer.
   InitializeComponent()
   "Add any initialization after the InitializeComponent() call
 End Sub
 "Form overrides dispose to clean up the component list.
 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
   If disposing Then
     If Not (components Is Nothing) Then
       components.Dispose()
     End If
   End If
   MyBase.Dispose(disposing)
 End Sub
 "Required by the Windows Form Designer
 Private components As System.ruponentModel.IContainer
 "NOTE: The following procedure is required by the Windows Form Designer
 "It can be modified using the Windows Form Designer.  
 "Do not modify it using the code editor.
 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
   "
   "Form1
   "
   Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
   Me.ClientSize = New System.Drawing.Size(292, 273)
   Me.Name = "Form1"
   Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
   Me.Text = "Form1"
 End Sub
  1. End Region
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 End Sub
 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
   Dim G As Graphics = e.Graphics
   Dim P As Pen = New Pen(Color.Blue, 1)
   Dim Pts() As Point = {New Point(10, 10), _
                   New Point(15, 10), _
                   New Point(20, 15), _
                   New Point(20, 20), _
                   New Point(15, 25), _
                   New Point(10, 10)}
   Dim Path As GraphicsPath = New GraphicsPath()
   Path.AddLines(Pts)
   G.SmoothingMode = SmoothingMode.AntiAlias
   Dim MyCustomLineCap As CustomLineCap = New CustomLineCap(Nothing, Path)
   MyCustomLineCap.BaseInset = 0
   MyCustomLineCap.WidthScale = 1
   MyCustomLineCap.StrokeJoin = LineJoin.Miter
   P.CustomEndCap = MyCustomLineCap
   P.CustomStartCap = MyCustomLineCap
   G.DrawLine(P, 50, 150, 200, 150)
   MyCustomLineCap.Dispose()
   Path.Dispose()
   P.Dispose()
 End Sub

End Class

      </source>


Custom Line Caps

<source lang="vbnet"> Imports System.Drawing.Drawing2D Imports System Imports System.Drawing.Text Imports System.Drawing Imports System.Windows.Forms

Public Class MainClass

  Shared Sub Main()
      Dim form1 As Form = New Form1()
      Application.Run(form1)
  End Sub " ValidationError

End Class


Public Class Form1

   Private Sub Form1_Paint(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       " Make a GraphicsPath that draws an X.
       Dim pts() As Point = { _
           New Point(-20, -20), _
           New Point(0, 0), _
           New Point(-20, 20), _
           New Point(0, 0), _
           New Point(20, 20), _
           New Point(0, 0), _
           New Point(20, -20) _
       }
       Dim cap_path As New GraphicsPath
       cap_path.AddLines(pts)
       " Make the CustomLineCap.
       Dim x_cap As New CustomLineCap(Nothing, cap_path)
       " Draw some lines with x_cap.
       Dim the_pen As New Pen(Color.Black, 1)
       the_pen.CustomStartCap = x_cap
       the_pen.CustomEndCap = x_cap
       e.Graphics.DrawLine(the_pen, 50, 100, 200, 10)
   End Sub

End Class


<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.SuspendLayout()
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Name = "Form1"
       Me.Text = "TransformArrow"
       Me.ResumeLayout(False)
   End Sub

End Class


      </source>


LineCap Demo

<source lang="vbnet"> Imports System Imports System.Windows.Forms Imports System.Drawing.Text Imports System.Drawing Public Class MainClass

   Shared Sub Main(ByVal args As String())
       Dim myform As Form = New Form1()
       Application.Run(myform)
   End Sub

End Class

Public Class Form1

   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
       Dim x As Integer = 5
       Dim y As Integer = 10
       Dim the_pen As New Pen(Color.Black, 20)
       DrawSample(e.Graphics, x, y, Drawing2D.LineCap.AnchorMask)
       DrawSample(e.Graphics, x, y, Drawing2D.LineCap.ArrowAnchor)
       DrawSample(e.Graphics, x, y, Drawing2D.LineCap.DiamondAnchor)
       DrawSample(e.Graphics, x, y, Drawing2D.LineCap.Flat)
       DrawSample(e.Graphics, x, y, Drawing2D.LineCap.NoAnchor)
       DrawSample(e.Graphics, x, y, Drawing2D.LineCap.Round)
       DrawSample(e.Graphics, x, y, Drawing2D.LineCap.RoundAnchor)
       DrawSample(e.Graphics, x, y, Drawing2D.LineCap.Square)
       DrawSample(e.Graphics, x, y, Drawing2D.LineCap.SquareAnchor)
       DrawSample(e.Graphics, x, y, Drawing2D.LineCap.Triangle)
   End Sub
   Private Sub DrawSample(ByVal gr As Graphics, ByRef x As Integer, ByRef y As Integer, ByVal style As Drawing2D.LineCap)
       Dim the_pen As New Pen(Color.Black, 20)
       the_pen.EndCap = style
       gr.DrawString(style.ToString, Me.Font, Brushes.Black, x, y - 5)
       gr.DrawLine(the_pen, x + 100, y, x + 150, y)
       y += 50
       If y > Me.ClientRectangle.Height Then
           x += 200
           y = 80
       End If
   End Sub
   

End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.SuspendLayout()
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(564, 209)
       Me.Name = "Form1"
       Me.Text = "Title"
       Me.ResumeLayout(False)
   End Sub

End Class

      </source>


LineCaps Demo

<source lang="vbnet"> Imports System Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Imaging Public Class MainClass

   Shared Sub Main()
       Dim myform As Form = New LineCapsForm()
       Application.Run(myform)
   End Sub

End Class Public Class LineCapsForm

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
       "Add any initialization after the InitializeComponent() call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing Then
           If Not (components Is Nothing) Then
               components.Dispose()
           End If
       End If
       MyBase.Dispose(disposing)
   End Sub
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       components = New System.ruponentModel.Container()
       Me.Text = "LineCapsForm"
   End Sub
  1. End Region
   Private Sub LineCapsForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
       Dim g As Graphics = e.Graphics
       Dim capNames As String() = System.Enum.GetNames(GetType(LineCap))
       Array.Sort(capNames)
       Dim x As Integer = 0
       Dim y As Integer = 0
       Dim width As Integer = Me.ClientRectangle.Width / 2
       Dim height As Integer = Me.ClientRectangle.Height / ((capNames.Length) / 2)
       Dim blackBrush As Brush = Brushes.Black
       Dim whitePen As Pen = Pens.White
       Dim capName As String
       For Each capName In capNames
           Dim pen As Pen = New Pen(Color.Black, 12)
           pen.EndCap = CType(System.Enum.Parse(GetType(LineCap), capName), LineCap)
           If pen.EndCap <> LineCap.AnchorMask Then
               If pen.EndCap = LineCap.Custom Then
                   pen.CustomEndCap = New AdjustableArrowCap(3, 3, False)
               End If
               g.DrawLine(pen, x, CInt(y + height * 2 / 3), CInt(x + width * 2 / 3), CInt(y + height * 2 / 3))
               g.DrawLine(whitePen, x, CInt(y + height * 2 / 3), CInt(x + width * 2 / 3), CInt(y + height * 2 / 3))
               g.DrawString(capName, Me.Font, blackBrush, x, y)
               x = x + width
               If x > Me.ClientRectangle.Width - width Then
                   y = y + height
                   x = 0
               End If
           End If
       Next
   End Sub

End Class

      </source>