VB.Net Tutorial/2D Graphics/StringFormat

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

Default String Format Flags

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Math public class DefaultStringFormatFlag

  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 txt As String = "The quick brown fox jumps over the lazy dog."
       Dim the_font As New Font("Garamond", 40, FontStyle.Italic, GraphicsUnit.Pixel)
       Dim layout_rect As RectangleF
       Dim string_format As New StringFormat
       string_format.Alignment = StringAlignment.Near
       string_format.LineAlignment = StringAlignment.Near
       e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit
       layout_rect = New RectangleF(10, 20, 200, 70)
       e.Graphics.DrawString("<default>", Me.Font, Brushes.Black, layout_rect.X, layout_rect.Y - 20)
       e.Graphics.DrawRectangle(Pens.White, Rectangle.Round(layout_rect))
       e.Graphics.DrawString(txt, the_font, Brushes.Black, layout_rect, string_format)
 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>

Draw String Layout Rectangle

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Math public class DrawStringLayoutRectangle

  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 txt As String = "The quick brown fox jumps over the lazy dog."
       Dim big_font As New Font("Times New Roman", 30, FontStyle.Bold)
       Dim layout_rect As New RectangleF(10, 10, Me.ClientSize.Width - 20, Me.ClientSize.Height - 20)
       Dim string_format As New StringFormat
       string_format.Alignment = StringAlignment.Center
       string_format.LineAlignment = StringAlignment.Near
       e.Graphics.DrawString(txt, big_font, Brushes.Black, layout_rect, string_format)
       e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(layout_rect))
   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>

Set Tab stop

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Math public class SetTabs

  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 the_font As New Font("Times New Roman", 15, FontStyle.Regular, GraphicsUnit.Pixel)
       Dim layout_rect As New RectangleF(10, 10, Me.ClientSize.Width - 20, Me.ClientSize.Height - 20)
       Dim string_format As New StringFormat
       Dim tab_stops() As Single = {60, 80}
       string_format.SetTabStops(0, tab_stops)
       e.Graphics.DrawRectangle(Pens.White, Rectangle.Round(layout_rect))
       Dim x As Single = layout_rect.X
       For i As Integer = 0 To tab_stops.Length - 1
           x += tab_stops(i)
           e.Graphics.DrawLine(Pens.White, x, layout_rect.Top, x, layout_rect.Bottom)
       Next i
       Dim rnd As New Random
       Dim txt As String = "A" & vbTab & "BBB" & vbTab & "C" & vbCrLf
       For r As Integer = 1 To 10
           txt &= rnd.Next(10, 99) & vbTab & rnd.NextDouble.ToString("0.000000") & vbTab & rnd.NextDouble.ToString("0.00") & vbCrLf
       Next r
       e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit
       e.Graphics.DrawString(txt, the_font, Brushes.Black, layout_rect, string_format)
 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>

StringFormatFlags.LineLimit

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Math public class LineLimitStringFormatFlag

  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 txt As String = "The quick brown fox jumps over the lazy dog."
       Dim the_font As New Font("Garamond", 40, FontStyle.Italic, GraphicsUnit.Pixel)
       Dim layout_rect As RectangleF
       Dim string_format As New StringFormat
       string_format.Alignment = StringAlignment.Near
       string_format.LineAlignment = StringAlignment.Near
       e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit
       layout_rect = New RectangleF(10, 20, 200, 70)
       string_format.FormatFlags = StringFormatFlags.LineLimit
       e.Graphics.DrawString("LineLimit", Me.Font, Brushes.Black, layout_rect.X, layout_rect.Y - 20)
       e.Graphics.DrawRectangle(Pens.White, Rectangle.Round(layout_rect))
       e.Graphics.DrawString(txt, the_font, Brushes.Black, layout_rect, string_format)
 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>

StringFormatFlags.NoClip

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Math public class NoClipStringFormatFlag

  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 txt As String = "The quick brown fox jumps over the lazy dog."
       Dim the_font As New Font("Garamond", 40, FontStyle.Italic, GraphicsUnit.Pixel)
       Dim layout_rect As RectangleF
       Dim string_format As New StringFormat
       string_format.Alignment = StringAlignment.Near
       string_format.LineAlignment = StringAlignment.Near
       e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit
       layout_rect = New RectangleF(10, 20, 200, 70)
       string_format.FormatFlags = StringFormatFlags.NoClip
       e.Graphics.DrawString("NoClip", Me.Font, Brushes.Black, layout_rect.X, layout_rect.Y - 20)
       e.Graphics.DrawRectangle(Pens.White, Rectangle.Round(layout_rect))
       e.Graphics.DrawString(txt, the_font, Brushes.Black, layout_rect, string_format)
 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>

StringFormat.LineAlignment = StringAlignment.Center

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class LineAlignmentStringAlignmentCenter

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       Dim canvas As Graphics = e.Graphics
       
       Dim mainFont As Font
       Dim textStyle As New FontStyle
       mainFont = New Font("Arial", 20, textStyle)
       
       Dim textArea As Rectangle = New Rectangle(20, 20, 500, 200)
       Dim textFormat As StringFormat = New StringFormat
       textFormat.LineAlignment = StringAlignment.Center
       e.Graphics.DrawRectangle(Pens.Gray, textArea)
       e.Graphics.DrawString("www.vbex.ru", mainFont, Brushes.Black, textArea, textFormat)
       canvas = Nothing
   End Sub

End Class

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

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected 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(610, 328)
       Me.ResumeLayout(False)
   End Sub

End Class</source>

StringFormat.LineAlignment = StringAlignment.Far

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class LineAlignmentStringAlignmentFar

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       Dim canvas As Graphics = e.Graphics
       
       Dim mainFont As Font
       Dim textStyle As New FontStyle
       mainFont = New Font("Arial", 20, textStyle)
       
       Dim textArea As Rectangle = New Rectangle(20, 20, 500, 200)
       Dim textFormat As StringFormat = New StringFormat
       textFormat.LineAlignment = StringAlignment.Far
       e.Graphics.DrawRectangle(Pens.Gray, textArea)
       e.Graphics.DrawString("www.vbex.ru", mainFont, Brushes.Black, textArea, textFormat)
       canvas = Nothing
   End Sub

End Class

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

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected 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(610, 328)
       Me.ResumeLayout(False)
   End Sub

End Class</source>

StringFormat.LineAlignment = StringAlignment.Near

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class LineAlignmentStringAlignmentNear

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
       Dim canvas As Graphics = e.Graphics
       
       Dim mainFont As Font
       Dim textStyle As New FontStyle
       mainFont = New Font("Arial", 20, textStyle)
       
       Dim textArea As Rectangle = New Rectangle(20, 20, 500, 200)
       Dim textFormat As StringFormat = New StringFormat
       textFormat.LineAlignment = StringAlignment.Near
       e.Graphics.DrawRectangle(Pens.Gray, textArea)
       e.Graphics.DrawString("www.vbex.ru", mainFont, Brushes.Black, textArea, textFormat)
       canvas = Nothing
   End Sub

End Class

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

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected 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(610, 328)
       Me.ResumeLayout(False)
   End Sub

End Class</source>