VB.Net/2D/Hatch Brush

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

Hatch Brushes Demo: names and effects

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
Public Class MainClass
    Shared Sub Main()
        Dim myform As Form = New HatchBrushesForm()
        Application.Run(myform)
    End Sub
End Class
Public Class HatchBrushesForm
    Inherits System.Windows.Forms.Form
#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()
        "
        "HatchBrushesForm
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Name = "HatchBrushesForm"
        Me.Text = "HatchBrushesForm"
    End Sub
#End Region

    Private Sub HatchBrushesForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim hatchNames As String() = System.Enum.GetNames(GetType(HatchStyle))
        Dim x As Integer = 0
        Dim y As Integer = 0
        Dim width As Integer = Me.ClientRectangle.Width / 4
        Dim height As Integer = Me.ClientRectangle.Height / (hatchNames.Length / 4)
        Dim blackBrush As Brush = Brushes.Black
        Dim blackPen As Pen = Pens.Black
        Array.Sort(hatchNames)
        Dim hatchName As String
        For Each hatchName In hatchNames
            Dim style As HatchStyle = CType(System.Enum.Parse(GetType(HatchStyle), hatchName), HatchStyle)
            Dim b As HatchBrush = New HatchBrush(style, Color.Black, Color.White)
            g.FillRectangle(b, x, CInt(y + height / 2), width, CInt(height / 2))
            g.DrawRectangle(blackPen, x, y, width, height)
            g.DrawString(hatchName, Me.Font, blackBrush, x, y)
            x = x + width
            If x > (Me.ClientRectangle.Width - width) Then
                y = y + height
                x = 0
            End If
        Next
    End Sub
End Class


Hatch Brush Hatch Style: Backward Diagonal

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
#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
#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 h As HatchBrush = New HatchBrush(HatchStyle.BackwardDiagonal, _
                                    Color.Black, _
                                    Color.Cyan)
    Dim P As Pen = New Pen(h, 20)
    e.Graphics.Clear(Color.AliceBlue)
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
    e.Graphics.DrawLine(P, 100, 200, 150, 200)
    e.Graphics.FillEllipse(h, 50, 50, 150, 150)
    P.Dispose()
    h.Dispose()
  End Sub
End Class


HatchStyle Shingle List

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
        DrawingHatchStyles(e.Graphics)
    End Sub
    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        DrawingHatchStyles(Me.CreateGraphics)
    End Sub
    Private Sub DrawingHatchStyles(ByVal gr As Graphics)
        Dim x As Integer = 10
        Dim y As Integer = 20
        gr.Clear(Me.BackColor)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.BackwardDiagonal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Cross)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DarkDownwardDiagonal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DarkHorizontal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DarkUpwardDiagonal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DarkVertical)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DashedDownwardDiagonal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DashedHorizontal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DashedUpwardDiagonal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DashedVertical)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DiagonalBrick)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DiagonalCross)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Divot)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DottedDiamond)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.DottedGrid)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.ForwardDiagonal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Horizontal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.HorizontalBrick)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.LargeCheckerBoard)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.LargeConfetti)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.LargeGrid)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.LightDownwardDiagonal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.LightHorizontal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.LightUpwardDiagonal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.LightVertical)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.NarrowHorizontal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.NarrowVertical)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.OutlinedDiamond)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent05)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent10)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent20)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent25)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent30)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent40)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent50)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent60)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent70)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent75)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent80)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Percent90)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Plaid)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Shingle)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.SmallCheckerBoard)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.SmallConfetti)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.SmallGrid)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.SolidDiamond)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Sphere)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Trellis)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Vertical)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Wave)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.Weave)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.WideDownwardDiagonal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.WideUpwardDiagonal)
        DrawingHatchStyle(gr, x, y, Drawing2D.HatchStyle.ZigZag)
    End Sub
    Private Sub DrawingHatchStyle(ByVal gr As Graphics, ByRef x As Integer, ByRef y As Integer, ByVal hatch_style As Drawing2D.HatchStyle)
        gr.DrawString(hatch_style.ToString, Me.Font, Brushes.Black, x, y)
        Dim the_brush As New Drawing2D.HatchBrush(hatch_style, Color.Black, Color.White)
        gr.FillRectangle(the_brush, x, y + 15, 100, 20)
        y += 50
        If y + 50 > Me.ClientRectangle.Height Then
            x += 150
            y = 20
        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