VB.Net/2D/Text Layout
MultiLine Text Automatically Layout Demo
<source lang="vbnet"> Imports System Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Globalization Imports System.Text Imports System.Collections Public Class MainClass
Shared Sub Main() Dim myform As Form = New MultiLineTextForm() Application.Run(myform) End Sub
End Class Public Class MultiLineTextForm
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. Friend WithEvents groupBox1 As System.Windows.Forms.GroupBox Friend WithEvents automaticPanel As System.Windows.Forms.Panel <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.groupBox1 = New System.Windows.Forms.GroupBox() Me.automaticPanel = New System.Windows.Forms.Panel() Me.groupBox1.SuspendLayout() Me.SuspendLayout() Me.groupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.automaticPanel}) Me.groupBox1.Dock = System.Windows.Forms.DockStyle.Left Me.groupBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!) Me.groupBox1.Name = "groupBox1" Me.groupBox1.Size = New System.Drawing.Size(152, 266) Me.groupBox1.TabIndex = 3 Me.groupBox1.TabStop = False Me.groupBox1.Text = "Automatic" " "automaticPanel " Me.automaticPanel.BackColor = System.Drawing.Color.White Me.automaticPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.automaticPanel.Dock = System.Windows.Forms.DockStyle.Fill Me.automaticPanel.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.automaticPanel.Location = New System.Drawing.Point(3, 31) Me.automaticPanel.Name = "automaticPanel" Me.automaticPanel.Size = New System.Drawing.Size(146, 232) Me.automaticPanel.TabIndex = 0 " "MultiLineTextForm " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(162, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.groupBox1}) Me.Name = "MultiLineTextForm" Me.Text = "MultiLineTextForm" Me.groupBox1.ResumeLayout(False) Me.ResumeLayout(False) End Sub
- End Region
Dim s As String = "Line 1" & vbCrLf & "Line 2" & vbCrLf & "Line 3" & vbCrLf & "Line 4" & vbCrLf & "Line 5" & vbCrLf & "Line 6"
Private Sub automaticPanel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles automaticPanel.Paint Dim g As Graphics = e.Graphics Dim layoutRect As RectangleF = RectangleF.op_Implicit(automaticPanel.ClientRectangle) g.DrawString(s, Me.Font, Brushes.Black, layoutRect) Dim size As SizeF = g.MeasureString(s, Me.Font, layoutRect.Size) g.DrawRectangle(Pens.Black, 0, 0, size.Width, size.Height) End Sub Private Sub manualPanel_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles automaticPanel.Layout Me.automaticPanel.Refresh() End Sub
End Class
</source>
MultiLine Text Manual Layout
<source lang="vbnet"> Imports System Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Globalization Imports System.Text Imports System.Collections Public Class MainClass
Shared Sub Main() Dim myform As Form = New MultiLineTextForm() Application.Run(myform) End Sub
End Class Public Class MultiLineTextForm
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. Friend WithEvents groupBox2 As System.Windows.Forms.GroupBox Friend WithEvents manualPanel As System.Windows.Forms.Panel Friend WithEvents automaticPanel As System.Windows.Forms.Panel <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.groupBox2 = New System.Windows.Forms.GroupBox() Me.manualPanel = New System.Windows.Forms.Panel() Me.automaticPanel = New System.Windows.Forms.Panel() Me.groupBox2.SuspendLayout() Me.SuspendLayout() " "groupBox2 " Me.groupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.manualPanel}) Me.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill Me.groupBox2.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!) Me.groupBox2.Location = New System.Drawing.Point(155, 0) Me.groupBox2.Name = "groupBox2" Me.groupBox2.Size = New System.Drawing.Size(137, 266) Me.groupBox2.TabIndex = 5 Me.groupBox2.TabStop = False Me.groupBox2.Text = "Manual" " "manualPanel " Me.manualPanel.BackColor = System.Drawing.Color.White Me.manualPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.manualPanel.Dock = System.Windows.Forms.DockStyle.Fill Me.manualPanel.Font = New System.Drawing.Font("Microsoft Sans Serif", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.manualPanel.Location = New System.Drawing.Point(3, 31) Me.manualPanel.Name = "manualPanel" Me.manualPanel.Size = New System.Drawing.Size(131, 232) Me.manualPanel.TabIndex = 0 "MultiLineTextForm " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.groupBox2}) Me.Name = "MultiLineTextForm" Me.Text = "MultiLineTextForm" Me.groupBox2.ResumeLayout(False) Me.ResumeLayout(False) End Sub
- End Region
Dim s As String = "Line 1" & vbCrLf & "Line 2" & vbCrLf & "Line 3"
Private Sub manualPanel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles manualPanel.Paint Dim g As Graphics = e.Graphics Dim y As Single = 0 Dim arrPens As Pen() = New Pen() {Pens.Red, Pens.Green, Pens.Blue} Dim line As String For Each line In s.Split(vbCrLf) Dim width As Single = manualPanel.ClientRectangle.Width Dim height As Single = manualPanel.ClientRectangle.Height - y Dim layoutRect As RectangleF = New RectangleF(0, y, width, height) Dim format As StringFormat = New StringFormat(StringFormatFlags.NoWrap Or StringFormatFlags.DisplayFormatControl) g.DrawString(line, Me.Font, Brushes.Black, layoutRect, format) Dim size As SizeF = g.MeasureString(line, Me.Font, layoutRect.Size, format) g.DrawRectangle(arrPens(CInt(y / Me.Font.GetHeight(g))), 0, y, size.Width, size.Height) y = y + Me.Font.GetHeight(g) Next End Sub Private Sub manualPanel_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles manualPanel.Layout Me.manualPanel.Refresh() End Sub
End Class
</source>