VB.Net Tutorial/2D Graphics/HatchBrush
Содержание
- 1 Create HatchBrush from HatchBrush style and two colors
- 2 Get All HatchStyles
- 3 HatchBrush HatchStyle BackwardDiagonal
- 4 HatchStyle.DarkDownwardDiagonal
- 5 HatchStyle.DashedVertical
- 6 HatchStyle.LightUpwardDiagonal
- 7 HatchStyle.SmallCheckerBoard
- 8 List all Hatch styles
- 9 Use HatchBrush with HatchStyle.Horizontal to draw an arrow
- 10 Use the HatchBrush
Create HatchBrush from HatchBrush style and two colors
<source lang="vbnet">Imports System Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data public class HatchBrushCreateHacthStyleAndColors
public Shared Sub Main Application.Run(New BrushesForm) End Sub
End class Public Class BrushesForm
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() " "BrushesForm " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Name = "BrushesForm" Me.Text = "Form1" End Sub
- End Region
Enum LinearGradientMode BackwardDiagonal ForwardDiagonal Horizontal Vertical End Enum Private Sub BrushesForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint Dim g As Graphics = e.Graphics Dim width As Integer = Me.ClientRectangle.Width Dim height As Integer = Me.ClientRectangle.Height / 5 Dim whiteBrush As Brush = System.Drawing.Brushes.White Dim blackBrush As Brush = System.Drawing.Brushes.Black Dim b As Brush Dim file As String = "YourFile.jpg" b = New HatchBrush(HatchStyle.Divot, Color.DarkBlue, Color.White) g.FillRectangle(b, 0, 0, width, height) g.DrawString(b.ToString(), Me.Font, whiteBrush, 0, 0) End Sub
End Class</source>
Get All HatchStyles
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class HatchBrushesHatchStyle
public Shared Sub Main Application.Run(New HatchBrushesForm) 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(600, 600) 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</source>
HatchBrush HatchStyle BackwardDiagonal
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class HatchBrushHatchStyleBackwardDiagonalBlackCyan
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form 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, 80, 90, 80, 200) e.Graphics.FillEllipse(h, 50, 50, 50, 30) P.Dispose() h.Dispose() End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
HatchStyle.DarkDownwardDiagonal
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class HatchStyleDarkDownwardDiagonal
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics " Dim hBrush2 As New HatchBrush(HatchStyle.DarkDownwardDiagonal, Color.Green, Color.Black) g.FillRectangle(hBrush2, New Rectangle(150, 80, 200, 140)) End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
HatchStyle.DashedVertical
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class HatchStyleDashedVertical
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics " Dim hBrush1 As New HatchBrush(HatchStyle.DashedVertical, Color.Chocolate, Color.Red) g.FillEllipse(hBrush1, 20, 40, 100, 120) End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
HatchStyle.LightUpwardDiagonal
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class HatchStyleLightUpwardDiagonal
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim recDrawingRectangle As Rectangle recDrawingRectangle = New Rectangle(30, 170, 100, 70) Dim hb As New HatchBrush(HatchStyle.LightUpwardDiagonal, _ Color.Aquamarine) e.Graphics.FillRectangle(hb, recDrawingRectangle) End Sub Public Sub New() Me.ClientSize = New System.Drawing.Size(292, 273) End Sub
End Class</source>
HatchStyle.SmallCheckerBoard
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class HatchStyleSmallCheckerBoard
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics " Dim hBrush3 As New HatchBrush(HatchStyle.SmallCheckerBoard, Color.BlueViolet, Color.Blue) Dim rect As New Rectangle(150, 80, 200, 140) g.FillPie(hBrush3, 40, 20, 200, 40, 0.0F, 60.0F) End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
List all Hatch styles
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class HatchStyle
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim x As Integer = 10 Dim y As Integer = 20 e.Graphics.Clear(Me.BackColor) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.BackwardDiagonal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Cross) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DarkDownwardDiagonal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DarkHorizontal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DarkUpwardDiagonal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DarkVertical) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DashedDownwardDiagonal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DashedHorizontal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DashedUpwardDiagonal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DashedVertical) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DiagonalBrick) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DiagonalCross) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Divot) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DottedDiamond) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.DottedGrid) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.ForwardDiagonal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Horizontal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.HorizontalBrick) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.LargeCheckerBoard) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.LargeConfetti) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.LargeGrid) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.LightDownwardDiagonal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.LightHorizontal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.LightUpwardDiagonal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.LightVertical) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.NarrowHorizontal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.NarrowVertical) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.OutlinedDiamond) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent05) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent10) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent20) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent25) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent30) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent40) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent50) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent60) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent70) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent75) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent80) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Percent90) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Plaid) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Shingle) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.SmallCheckerBoard) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.SmallConfetti) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.SmallGrid) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.SolidDiamond) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Sphere) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Trellis) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Vertical) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Wave) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.Weave) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.WideDownwardDiagonal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.WideUpwardDiagonal) DrawSample(e.Graphics, x, y, Drawing2D.HatchStyle.ZigZag) End Sub Private Sub DrawSample(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 += 40 If y + 40 > Me.ClientRectangle.Height Then x += 150 y = 20 End If End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(600, 600) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
Use HatchBrush with HatchStyle.Horizontal to draw an arrow
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class DrawArrowHatchStyleHorizontal
public Shared Sub Main Application.Run(New Form1) End Sub
End class public class Form1
Inherits System.Windows.Forms.Form Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) e.Graphics.ScaleTransform(30, 30, MatrixOrder.Append) DrawArrow(e.Graphics, HatchStyle.Horizontal) End Sub Private Sub DrawArrow(ByVal gr As Graphics, ByVal hatch_style As HatchStyle) Dim pts() As Point = { _ New Point(0, 1), _ New Point(2, 1), _ New Point(2, 0), _ New Point(4, 2), _ New Point(2, 4), _ New Point(2, 3), _ New Point(0, 3) _ } gr.FillPolygon(New HatchBrush(hatch_style, Color.Black, Color.White), pts) gr.DrawPolygon(New Pen(Color.Black, 0), pts) End Sub Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen End Sub
End Class</source>
Use the HatchBrush
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class BrushDemo
public Shared Sub Main Application.Run(New Form1) End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent()
End Sub 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 Private components As System.ruponentModel.IContainer Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.PictureBox1 = New System.Windows.Forms.PictureBox Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() " "PictureBox1 " Me.PictureBox1.BackColor = System.Drawing.SystemColors.Window Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Top Me.PictureBox1.Location = New System.Drawing.Point(0, 0) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(336, 168) Me.PictureBox1.TabIndex = 0 Me.PictureBox1.TabStop = False " "Button1 " Me.Button1.Location = New System.Drawing.Point(112, 184) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(80, 24) Me.Button1.TabIndex = 1 Me.Button1.Text = "Draw" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(336, 238) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.PictureBox1) Me.ResumeLayout(False) End Sub Dim brush1 As System.Drawing.Brush Dim g As System.Drawing.Graphics Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click brush1 = New System.Drawing.Drawing2D.HatchBrush(Drawing.Drawing2D.HatchStyle.Cross, Color.Red, Color.Green) PictureBox1.Refresh() g = PictureBox1.CreateGraphics g.FillEllipse(brush1, 70, 10, 100, 150) End Sub
End Class</source>